c#编译器返回错误并非所有代码路径都返回一个值

时间:2016-06-14 08:13:02

标签: c#

get
{
    string dirName = System.IO.Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location);
    string pathName;
    DirectoryInfo d = new DirectoryInfo("TradeBotData");
    if (!d.Exists)
    {
        if (d.Parent.Name.ToString() == "Plugins")
        {
            d.Create();
            return d.FullName;
        }
    }
    else
    {
        if (d.Parent.Name.ToString() == "Plugins")
        {
            return d.FullName;
        }
        else
        { 
            Console.WriteLine("Data path Fallback!!!");
            pathName = System.IO.Path.Combine(dirName, @"\TradeBotData");
            System.IO.Directory.CreateDirectory(pathName);
            Console.WriteLine("Created Save Folder At: {0} :", pathName);
            return pathName;
        }
    }
}

我不确定为什么会这样。我认为所有代码路径都返回一个值,因为我有ifelse。 如果我在代码中插入return "";,它就会返回"&#34 ;;无限。

请告知。

2 个答案:

答案 0 :(得分:3)

如果DirectoryInfo不存在,编译器将检查d.Parent.Name.ToString() == "Plugins"是否为if,带有return语句的代码将执行,否则编译器将执行抛出异常:“并非所有代码路径都返回一个值”因为你没有返回任何内容

所以你错过了get { string dirName =System.IO.Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location); string pathName; DirectoryInfo d = new DirectoryInfo("TradeBotData"); if (!d.Exists) { if (d.Parent.Name.ToString() == "Plugins") { d.Create(); return d.FullName; } //////////HERE/////// } else { if (d.Parent.Name.ToString() == "Plugins") { return d.FullName; } else { Console.WriteLine("Data path Fallback!!!"); pathName = System.IO.Path.Combine(dirName, @"\TradeBotData"); System.IO.Directory.CreateDirectory(pathName); Console.WriteLine("Created Save Folder At: {0} :", pathName); return pathName; } } }

 OkHttpClient httpClient = new OkHttpClient();

        httpClient.networkInterceptors().add(new Interceptor() {
            @Override
            public com.squareup.okhttp.Response intercept(Chain chain) throws IOException {
                Request request = chain.request().newBuilder().addHeader("id",id).build();
                return chain.proceed(request);
            }
        });

答案 1 :(得分:0)

将pathName初始化为空字符串。然后在你的if / else逻辑中设置它,并在get和{}返回它:

  get
            {
              string dirName = System.IO.Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location);
              string pathName = String.Empty;
              DirectoryInfo d = new DirectoryInfo("TradeBotData");
              if (!d.Exists) {
                if (d.Parent.Name == "Plugins") {
                  d.Create();
                  pathName = d.FullName;
                }
              } else {
                if (d.Parent.Name == "Plugins") {
                  pathName = d.FullName;
                } else {
                  Console.WriteLine("Data path Fallback!!!");
                  pathName = System.IO.Path.Combine(dirName, @"\TradeBotData");
                  System.IO.Directory.CreateDirectory(pathName);
                  Console.WriteLine("Created Save Folder At: {0} :", pathName);

                }
              }

              return pathName;
            }