获取错误为CS0162:检测到无法访问的代码 [英] Getting Error is cs0162: unreachable code detected

查看:172
本文介绍了获取错误为CS0162:检测到无法访问的代码的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

using System;
using System.Collections.Generic;
using System.Text;


namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {

        }
        decimal ParseDecimal(string number)
        {
            if (number.Equals("0E+3",StringComparison.OrdinalIgnoreCase))
            {
                return 0;
            }

            return decimal.Parse(number, System.Globalization.NumberStyles.Any);
           Console.WriteLine(number);
        }
    }
}

推荐答案

从方法返回值后调用
Console.WriteLine(number);

,因此它将永远不会被调用.

像这样尝试:

after you return value from method.So it will never be called.

Try it like that:

using System;
using System.Collections.Generic;
using System.Text;


namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
             Program p = new Program();
             decimal result = p.ParseDecimal("2.5");
             //you can return void if you will not use result
        }
        decimal ParseDecimal(string number)
        {
            if (number.Equals("0E+3",StringComparison.OrdinalIgnoreCase))
            {
                return 0;
            }
            Console.WriteLine(number);
            return decimal.Parse(number, System.Globalization.NumberStyles.Any);         
        }
    }
}


检测到无法访问的代码

由于最后一行,由于最后一行写在
Unreachable code detected

Because of the last line .The last line of the function never execute due to the return line written above the
Console.WriteLine(number);


上方,因此该函数的最后一行从不执行
删除最后一行或将其写在return语句上方.

并考虑答案1请在主函数中写一些东西(调用函数).



Remove the last line or write the line above the return statement.

and also consider answer 1 Please write something(call the function) in the main function.


尝试以下代码:-
try this code:-
using System;
using System.Collections.Generic;
using System.Text;


namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
           decimal dec = ParseDecimal("2.78");
           Console.WriteLine(dec);
        }
       static decimal ParseDecimal(string number)
        {
         //#pragma warning disable
            if (number.Equals("0E+3", StringComparison.OrdinalIgnoreCase))
            {
                return 0;
            }
           return decimal.Parse(number, System.Globalization.NumberStyles.Any);
        }
    }
}


这篇关于获取错误为CS0162:检测到无法访问的代码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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