tag:blogger.com,1999:blog-6691579676571716537.post5079521661231436555..comments2023-07-02T10:08:07.024+01:00Comments on Project 20: Do you use goto in C#?Ian Dykeshttp://www.blogger.com/profile/08235675789374664427noreply@blogger.comBlogger7125tag:blogger.com,1999:blog-6691579676571716537.post-8301049070510204452009-04-08T10:01:00.000+01:002009-04-08T10:01:00.000+01:00Dont use switch either..However i thing gotos are ...Dont use switch either..<BR/><BR/><BR/>However i thing gotos are ok ( better than switch) when you have doubly nested loopsAnonymousnoreply@blogger.comtag:blogger.com,1999:blog-6691579676571716537.post-85792339728656341702008-09-12T21:33:00.000+01:002008-09-12T21:33:00.000+01:00I see nothing wrong with using GOTO in a Switch st...I see nothing wrong with using GOTO in a Switch statement.Anonymousnoreply@blogger.comtag:blogger.com,1999:blog-6691579676571716537.post-88172833091715094032008-03-25T19:56:00.000+00:002008-03-25T19:56:00.000+00:00I've made a response here<A HREF="http://iandykes.blogspot.com/2008/03/more-on-goto-in-c.html" REL="nofollow">I've made a response here</A>Ian Dykeshttps://www.blogger.com/profile/08235675789374664427noreply@blogger.comtag:blogger.com,1999:blog-6691579676571716537.post-57287449184095248202008-03-24T18:25:00.000+00:002008-03-24T18:25:00.000+00:00I think there's such a knee-jerk reaction against ...I think there's such a knee-jerk reaction against goto sometimes, that people instantly dismiss it as a bad construct, regardless of how it's used.<BR/><BR/>One thing to remember is that all the loops and conditionals you use in C# (if, while, for, foreach, switch...) are ALL implemented using some form of goto (breaks in IL) behind the scenes.<BR/><BR/>For example, take the following code and compile it in an class library in Release mode.<BR/><BR/>class myclass<BR/>{<BR/> void a(object x, object y)<BR/> {<BR/> if ((x == null) && (y == null))<BR/> {<BR/> System.Console.WriteLine("foo");<BR/> }<BR/> else<BR/> {<BR/> System.Console.WriteLine("bar");<BR/> }<BR/> }<BR/><BR/> void b(object x, object y)<BR/> {<BR/> if (x != null) goto end;<BR/> if (y != null) goto end;<BR/><BR/> System.Console.WriteLine("foo");<BR/><BR/> return;<BR/><BR/> end: System.Console.WriteLine("bar");<BR/> }<BR/>}<BR/><BR/>Now, open this in Reflector and look at the IL and C# generated for method a versus method b...Akidanhttps://www.blogger.com/profile/09333584091024498099noreply@blogger.comtag:blogger.com,1999:blog-6691579676571716537.post-31195831542847575532007-11-18T18:17:00.000+00:002007-11-18T18:17:00.000+00:00The goto you see in reflector is probably just bec...The goto you see in reflector is probably just because the of the decompilation that reflector does. It doesn't mean that the original sourcecode looked like this, it just means that the MSIL code looks like this when represented as C#. I've seen this happen in reflector when viewing my own code too.Anonymousnoreply@blogger.comtag:blogger.com,1999:blog-6691579676571716537.post-29145701779249319692007-10-15T23:12:00.000+01:002007-10-15T23:12:00.000+01:00Being the 'junior' here i'll keep my comments brie...Being the 'junior' here i'll keep my comments brief :P<BR/><BR/>I think in some rare scenarios, where there are lots of if statements, loops and flags, the <B>GOTO</B> command can help rather than hinder. <BR/><BR/>Also, it must be a benefit performance wise, especially if you are throwing exceptions (which we know are expensive) to jump around the code.<BR/><BR/>I don't think it should be completely avoided, just evaluate carefully what you are trying to do in code before using it. Saying all this, I've used GOTO only a few times - all at uni (which seems so long ago now).Neil kilbridehttps://www.blogger.com/profile/11689141460411902032noreply@blogger.comtag:blogger.com,1999:blog-6691579676571716537.post-62424141070373576912007-10-10T20:21:00.000+01:002007-10-10T20:21:00.000+01:00As you know, I share many of your views on coding ...As you know, I share many of your views on coding style and techniques and I would say that a loathing of the goto is part of that. Whilst I am sure that someone can come up with an example of somthing that requires it, it would still be a case of the exception rather than the rule; as you say, the example you show could be coded without using it.<BR/>I recall when the first drafts of the C# specification were made available there were a few comments made to the effect of what a pity they didnt choose to exclude this contentious feature!.<BR/>IMHO the problem with it is that it promotes spaghetti type code in which paths intertwine. To return to your example note how the execution in the second branch of the case jumps to the first label/code block and then to the third, while another jump initiated from the default branch ends up between the two. <BR/><BR/>It's also worth mentioning in a discussion about the goto is the fact that it is possible to effect one without the use of the word. How many times do you see returns from the middle of a function? Not an issue if it occurs say after validating an input [though throwing an exception might be better style]. But if is after allocating a load of resources then there may be potential for problems to arise - less so with garbage collection in C# but those of us who used C++ are still seem to be mindful of these things!!Anonymousnoreply@blogger.com