C# 从构造函数访问更新的数组

C# 从构造函数访问更新的数组,c#,arrays,custom-lists,C#,Arrays,Custom Lists,我有一个参数化构造函数和一个默认构造函数。它们都创建了一个x长度的新对象数组,但是,当我尝试在Add方法中访问该数组时,它返回值“null”。我无法在字段中初始化数组,因为我不知道用户希望它的大小,但我不知道以后如何访问代码中的“更新”数组。我在代码行:if(count>data.Length)上得到一个NullReferenceException(),因为数据的值为null class CustomList { private int count; private String

我有一个参数化构造函数和一个默认构造函数。它们都创建了一个x长度的新对象数组,但是,当我尝试在Add方法中访问该数组时,它返回值“null”。我无法在字段中初始化数组,因为我不知道用户希望它的大小,但我不知道以后如何访问代码中的“更新”数组。我在代码行:if(count>data.Length)上得到一个NullReferenceException(),因为数据的值为null

class CustomList
{
    private int count;
    private String[] data;

    public int Count
    {
        get { return count; }
    }
    public CustomList(int arrayNum)
    {
        String[] data = new String[arrayNum];
    }
    public CustomList(): this(4)
    {
    }

    public void Add (String item)
    {
        if (count > data.Length)
        {
            String[] temp = new String[count * 2];
            for (int i = 0; i < data.Length; i++)
            {
                temp[i] = data[i];
            }
            data = temp;
        }
        data[count] = item;
        count++;
    }
类自定义列表
{
私人整数计数;
私有字符串[]数据;
公共整数计数
{
获取{返回计数;}
}
公共客户列表(int arrayNum)
{
字符串[]数据=新字符串[arrayNum];
}
public CustomList():此(4)
{
}
公共无效添加(字符串项)
{
如果(计数>数据长度)
{
字符串[]临时=新字符串[计数*2];
for(int i=0;i
更改此选项:

public CustomList(int arrayNum)
{
    String[] data = new String[arrayNum];
}
为此:

public CustomList(int arrayNum)
{
    data = new String[arrayNum];
}
您意外地在正在分配给的构造函数中创建了一个局部变量,而不是要分配给的字段。

更改此设置:

public CustomList(int arrayNum)
{
    String[] data = new String[arrayNum];
}
为此:

public CustomList(int arrayNum)
{
    data = new String[arrayNum];
}
您意外地在正在分配给的构造函数中创建了一个局部变量,而不是要分配给的字段。

更改代码

构造函数中的数据对象是局部变量。 并且您没有初始化实例数据对象

class CustomList
{
private int count;
private String[] data;

public int Count
{
    get { return count; }
}
public CustomList(int arrayNum)
{
     data = new String[arrayNum];
}
public CustomList(): this(4)
{
}

public void Add (String item)
{
    if (count > data.Length)
    {
        String[] temp = new String[count * 2];
        for (int i = 0; i < data.Length; i++)
        {
            temp[i] = data[i];
        }
        data = temp;
    }
    data[count] = item;
    count++;
}
类自定义列表
{
私人整数计数;
私有字符串[]数据;
公共整数计数
{
获取{返回计数;}
}
公共客户列表(int arrayNum)
{
数据=新字符串[arrayNum];
}
public CustomList():此(4)
{
}
公共无效添加(字符串项)
{
如果(计数>数据长度)
{
字符串[]临时=新字符串[计数*2];
for(int i=0;i
更改您的代码

构造函数中的数据对象是局部变量。 并且您没有初始化实例数据对象

class CustomList
{
private int count;
private String[] data;

public int Count
{
    get { return count; }
}
public CustomList(int arrayNum)
{
     data = new String[arrayNum];
}
public CustomList(): this(4)
{
}

public void Add (String item)
{
    if (count > data.Length)
    {
        String[] temp = new String[count * 2];
        for (int i = 0; i < data.Length; i++)
        {
            temp[i] = data[i];
        }
        data = temp;
    }
    data[count] = item;
    count++;
}
类自定义列表
{
私人整数计数;
私有字符串[]数据;
公共整数计数
{
获取{返回计数;}
}
公共客户列表(int arrayNum)
{
数据=新字符串[arrayNum];
}
public CustomList():此(4)
{
}
公共无效添加(字符串项)
{
如果(计数>数据长度)
{
字符串[]临时=新字符串[计数*2];
for(int i=0;i