从历史上看,我在分配之前初始化了成员变量,如下所示:
private bool _myBool = false;
private int _myInt = int.MinValue;
private string _myString = String.Empty;
最近我发现自己像这样初始化它们(通常使用原语):
private DateTime _myDateTime = default(DateTime);
private bool _myBool = default(bool);
//etc
这是好事还是坏事?怎么会?
最佳答案
我真的不认为这样做有什么意义,因为您正在添加实际上什么都不做的额外代码。
如果您尝试默认(类),同样适用,您将得到 null。
关于c# - 在初始化成员变量时使用 default 关键字是不是一个坏主意?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19302801/