c# 中为什么有的用new 而又得不用

来源:百度知道 编辑:UC知道 时间:2024/06/09 02:10:40
private DitchControl _CurrentDitchControl;
public DitchControlCollection _AllControls = new DitchControlCollection();
比如这个 是什么意思啊 我是新手

private DitchControl _CurrentDitchControl;
声明了一个类型为DitchControl的私有变量但是没有实例化(静态类不需要),这个变量不能直接赋值必须在使用的地方实例化一下也就是 _CurrentDitchControl = new _CurrentDitchControl();

public DitchControlCollection _AllControls = new DitchControlCollection(); 声明了一个类型为DitchControlCollection的公有变量并实例化了可以直接使用。

静态类不需要不需要实例化就可以直接使用其成员

private DitchControl _CurrentDitchControl;
public DitchControlCollection _AllControls = new DitchControlCollection();
这两个都是在实例一个类,但是第一个是没有初始化,而第二个类对象已经初始化完成,不知道你对初始化有没有认识~~
一般这样写的话,都是第一个类在后面初始化,因为它是私有的,也就是只在该类中可见,我建议你拿本书看看~~

静态的不要用new,非静态的就要用new