Extract method refactor (C# in VS 2010)

David Nicholson <[email protected]> Wed, 19 May 2010 10:20:39 -0400
Newsgroups gmane.comp.windows.devel.dotnet.advanced
Message-ID <[email protected]>
I have a function like this:

        private void DoSomething(List<string> slist)
        {
            foreach (string s in slist)
            {
                if (s =3D=3D "b")
                    return;
            }

            string x =3D "Hello";
        }

and I select all of the foreach loop. If I refactor/extract method I get =
a message saying that all paths must be terminated by a return statement =
if any are.

If I remove the assignment to x at the end (outside the part I want to re=
factor), it works.

I don't understand why the assignment stops it doing something like this:=


        private void DoSomething(List<string> slist)
        {
            f(slist);

            string x =3D "World";
        }

        private static void f(List<string> slist)
        {
            foreach (string s in slist)
            {
                if (s =3D=3D "b")
                    return;
            }
        }

Thanks
David.

=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D
View archives and manage your subscription(s) at http://peach.ease.lsoft.com/archives