为什么我不能在初始化我我的结构领域? [英] Why can't I initialize my fields in my structs?
本文介绍了为什么我不能在初始化我我的结构领域?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我很惊讶,我不能初始化我在结构领域,为什么会这样呢?这样的:
I am surprised that I can't initialize my fields in structs, why is it like that? Like:
struct MyStruct
{
private int a = 90;
}
但它是一个complie时错误。我不知道为什么这是一个问题吗?请给我讲解一下。
but it's a complie time error. I don't know why it's a problem? Please explain this to me.
推荐答案
究其原因,主要是性能。考虑下面的,
The reason is mainly performance. Consider the following,
var a = new MyStruct[1000];
如果C#允许在一个结构域的初始化则初始化将必须进行1000次,一次对于阵列中的每个元素。 C#想避免这种类型的内隐行为可能会在其他语言中找到。
If C# allowed initialization of fields in a struct then the initialization would have to be performed 1000 times, once for each element in the array. C# wanted to avoid such kinds of implicit behavior as might be found in other languages.
这篇关于为什么我不能在初始化我我的结构领域?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文