以下是带注释的示例:
class Program
{
// first version of structure
public struct D1
{
public double d;
public int f;
}
// during some changes in code then we got D2 from D1
// Field f type became double while it was int before
public struct D2
{
public double d;
public double f;
}
static void Main(string[] args)
{
// Scenario with the first version
D1 a = new D1();
D1 b = new D1();
a.f = b.f = 1; …Run Code Online (Sandbox Code Playgroud) private static ext.clsPassageiro ConversaoPassageiro(ncl.clsPassageiro clsPassageiro)
{
ext.clsPassageiro _result = new ext.clsPassageiro();
throw new NotImplementedException();
return _result;
}
Run Code Online (Sandbox Code Playgroud)
显示"检测到无法访问的代码" return _result;,
private static ext.clsPassageiro ConversaoPassageiro(ncl.clsPassageiro clsPassageiro)
{
ext.clsPassageiro _result = new ext.clsPassageiro();
return _result;
throw new NotImplementedException();
}
Run Code Online (Sandbox Code Playgroud)
没有显示"检测到无法访问的代码" throw new NotImplementedException();,
为什么第二种情况没有显示警告?
下面的代码会抱怨
try
{
session.Save(obj);
return true;
}
catch (Exception e)
{
throw e;
return false; // this will be flagged as unreachable code
}
Run Code Online (Sandbox Code Playgroud)
而这不会:
try
{
session.Save(obj);
return true;
}
catch (Exception e)
{
return false;
throw e;
}
Run Code Online (Sandbox Code Playgroud)
我不明白......我以为我的csc101告诉我,return语句应该始终是函数中的最后一个语句,它退出函数并将控制权返回给调用代码.为什么这会违背我教授的逻辑,为什么只有其中一个产生警告呢?
当我在Visual Studio中尝试使用C#中的示例表达式时
public int Test()
{
if (10/2 == 5)
throw new Exception();
return 0;
}
Run Code Online (Sandbox Code Playgroud)
当我保持表达式10/2 == 5时,vs.net会自动发出警告"检测到无法访问的代码".
如果我改变表达式10/2 == 6,IDE很高兴吗?怎么会发生?
编辑:对不完整的问题抱歉.即使在编译代码之前,它也会立即发生并发生?
我已经对每个回复进行了投票,并接受了基于FIFO的第一个答案