使用 null 初始化的 C# 值类型

发布于 2024-10-04 10:04:08 字数 533 浏览 7 评论 0原文

在 C# 3.0 中可以将 null 赋给 int 吗? type(在CLR中int?是一个结构体):

int? a = null;

但是当你定义一个自定义结构体时:

struct MyStruct
{

}

在这段代码的编译过程中出现错误:

MyStruct a = null;

错误如下:

Cannot convert null to 'Example.MyStruct' because it is a non-nullable value type

而int?是 CLR 中的一个结构体,我们可以为其分配 null ,这在某种程度上是违反直觉的。 我认为 null 被隐式转换或装箱为某个 int ?表示空值的值。是如何精确完成的?如何以可以执行以下行的方式扩展 MyStruct:

MyStruct a = null;

In C# 3.0 you can assign null to int? type (in CLR int? is a struct):

int? a = null;

but when you define a custom struct:

struct MyStruct
{

}

there's an error during the compilation of this code:

MyStruct a = null;

The error is as follows:

Cannot convert null to 'Example.MyStruct' because it is a non-nullable value type

While int? is a struct in CLR it is somehow counterintuitive that we can assign null to it.
I suppose that null is implicitly casted or boxed to a certian int? value that represents the null value. How is it done precisely? How can I extend MyStruct in such a way that it would be possible to execute the line:

MyStruct a = null;

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

风轻花落早 2024-10-11 10:04:08

这是因为 int? 实际上是简写 Nullable。您也可以对您的类型执行相同的操作:

MyStruct? a = null;

null 只能隐式转换为 nullable 类型,这意味着任何引用类型或任何可为 null 的值类型 - 后者对于某些 T 意味着 Nullable

请注意,这种从 null 转换的能力实际上是一种语言功能 - 编译器将其转换为:

int? a = null;

它们

int? a = new int?();

的含义相同 - 基本上是可空值的“null”值值类型是 HasValue 为 false 的值,默认情况下为 false。它与空引用不同 - 尽管像这样装箱空值将导致空引用。

(这是跨越语言、库和 CLR 边界的功能的一个示例。库部分非常简单,CLR 部分仅与装箱和拆箱有关 - 但语言中的提升运算符等方面有很多内容.)

It's because int? is actually shorthand Nullable<int>. You can do the same thing for your type too:

MyStruct? a = null;

null can only be implicitly converted to a nullable type, which means any reference type or any nullable value type - where the latter means Nullable<T> for some T.

Note that this ability to conver from null is really a language feature - the compiler is converting this:

int? a = null;

into

int? a = new int?();

They mean the same thing - basically a "null" value for a nullable value type is a value where HasValue is false, which it will be by default. It's not the same as a null reference - although boxing a null value like that will result in a null reference.

(This is one example of a feature which crosses language, library and CLR boundaries. The library part is quite simple, the CLR part is only to do with boxing and unboxing - but there's quite a lot in terms of lifted operators etc in the language.)

情域 2024-10-11 10:04:08

结构体是 C# 中的值类型,而不是引用类型,因此它们不能为 null。

MyStruct? = null;

会起作用,但请记住结构是不可变的,例如:

public struct Point 
{
   public int x, y;

   public Point(int p1, int p2) 
   {
       x = p1;
       y = p2;    
   }
}

void Main()
{

    //create a point
    var point1 = new Point(10, 10); 

    //assign point2 to point, point2
    var point2 = point1; 

    //change the value of point2.x
    point2.x = 20; 

    //you will notice that point1.x does not change
    //this is because point2 is set by value, not reference.
    Console.WriteLine(point1.x);
    Console.WriteLine(point2.x);

}

Structs are value types in C#, not reference types, so they can't be null.

MyStruct? = null;

will work, but remember that structs are immutable, as a example:

public struct Point 
{
   public int x, y;

   public Point(int p1, int p2) 
   {
       x = p1;
       y = p2;    
   }
}

void Main()
{

    //create a point
    var point1 = new Point(10, 10); 

    //assign point2 to point, point2
    var point2 = point1; 

    //change the value of point2.x
    point2.x = 20; 

    //you will notice that point1.x does not change
    //this is because point2 is set by value, not reference.
    Console.WriteLine(point1.x);
    Console.WriteLine(point2.x);

}
诗化ㄋ丶相逢 2024-10-11 10:04:08

只需添加即可,可为 null 的类型可以表示其基础值类型的正常值范围,再加上一个附加的 null 值。

在表示法中,就像 int 实际上是 Int32,而不是任何 int 一样,?符号表示可为空。
在本例中可以为空。

http://msdn.microsoft.com/en-us /library/1t3y8s4s%28VS.80%29.aspx

just adding, a nullable type can represent the normal range of values for its underlying value type, plus an additional null value.

In notation, just as int is actually Int32, not any int, the ? sign means a nullable.
Nullable, in this case.

http://msdn.microsoft.com/en-us/library/1t3y8s4s%28VS.80%29.aspx

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文