C#定义空数组的方法

how to define null array in c#

我想做的是用文本框中的数据编辑数据库文本文件,如果文本框为空或空白数据不会被更改。数据库中的读写方法工作正常我只需要将字符串数组中的数据传递给我的方法,这样它就会被写成逗号分隔的文件。我也使用了 IsNullOrWhitespace 但仍然收到此异常错误。

private void AdminEditsNewStudentButton_Click(object sender, EventArgs e)
    {
       
        string[] data= null;
       
        if (this.StudentIDTextBox.Text != null || Directory.Exists(@"database\Students\" + this.StudentIDTextBox.Text))
        {
            Admin obj = new Admin();
            data[0] = string.IsNullOrWhiteSpace(this.NewFirstNameTextBox.Text) ? " " : this.NewFirstNameTextBox.Text;
            data[1] = string.IsNullOrWhiteSpace(this.NewLastNameTextBox.Text) ? " " : this.NewLastNameTextBox.Text;
            data[2] = string.IsNullOrWhiteSpace(this.NewPhoneTextBox.Text) ? " " : this.NewPhoneTextBox.Text;
            data[3] = string.IsNullOrWhiteSpace(this.NewEmailTextBox.Text) ? " " : this.NewEmailTextBox.Text;
            data[4] = string.IsNullOrWhiteSpace(this.NewSuiteTextBox.Text) ? " " : this.NewSuiteTextBox.Text;
            data[5] = string.IsNullOrWhiteSpace(this.NewStreetNumTextBox.Text) ? " " : this.NewStreetNumTextBox.Text;
            data[6] = string.IsNullOrWhiteSpace(this.NewPostalCodeTextBox.Text) ? " " : this.NewPostalCodeTextBox.Text;
            obj.AdminEditsStudent(this.StudentIDTextBox.Text,data);
            MessageBox.Show("Student's info was modified", "Successful Write");
            this.Close();
        }
        else
        {
            MessageBox.Show("Student ID not found!");

        }
       
    }

enter image description here

初始化你的字符串数组data

替换:

string[] data= null; 

string[] data = new string[7];