如何避免初始化已经初始化的变量?

问题描述:

namespace text_test 
{ 
public class txt_program 


{ 
    public class txt 
    { 

     /* 0 */ 
     int[] M_array_0 = { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }; 
     /* 1 */ 
     int[] M_array_1 = { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }; 
     /* 2 */ 
     int[] M_array_2 = { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }; 
     /* 3 */ 
     int[] M_array_3 = { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }; 
     /* 4 */ 
     int[] M_array_4 = { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }; 
     /* 5 */ 
     int[] M_array_5 = { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }; 
     /* 6 */ 
     int[] M_array_6 = { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }; 
     /* 7 */ 
     int[] M_array_7 = { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }; 
     /* 8 */ 
     int[] M_array_8 = { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }; 
     /* 9 */ 
     int[] M_array_9 = { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }; 
     /* 10 */ 
     int[] M_array_10 = { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }; 
     /* 11 */ 
     int[] M_array_11 = { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }; 
     /* 12 */ 
     int[] M_array_12 = { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }; 
     /* 13 */ 
     int[] M_array_13 = { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }; 

     int[][] M = { M_array_0, M_array_1 }; 

    } 
} 

}如何避免初始化已经初始化的变量?

错误出现与所述段相关联:

int[][] M = { M_array_0, M_array_1 }; 

错误涉及M_array_0和M_array_1在上面。我不明白的是为什么我无法从上面创建一个多维数组。我应该使用那个代码? 我尝试:

string[][] M = { M_array_0, M_array_1 }; 
double[][] M = { M_array_0, M_array_1 }; 

错误读取:

甲字段初始不能引用非静态字段,方法或属性 'txt_program.txt.M_array_0' text_test \ PSF \首页\文件\ Visual Studio 2015 \ Projects \ text_test \ text_test \ text.cs 45活动

提前谢谢。

+1

你应该在[构造]设置这些变量(https://msdn.microsoft.com/en-us/library /k6sa6h87.aspx)。 – x13

+0

如果你使数组静态,这将工作。 – juharr

+0

我已经重新提出了这个问题,因为它看起来更像锯齿阵列创建而不是关于错误发生。 –

一种方式是写一个返回所需的阵列静态方法,并用它来分配领域:

public class txt 
{ 
    private int[][] M = createArray(); 

    private static int[][] createArray() 
    { 
     /* 0 */ 
     int[] M_array_0 = { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }; 
     /* 1 */ 
     int[] M_array_1 = { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }; 
     /* 2 */ 
     int[] M_array_2 = { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }; 
     /* 3 */ 

     // Etc 

     return new [] { M_array_0, M_array_1, M_array_2 /* etc */ }; 
    } 
} 

另外,您可以在线写像这样(但这是不够灵活) :

public class txt 
{ 
    private int[][] M = 
    { 
     new[] {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}, // M_array_0 
     new[] {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}, // M_array_1 
     new[] {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}, // M_array_3 

     // Etc 
    }; 
} 

因为你是初始化所有的阵列相同的大小,以及包含零,就可以缩短,为:

public class txt 
{ 
    int[][] M = 
    { 
     new int[14], // M_array_0 
     new int[14], // M_array_1 
     new int[14], // M_array_2 

     // Etc 
    }; 
} 

但是,我怀疑这只是您给出的一个示例,并且在实际代码中您将使用非零值。

还要注意的是,你可以声明M为只读:

private readonly int[][] M = ... 

,我建议您这样做,除非你需要创建类的实例后更改阵列本身(而不是内容)。

甲字段初始不能引用非静态字段,方法或 属性“字段”的实例字段不能被用于初始化的方法以外的其他 实例字段。如果你想初始化的方法外 变量,考虑执行类的构造函数

Reference

内初始化 也看到this一个描述性的答案。

我建议为了使用的LINQ产生阵列

int[][] M = Enumerable.Range(0, 13) // 13 rows 
    .Select(x => new int[13])   // each line contains 13 items 
    .ToArray();