c# – 未初始化的struct成员是否保证有值?

如果使用初始化列表创建结构,那么您省略的成员是否会获得已知的认值?
public struct Testing
{
    public int i;
    public double d;
    public string s;
}

Testing test = new Testing { s="hello" };

我发现微软的一个链接暗示了它,但没有明确说明:Default Values Table (C# Reference).

一个小的测试程序显示它不会生成编译器错误并产生预期的结果.我知道最好不要依靠简单的测试来保证. http://ideone.com/nqFBIZ

解决方法

是的,它们包含认值(T),其中T是值的类型.
对象引用将为null.

摘抄:

As described in Section 5.2,several kinds of variables are
automatically initialized to their default value when they are
created. For variables of class types and other reference types,this
default value is null. However,since structs are value types that
cannot be null,the default value of a struct is the value produced by
setting all value type fields to their default value and all reference
type fields to null.

取自这里:

http://msdn.microsoft.com/en-us/library/aa664475(v=vs.71).aspx

我记得在研究MOC时,他们明确说明了这一点.这是该语言的保证功能.

相关文章

在要实现单例模式的类当中添加如下代码:实例化的时候:frmC...
1、如果制作圆角窗体,窗体先继承DOTNETBAR的:public parti...
根据网上资料,自己很粗略的实现了一个winform搜索提示,但是...
近期在做DSOFramer这个控件,打算自己弄一个自定义控件来封装...
今天玩了一把WMI,查询了一下电脑的硬件信息,感觉很多代码都...
最近在研究WinWordControl这个控件,因为上级要求在系统里,...