A“goto”用C#构造?他们在想什么?! [英] A "goto" construct in C#? What were they thinking?!

查看:86
本文介绍了A“goto”用C#构造?他们在想什么?!的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

虽然我在过去20年左右玩过C,C ++和Java,但我的主要语言是BASIC,QBASIC,然后是Visual Basic,并且

最后是Visual Basic .NET。但最近,我一直在使用C#和我

绝对*爱*它。在我将代码写入编辑器之前,它让我更多地考虑我正在做什么

。我写的代码比

更好。


到目前为止我唯一不喜欢的是开关

构造。我不能这样做:


switch(student.Grade){

case" 1"," 2"," 3" ,4,5,6:

school =" Elementary";

break;

case 7,8,9:

school =" Junior High";

break;

default :

school =" High School" ;;

休息;

}


相反,我必须这样做:


开关(student.Grade){

case" 1":

school =" ;小学;

休息;

case" 2":

goto case" 1";

break;

While I''d toyed with C, C++, and Java over the last 20 years or so, my
principal language has been BASIC, QBASIC, then Visual Basic, and
finally Visual Basic .NET. But lately, I''ve been using C# and I
absolutely *love* it. It makes me think more about what I''m doing it
before I just spew code into the editor. I''m writing better code than
ever.

The only thing so far that I don''t like about it is the switch
construct. I can''t do this:

switch(student.Grade) {
case "1", "2", "3", "4", "5", "6":
school = "Elementary";
break;
case "7", "8", "9":
school = "Junior High";
break;
default:
school = "High School";
break;
}

Instead, I have to do this:

switch(student.Grade) {
case "1":
school = "Elementary";
break;
case "2":
goto case "1";
break;

推荐答案



Mike Hofer写道:

Mike Hofer wrote:
虽然我玩弄了在过去20年左右的时间里,使用C,C ++和Java,我的主要语言是BASIC,QBASIC,然后是Visual Basic,最后是V isual Basic .NET。但最近,我一直在使用C#而且我绝对喜欢它。在我将代码写入编辑器之前,它让我更多地思考我在做什么。我写的代码比
更好。

到目前为止我唯一不喜欢的是开关
构造。我不能这样做:

switch(student.Grade){
case1,2,3,4, 5"," 6":
school =" Elementary";
break;
case7,8,9:
学校=初中;
休息;
默认:
学校="高中;
休息;
}

相反,我必须这样做:

switch(student.Grade){
case" 1":
school =" Elementary" ;;
break;
案例" 2":
goto case" 1" ;;
break;



}
换句话说,你似乎无法在没有
使用goto构造的情况下组合案例。

< RANT>一个frickin''GOTO !! !他们在想什么?!我认为我们本应该从意大利面代码中移走代码!!!< / RANT>

所以我的问题是,你如何处理方案在哪里你需要一个块来处理多个案例,如上所示?从编码的角度来看,有更优雅的方式吗?

另外,我注意到他们添加了对goto的全面支持 - 根据C#语言参考,你可以标记行,然后使用转到
将执行转移到该行。对我来说,这有点旧的非结构化BASIC代码。有没有人在生产代码中使用此功能?
在什么情况下你觉得它有用,如果有的话?我想我只是想弄清楚微软是如何证明包含一个似乎非常普遍地厌恶一种全新语言的关键词
那些被设计为面向对象的(从而固有地结构化)。

非常感谢您的投入!
While I''d toyed with C, C++, and Java over the last 20 years or so, my
principal language has been BASIC, QBASIC, then Visual Basic, and
finally Visual Basic .NET. But lately, I''ve been using C# and I
absolutely *love* it. It makes me think more about what I''m doing it
before I just spew code into the editor. I''m writing better code than
ever.

The only thing so far that I don''t like about it is the switch
construct. I can''t do this:

switch(student.Grade) {
case "1", "2", "3", "4", "5", "6":
school = "Elementary";
break;
case "7", "8", "9":
school = "Junior High";
break;
default:
school = "High School";
break;
}

Instead, I have to do this:

switch(student.Grade) {
case "1":
school = "Elementary";
break;
case "2":
goto case "1";
break;
.
.
.
}

In other words, you don''t appear to be able to combine cases without
using a goto construct.

<RANT>A frickin'' GOTO!!!! What in the heck were they thinking?! I
thought we were supposed to be moving AWAY from spaghetti
code!!!</RANT>

So my question to you all is, how do you deal with scenarios where you
need a single block to handle multiple cases, as shown above? From a
coding perspective, is there a more elegant way to do it?

Also, I notice that they added full support for goto--according to the
C# language reference, you can label lines and then use goto to
transfer execution to that line. To me, this smacks of old unstructured
BASIC code. Does anyone use this functionality in production code?
Under what circumstances do you find it useful, if at all? I guess I''m
just trying to figure out how Microsoft justified including a keyword
that seems to be pretty universally loathed in a brand new language
that''s designed to be object oriented (and thereby inherently
structured).

Thanks much for your input!






你可以使用if if blocks和if(student.Grade == 1 || student.Grade == 2

......

或student.Grade< 7
you can use else if blocks and if(student.Grade == 1 || student.Grade == 2
......
or student.Grade < 7


" Mike Hofer"< kc ******** @ gmail.com>在留言中写道

news:11 **********************@f14g2000cwb.googlegr oups.com ...
"Mike Hofer" <kc********@gmail.com> wrote in message
news:11**********************@f14g2000cwb.googlegr oups.com...
虽然我玩弄C,C ++和Java在过去20年左右的时间里,我的主要语言是BASIC,QBASIC,然后是Visual Basic,最后是Visual Basic .NET。但是最近,我一直在使用C#和我绝对喜欢它。它让我更多地思考我在做什么
在我将代码写入编辑器之前。我写的代码比
更好。

到目前为止我唯一不喜欢的是开关
构造。我不能这样做:

switch(student.Grade){
case1,2,3,4, 5"," 6":
school =" Elementary";
break;
case7,8,9:
学校=初中;
休息;
默认:
学校="高中;
休息;
}

相反,我必须这样做:

switch(student.Grade){
case" 1":
school =" Elementary" ;;
break;
案例" 2":
goto case" 1" ;;
break;



}
换句话说,你似乎无法在没有
使用goto构造的情况下组合案例。

< RANT>一个frickin''GOTO !! !他们在想什么?!我认为我们本应该从意大利面代码中移走代码!!!< / RANT>

所以我的问题是,你如何处理方案在哪里你需要一个块来处理多个案例,如上所示?从编码的角度来看,有更优雅的方式吗?

另外,我注意到他们添加了对goto的全面支持 - 根据C#语言参考,你可以标记行,然后使用转到
将执行转移到该行。对我来说,这有点旧的非结构化BASIC代码。有没有人在生产代码中使用此功能?
在什么情况下你觉得它有用,如果有的话?我想我只是想弄清楚微软是如何证明包含一个似乎非常普遍地厌恶一种全新语言的关键词
那些被设计为面向对象的(从而固有地结构化)。

非常感谢您的投入!
While I''d toyed with C, C++, and Java over the last 20 years or so, my
principal language has been BASIC, QBASIC, then Visual Basic, and
finally Visual Basic .NET. But lately, I''ve been using C# and I
absolutely *love* it. It makes me think more about what I''m doing it
before I just spew code into the editor. I''m writing better code than
ever.

The only thing so far that I don''t like about it is the switch
construct. I can''t do this:

switch(student.Grade) {
case "1", "2", "3", "4", "5", "6":
school = "Elementary";
break;
case "7", "8", "9":
school = "Junior High";
break;
default:
school = "High School";
break;
}

Instead, I have to do this:

switch(student.Grade) {
case "1":
school = "Elementary";
break;
case "2":
goto case "1";
break;
.
.
.
}

In other words, you don''t appear to be able to combine cases without
using a goto construct.

<RANT>A frickin'' GOTO!!!! What in the heck were they thinking?! I
thought we were supposed to be moving AWAY from spaghetti
code!!!</RANT>

So my question to you all is, how do you deal with scenarios where you
need a single block to handle multiple cases, as shown above? From a
coding perspective, is there a more elegant way to do it?

Also, I notice that they added full support for goto--according to the
C# language reference, you can label lines and then use goto to
transfer execution to that line. To me, this smacks of old unstructured
BASIC code. Does anyone use this functionality in production code?
Under what circumstances do you find it useful, if at all? I guess I''m
just trying to figure out how Microsoft justified including a keyword
that seems to be pretty universally loathed in a brand new language
that''s designed to be object oriented (and thereby inherently
structured).

Thanks much for your input!




开关(student.Grade)
{

case" 1":

case" 2":

case" 3":

case" 4":

case" 5":

case" 6":

school = 小学;

休息;

case" 7":

case" 8":

case9:

school =" Junior High";

break;

默认值:

school =" High School" ;;

休息;

}


问候


Richard Blewett - DevelopMentor
http://www.dotnetconsult.co.uk/weblog
http://www.dotnetconsult.co.uk



switch(student.Grade)
{
case "1":
case "2":
case "3":
case "4":
case "5":
case "6":
school = "Elementary";
break;
case "7":
case "8":
case "9":
school = "Junior High";
break;
default:
school = "High School";
break;
}

Regards

Richard Blewett - DevelopMentor
http://www.dotnetconsult.co.uk/weblog
http://www.dotnetconsult.co.uk


这篇关于A“goto”用C#构造?他们在想什么?!的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆