winform的后置代码,没有对变量message进行初始化赋值,怎么可以进行赋值运算了,这不是会引发编译错误吗

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace 对话框
{
public partial class Form2 : Form
{
 public string message; 

public Form2()
{
InitializeComponent();
}

private void Form2_Load(object sender, EventArgs e)
{
  label.Text = message; 

}


private void button1_Click(object sender, EventArgs e)
{

this.Close();
}


}
}

生成一个这样的窗体

(逐语句过程调试)监视窗口显示messagenullstring,这不是会引发编译错误吗,难道在 label.Text = message中的message跟public string message中的message变量时不同的,求解救

桃花长相依
浏览 314回答 5
5回答

慕娘9325324

没有初始化~

开满天机

因为你的message是public的,那么这个变量就存在外部赋值的可能。 在.NET中,变量未赋值的时候都有默认值(可以通过default(type)设置,字符串默认是null)。只是,.NET中,为了安全起见(.NET的设计中就有这个要求),所以,禁止对未赋值的变量直接进行使用。

holdtom

谢谢。我明白了
打开App,查看更多内容
随时随地看视频慕课网APP