C#局部变量

问题描述:

有人可以请解释为什么我得到:C#局部变量

“使用未分配的局部变量number_of_column” 为:如果(我< number_of_column -1)

,什么是最好的方式处理这个?

static void Main(string[] args) 
{ 
    int number_of_column; 
    if (Directory.Exists(path)) 
    { 
     var file = dir.GetFiles().OrderByDescending(f => f.LastWriteTime).First(); 
     string file1 = file.ToString(); 
     Console.WriteLine(file1); 
     StreamReader sr = new StreamReader(path + "\\" + file1); 
     string line; 
     while ((line = sr.ReadLine()) != null) 
     { 
      if (start == true) 
      { 
       string[] line1 = line.Split(','); 
       number_of_column = line1.Count(); 
       i = 0; 
       foreach (string s in line1) 
       { 
        if ((s != "0") || (!string.IsNullOrEmpty(s))) 
        { 
         col[i] = "checked"; 

        } 
        i++; 
       } 
      } 
      else 
      { 
       if (line.Contains("Timestamp") && line.Contains("LiveStandby") && line.Contains("peak")) 
       { 
        start = true; 

       } 
      } 
     } 
     sr.Close(); 

     i=0; 
     foreach (string s in col) 
     { 
      if (i < number_of_column -1) 
      { 
+0

想想为什么你声明'number_of_column'都将是您解决问题的第一步。 – 2013-02-28 17:48:06

如果你从来没有进入while循环,该变量没有分配的值。

如果你点击if中的else分支,变量没有赋值。

这些是你得到这个错误信息的原因。

您可以通过在声明变量时分配值来解决此问题。

C#要求您在使用它们之前初始化局部变量。 成员变量,这是没有必要的,他们得到他们的默认值自动分配。

+0

@MarcinJuraszek它看起来像在它之后的foreach循环中 – 2013-02-28 16:50:35

试着改变你的第一行:

static void Main(string[] args){ int number_of_column = 0;if (Directory.Exists(path)) 

或格式化:

static void Main(string[] args) 
{ 
    int number_of_column = 0; 
    if (Directory.Exists(path)) ... 

这意味着你没有给予任何初始值到它。

替换此

int number_of_column; 

随着

int number_of_column = 0; 

这将解决您的问题。

作为一种防止副作用的机制,C#不允许使用未初始化的变量 - 声明的变量,但没有明确设置的值。这就是你得到错误的原因。初始化为零可以解决问题。

+0

是的,在C#中,它是必需的。 – 2013-02-28 16:56:20

完全精炼代码的

  1. file然后file1只是多余的,语句可以合并和被更好的语义含义。

  2. if (start == true)过于复杂,只是if(start)

  3. 使用using Statement woule更好地确保像StreamReader的正确使用IDisposable

  4. Path.Combine Method将两个字符串组合成一个路径。

  5. 这两个foreach s最好是for,因为你是按顺序迭代数组。

  6. 不难发现number_of_column不是必要的,因为您只是存储数组迭代的计数。


代码:

static void Main(string[] args) { 
    int number_of_column; // never used 

    if(Directory.Exists(path)) { 
     var file1=(
      from f in dir.GetFiles() 
      orderby f.LastWriteTime 
      select f 
      ).First().ToString(); 

     Console.WriteLine(file1); 

     using(var sr=new StreamReader(Path.Combine(path, file1))) 
      for(String line; null!=(line=sr.ReadLine());) { 
       if(start) { 
        var line1=line.Split(','); 

        for(var i=0; i<line1.Length; ++i) { 
         var s=line1[i]; 

         if("0"!=s||!String.IsNullOrEmpty(s)) 
          col[i]="checked"; 
        } 

        continue; 
       } 

       if(
        line.Contains("Timestamp") 
        && 
        line.Contains("LiveStandby") 
        && 
        line.Contains("peak" 
        )) 
        start=true; 
      } 

     for(var i=0; i<col.Length; ++i) { 
      // following lines are no more needed 
      // if(i<number_of_column-1) { 
      // } 
     } 
    } 
}