简体   繁体   English

C#中局部变量和字段的数组声明和数组内存分配的差异

[英]difference in array declaration and array memory allocation for local variables and fields in C#

I have the question listed above. 我有上面列出的问题。 difference in array declaration and array memory allocation for local variables and fields in C# C#中局部变量和字段的数组声明和数组内存分配的差异

I am working on a WindowsForm application but I would like to declare some arrays. 我正在WindowsForm应用程序上工作,但我想声明一些数组。

In the public partial class Form1 : Form , I cannot declare an array and do the memory allocation separately ( if I do int[] numero = new int[5] it works ) but separately (in two steps) as shown in the code below it does not. 公共局部类Form1:Form中 ,我无法单独声明数组并单独进行内存分配( 如果我执行int [] numero = new int [5],则可以 ),但不能分开(分两步),如下面的代码所示它不是。

When I hoop over the variable numero VS labels it as a field . 当我将变量numero圈起来时,VS将其标记为字段 I did the same in the derivative class public Form1() and it works fine in one step (int[] numero = new int[4]) and in two steps as shown below in the code. 我在派生类公共Form1()中做了同样的事情,它一步一步就可以正常工作(int [] numero = new int [4]),并且可以按照下面的代码两步进行。 When I hoop over the variable casinumero , it labels it as a local variable . 当我将变量casinumero环绕时 ,它将其标记为局部变量 So the question is: 所以问题是:

WHY THE ARRAY DECLARATION AND MEMORY ALLOCATION CANNOT BE DONE IN ONE STEP FOR FIELDS AS IT IS FOR LOCAL VARIABLES? 为什么字段声明不能像现场变量那样一步一步地完成数组声明和内存分配?

public partial class Form1 : Form
    {
        public int [] numero;
        numero = new int[5];

        public Form1()
       {
            InitializeComponent();
            int[] casinumero;
            casinumero = new int[4];
       }
    }

This is declaration: 这是声明:

public int [] numero;

Below is instantiation. 下面是实例化。 Another way to word it: This is using the variable. 另一种表达方式: 使用变量。 . You are using the variable by assigning something to it or initializing it explicitly. 您通过向变量赋值或显式初始化来使用该变量。

numero = new int[5];

You cannot use a variable at the class level. 您不能在类级别使用变量。

From C# Specs: C#规范:

An instance variable of a class comes into existence when a new instance of that class is created, 创建该类的新实例时,该类的实例变量就存在了,

Therefore, you cannot use it before the instance is created. 因此,您不能在创建实例之前使用它。

You can declare it. 您可以声明它。 If you do not initialize it, it will have a default initial value. 如果不初始化,它将具有默认的初始值。 Depending on the type, the default values vary. 默认值取决于类型。 For reference type, as is the case with arrays, it will be initialized to null . 对于引用类型,与数组一样,它将被初始化为null This is called definite assignment and it is one of the C# rules. 这称为定值分配 ,它是C#规则之一。

You can do this, however (Please note inline comments): 但是,您可以执行此操作(请注意行内注释):

public class Example
{
    // Declared here and it will be defaulted to null since we never
    // initialized it explicitly.
    public int[] numero;

    // Or we can initialize it and declare it in one step:
    // public int[] numero = new int[5];

    public Example()
    {
        // Now we are in the constructor, we can use the variable and 
        // explicitly initialize it.
        numero = new int[5];

        int[] casinumero;
        casinumero = new int[4];
    }

    public void SomeMethod()
    {
        // Or we can use it in a method
        numero = new int[5];
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM