你能解释我应该如何避免这个警告:在Visual Studio 2010 Express中"检测到无法访问的代码"?我正在从手册中学习C#.这是一个创建简单方法的练习.我正如书中所写的那样进入这个例子.谢谢.
public class Multiply
{
//Multiplies argument by 4
public static int MultiplyBy4(int aNumber)
{
return 4 * aNumber;
//Shows ways of passing various arguments to a method public static void Main
int x = 7;
int y = 20;
int z = -3;
int result = 0;
result = MultiplyBy4(x);
Console.WriteLine("Passsing a variable, x : {0}", result);
result = MultiplyBy4(y + 2);
Console.WriteLine("Passing an expression, Y + 2: {0}", result);
result = 5 + MultiplyBy4(z);
Console.WriteLine("Using …Run Code Online (Sandbox Code Playgroud) 无法访问的代码是Java等语言中的编译时错误.但为什么它只是在C++和C中发出警告?考虑以下示例:
#include <iostream>
int f()
{
int a=3;
return a;
int b=6; // oops it is unreachable code
std::cout<<b; // program control never goes here
}
int main()
{
std::cout<<f()<<'\n';
}
Run Code Online (Sandbox Code Playgroud)
不应该编译器在这个程序中抛出一个错误,因为函数f()中的return语句之后的语句永远不会被执行?允许无法访问代码的原因是什么?
编写无法访问的代码时,Java编译器会抱怨.例如
public void go()
{
return;
System.out.println("unreachable");
}
Run Code Online (Sandbox Code Playgroud)
但是,当您在匿名类中定义一个无法从任何地方访问的新方法时,编译器不会抱怨.它允许你这样做,为什么?例如,
class A
{
public void go()
{
System.out.println("reachable - A");
}
}
class B
{
public static void main(String [] args)
{
A a = new A() {
public void go()
{
System.out.println("reachable - B");
}
public void foo()
{
System.out.println("unreachable - B");
}
};
a.go(); // valid
a.foo(); // invalid, compiler error
}
}
Run Code Online (Sandbox Code Playgroud) 我似乎找不到解决这个问题的方法.我正在做的就是声明一个整数,它告诉我代码无法访问.
private class myStack{
Object [] myStack = new Object[50];
private void push(Object a){
int count = 50;
while(count>0){
myStack[count]=myStack[count-1];
count--;
}
myStack[0]=a;
}
private Object pop(){
return myStack[0];
int count2 = 0; //Unreachable Code
}
}
Run Code Online (Sandbox Code Playgroud) 我正在尝试在C#中创建一个函数类,以便在excel自动化加载项中使用.使用下面的简单代码,我想要在与用户选择的颜色匹配的范围内添加值(例如,和范围("A1:A10"),其中单元格颜色与"B1"相同) :colourSum(A1:A10,B1).
public double ColourSum(Range RangeToSum, Range cellContainingColourToSum)
{
double currentTotal = 0;
//return 0; this code is edited from my original posting, due to posting error
for (int i = 0; i < RangeToSum.Cells.Count;i++) //error at 'int i'
{
double iColour = RangeToSum.Interior.ColorIndex(i);
if (iColour == cellContainingColourToSum.Interior.ColorIndex)
{
currentTotal = currentTotal + RangeToSum.Value2(i);
//return currentTotal;
}
}
return currentTotal;
}
Run Code Online (Sandbox Code Playgroud)
不幸的是,上面的代码在第4行返回"检测到无法访问的代码".我给出的代码示例是我实际想要做的一个简化示例,但很好地说明了我的观点.我的代码有问题,还是我可以用更好的方式来避免这个问题?
谢谢,Rico.
总结一下这个问题,下面的代码完全有效(改为(int i ... with foreach(Range r ...):
public double ColourSum(Range RangeToSum, Range cellContainingColourToSum)
{
double currentTotal = …Run Code Online (Sandbox Code Playgroud) 我有以下代码
switch (attr.templateType) {
case 'text': return tpl_default; break;
case 'currency': return tpl_currency; break;
case 'percentage': return tpl_percentage; break;
case 'latlong': return tpl_latlong; break;
case 'tel': return tpl_phone; break;
case 'number': return tpl_number; break;
case 'address': return tpl_address; break;
case 'date': return tpl_date; break;
case 'permissions': return tpl_permissions; break;
case 'pagination': return tpl_pagination; break;
case 'time': return tpl_time; break;
case 'notEmpty': return tpl_notEmpty; break;
default: return tpl_default; break;
}
Run Code Online (Sandbox Code Playgroud)
和lint告诉我所有中断都"检测到无法访问的代码".如果我拿出休息时间,lint没有错误.
有谁知道为什么?代码工作,没有任何错误.
我正在实现一些通用IEqualityComparer<T> Equal()方法,当交换机中的代码无法访问时,我没有明显的理由:
public bool Equals(T x, T y)
{
switch (nameof(T))
{
case nameof(Accessory):
return (x as Accessory).Id == (y as Accessory).Id;//not reachable
default:
return false;
}
}
Run Code Online (Sandbox Code Playgroud)
有人有线索吗?
灵感来自问题
在我提出这个问题之前,我读过:
问题的挑战是
编写一个程序,该程序具有可访问的goto语句,但相应的标记语句无法访问 - Eric Lippert
一个可行的答案就像
// the 3 lines are not important but declare variable for afterwards use
var whateverException=new Exception("whatever exception");
var whateverAction=default(Action);
whateverAction=() => whateverAction();
Run Code Online (Sandbox Code Playgroud)
try {
goto whateverLabel; // (1) the goto is reachable
}
finally {
throw whateverException; // (3) because finally hijacks
}
whateverLabel: // (2) but the label is not really reached
whateverAction();
Run Code Online (Sandbox Code Playgroud)
我想知道在一个单线程程序中,它是唯一一个指向无法访问标签的可达goto吗?以下代码也被认为是可行的答案吗?
here:
int d=0, n=1/d;
goto here;
Run Code Online (Sandbox Code Playgroud) 我在for循环中收到C4702:无法访问的代码警告; 奇怪的是 - 通过分解parens内部的组件 - 警告指向增量部分.这是一个演示此错误的示例程序:
int main()
{
int foo = 3;
for (int i = 0;
i < 999;
i++) // warning on this line
{
if (foo == 4);
{
break;
}
}
return 0;
}
Run Code Online (Sandbox Code Playgroud)
我无法弄清楚这一行有什么问题,因为for循环看起来非常简单.
我知道如果java找到一行代码,保证控件永远不会到达,那么编译器会报告无法访问的代码错误.
考虑以下代码.
static int method1() {
try{ return 1; }
catch(Exception e){ } // LINE-1
finally{ }
System.out.println("abc"); //LINE-2
return 2;
}
}
Run Code Online (Sandbox Code Playgroud)
在上面的代码
1中, try块保证通过返回1退出,但是在finally块(LINE-2以后)之后仍然可以到达.
2.如果我评论catch块(LINE-1),则LINE-2变得无法访问.
为什么会如此.编译器是否能够在case-1的try块中看到无条件返回.
unreachable-code ×10
c# ×4
java ×3
c ×2
c++ ×2
excel ×1
exception ×1
function ×1
javascript ×1
return ×1