首页 新闻 会员 周边 捐助

求几道小题目的答案,发现网上答案有些错误?

0
悬赏园豆:10 [已解决问题] 解决于 2008-07-15 18:40
<P>1..try {}里有一个return语句,那么紧跟在这个try后的finally {}里的code会不会被执行,什么时候被执行,在return前还是后?<BR>答:会执行,在return前执行。</P> <P>但是我调试时,是在return后执行的,这是我的调试代码:</P> <P>YeShu.BLL.Dictionary_Type bll = new YeShu.BLL.Dictionary_Type();<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; try<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; {</P> <P>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; int i = bll.Add(TextBox1.Text.Trim());<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Page.ClientScript.RegisterClientScriptBlock(this.GetType(), "aa", "alert('" + i.ToString() + "')", true);<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; return 2;<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; }</P> <P>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; finally<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; {<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; bll = null;<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; }</P> <P>2.<SPAN lang=EN-US style="FONT-SIZE: 10.5pt; COLOR: black; FONT-FAMILY: 新宋体; mso-bidi-font-family: 'Times New Roman'; mso-font-kerning: 1.0pt; mso-ansi-language: EN-US; mso-fareast-language: ZH-CN; mso-bidi-language: AR-SA">swtich</SPAN><SPAN style="FONT-SIZE: 10.5pt; COLOR: black; FONT-FAMILY: 新宋体; mso-bidi-font-family: 'Times New Roman'; mso-font-kerning: 1.0pt; mso-ansi-language: EN-US; mso-fareast-language: ZH-CN; mso-bidi-language: AR-SA">是否能作用在<SPAN lang=EN-US>byte</SPAN>上,是否能作用在<SPAN lang=EN-US>long</SPAN>上,是否能作用在<SPAN lang=EN-US>String</SPAN>上<SPAN lang=EN-US>?<BR></SPAN>答:<SPAN lang=EN-US>switch</SPAN>(<SPAN lang=EN-US>expr1</SPAN>)中,<SPAN lang=EN-US>expr1</SPAN>是一个整数表达式。因此传递给<SPAN lang=EN-US>&nbsp;switch&nbsp;</SPAN>和<SPAN lang=EN-US>&nbsp;case&nbsp;</SPAN>语句的参数应该是<SPAN lang=EN-US>&nbsp;int</SPAN>、<SPAN lang=EN-US>&nbsp;short</SPAN>、<SPAN lang=EN-US>&nbsp;char&nbsp;</SPAN>或者<SPAN lang=EN-US>&nbsp;byte</SPAN>。<SPAN lang=EN-US>long,string&nbsp;</SPAN>都不能作用于<SPAN lang=EN-US>swtich</SPAN>。<SPAN lang=EN-US><BR style="mso-special-character: line-break"><BR style="mso-special-character: line-break">但是我测试时long和string都可以,测试代码:</SPAN></SPAN></P> <P><SPAN style="FONT-SIZE: 10.5pt; COLOR: black; FONT-FAMILY: 新宋体; mso-bidi-font-family: 'Times New Roman'; mso-font-kerning: 1.0pt; mso-ansi-language: EN-US; mso-fareast-language: ZH-CN; mso-bidi-language: AR-SA"><SPAN lang=EN-US>private void bb(string str)<BR>&nbsp;&nbsp;&nbsp; {<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; switch (str)<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; {<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; case "1":<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Response.Write("aaa1");<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; break;<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; case "2":<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Response.Write("aaa2");<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; break;<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; default:<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Response.Write("aaat");<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; break;<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; }<BR>&nbsp;&nbsp;&nbsp; }</SPAN></SPAN></P> <P><SPAN style="FONT-SIZE: 10.5pt; COLOR: black; FONT-FAMILY: 新宋体; mso-bidi-font-family: 'Times New Roman'; mso-font-kerning: 1.0pt; mso-ansi-language: EN-US; mso-fareast-language: ZH-CN; mso-bidi-language: AR-SA"><SPAN lang=EN-US>到底是我测试出错了,还是答案是错误的</SPAN></SPAN></P>
999999999999999的主页 999999999999999 | 初学一级 | 园豆:3
提问于:2008-07-13 18:36
< >
分享
最佳答案
0
第一个问题,return 确实是在fininal后执行,答案是对的,看下面代码: static int Test() { int ret = 0; try { return ret; } finally { ret = 1; } } 汇编代码如下 .method private hidebysig static int32 Test() cil managed { // Code size 16 (0x10) .maxstack 1 .locals init ([0] int32 'ret', [1] int32 CS$1$0000) IL_0000: nop IL_0001: ldc.i4.0 IL_0002: stloc.0 .try { IL_0003: nop IL_0004: ldloc.0 IL_0005: stloc.1 IL_0006: leave.s IL_000d } // end .try finally { IL_0008: nop IL_0009: ldc.i4.1 IL_000a: stloc.0 IL_000b: nop IL_000c: endfinally } // end handler IL_000d: nop IL_000e: ldloc.1 IL_000f: ret } // end of method Program::Test 这里,你可以看到在finally之后才执行 IL_000f: ret 但从程序结果来看,似乎是return在finally之前,因为你看 IL_0004: ldloc.0 IL_0005: stloc.1 这两句发生在finally之前,在此时已经将返回值写入了堆栈,所以在 finally中再对局部变量ret复制,返回值不会改变。但返回值写入堆栈, 并不代表函数已经返回,函数只有在 IL_000f: ret 这一句才返回, 函数返回的标志是将IP指针从堆栈中弹出,以便程序可以执行函数的 下一条语句。 第二道题,答案错了,对于.net来说 String , long等都可以用在switch里面, 对于C++则不可以。
eaglet | 专家六级 |园豆:17139 | 2008-07-14 08:13
其他回答(2)
0
finally 在return后也会执行的. 整型和字符型都可以~~
沙加 | 园豆:3680 (老鸟四级) | 2008-07-13 21:10
0
看反编译的代码那是最正确的,因为可以看到真正的流程! 另外第2题C#对Switch没有那么多限制。。倒是我记得Java的限制也很多!
Allie | 园豆:707 (小虾三级) | 2008-07-14 11:22
清除回答草稿
   您需要登录以后才能回答,未注册用户请先注册