在.NET中,默认字段的值是什么? [英] In .NET what does default the values of fields?

查看:118
本文介绍了在.NET中,默认字段的值是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当您创建一个类,其中某些字段没有开发人员明确指定的任何值时,它们默认为默认值(对于Int32,它为零)。所以我的问题是他们如何违约? C#编译器在编译过程中是否在幕后做了些什么?或者也许是运行时间?



我尝试过:



我正在努力了解会发生什么,因为我很想知道事情是如何运作的。

解决方案

Google是你的朋友:



c#对象默认值 - Google搜索 [ ^ ]


除了John在解决方案1中提出的建议外,还请阅读.NET框架上下文中的值类型和引用类型。在上下文中,如果开发人员省略了初始化步骤并且仅执行声明,则分配默认值 - 这是显而易见的。我不是专家,但我认为这种行为源于C / C ++,其中,0表示错误,空指针(在很久以前)是零值。因此,要确保默认情况下的所有内容都相同。像这样,



  int  a =  0 ; 

if (a){
// 这不应该执行。
} else {
// 应该执行
}



在这里测试一下,< a href =http://cpp.sh/3hj3p> C ++ Shell [ ^ ]



但是,由于C#是托管语言,它不会让那些块并且总是需要一个表达式,如果操作数是任何东西,则解析为布尔值布尔值以外的。



我的意思是说,每个物体都会有东西。如果对象是引用类型,那么它将是 null ;这是引用类型的默认值,如 string



哦,C#也支持默认值分配。像这样,

  int  a = 默认 int ); 

// 相同
int a = new int ();
int a = 0 ;
int a;



这些只是个人选择和偏好。 :-)



默认值表(C#参考)| Microsoft Docs [ ^ ]

值类型和引用类型| Microsoft Docs [ ^ ]



哦,是的,编译器可以完成所有操作这个 - 如果你不想真正深入了解CLR并查看C#的IL。 :笑:


When you create a class with some fields that don't have any value explicitly specified by the developer they are defaulted to their default values (for Int32 it's zero). So my question is how do they get defaulted? Does the C# compiler do something behind the scenes in the process of compiling? Or maybe it's the runtime?

What I have tried:

I am trying to understand what happens as I am curious to know how the things work under the hood.

解决方案

Google is your friend:

c# object defaults - Google Search[^]


Apart from what John suggested in Solution 1, please also read about value-types and the reference-types in .NET framework context. In the context, if the developer leaves out the initialization step and only performs declaration, the default value is assigned — which is obvious. I am not an expert, but I think this behavior has the roots in C/C++, where, 0 means false and a null pointer was (in very old days) a zero value. So, to make sure everything in the default is same. Like this,

int a = 0;

if(a) {
   // This should not be executed.
} else {
   // Should execute
}


Test this one here, C++ Shell[^]

However, since C# is managed language, it does not let those blocks and always requires an expression that resolves to boolean value if the operand is anything other than boolean.

What I mean to say is, that every object would have something. If the object is reference-type, then it would be null; which is the default value for reference-types like string.

Oh, and C# also supports a default value assignation, too. Like this,

int a = default(int);

// Same as 
int a = new int();
int a = 0;
int a;


These are merely personal choices and preferences. :-)

Default values table (C# Reference) | Microsoft Docs[^]
Value Types and Reference Types | Microsoft Docs[^]

Oh, and yeah, it is the compiler that does all this — if you do not want to really dig deeper inside the CLR and see the IL of the C#. :laugh:


这篇关于在.NET中,默认字段的值是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆