简体   繁体   English

C# 可空类型和 Value 属性

[英]C# Nullable Types and the Value property

I'm a little unclear on when/if the Value property on nullable types must be used when getting the value contained in a nullable type.我有点不清楚何时/是否在获取可空类型中包含的值时必须使用可空类型的Value属性。 Consider the following example:考虑以下示例:

int? x = 10;

Console.WriteLine("The value of 'x' is " + x.Value);
Console.WriteLine("The value of 'x' is " + x);

Both of these return the same value (10).这两者都返回相同的值 (10)。

However, if I initially set x to null , the first Console.WriteLine throws an exception and the second one does not.但是,如果我最初将x设置为null ,第一个Console.WriteLine会引发异常,而第二个则不会。

So, my question is this.所以,我的问题是这个。 What is the point of using the Value property?使用Value属性有什么意义? It doesn't appear to be needed to get the actual value (even if it's null ) and will throw an exception if the value is indeed null .似乎不需要获取实际值(即使它是null ),如果值确实是null ,则会抛出异常。

It is needed usually - just not in your particular case.通常需要-只是没有你的具体情况。 The type of x is Nullable<int> , not int - and there's no implicit conversion from Nullable<T> to T . x的类型是Nullable<int> ,而不是int - 并且没有从Nullable<T>T隐式转换。

Let's look at what's happening in your example though.不过,让我们看看您的示例中发生了什么。 Your final line is being converted into:您的最后一行正在转换为:

Console.WriteLine(string.Concat("The value of 'x' is ", x));

That's boxing x , which will result in either a boxed int or a null reference... both of which are handled by string.Concat .这是拳击x ,这将导致无论是盒装int空引用...这两者都是通过处理string.Concat

When you're not converting to a string via string concatenation, eg if you wanted:当您通过字符串连接转换为字符串时,例如,如果您想要:

int nonNullable = x.Value;

then you do have to use the Value property - or an explicit cast, or possibly a null coalescing operator, or a call to GetValueOrDefault :那么你必须使用Value属性-或有明确的转换,也可能是空合并运算符,或一个电话GetValueOrDefault

int nonNullable = (int) x;
int alternative = x ?? 20;
int andAnother = x.GetValueOrDefault(20);

x and x.Value have different types. xx.Value有不同的类型。

    static void Meth(int i)
    {
        int? x = 5;
        Meth(x);
        Meth(x.Value);
    }

Second line doesn't compile.第二行不编译。

So reason of using x.Value is obvious - you need to call it when you need the actual value.所以使用x.Value原因很明显——当你需要实际值时需要调用它。 And NullReferenceException is reasonable, because null doen't correspond to value of value type.NullReferenceException是合理的,因为null不对应值类型的值。

You should access Value property if HasValue property returns true, otherwise you will get an exception.如果 HasValue 属性返回 true,您应该访问 Value 属性,否则您将收到异常。 Your example works because Nullable has overloaded ToString() method and C# compiler supports shortcuts for this type.您的示例有效,因为 Nullable 重载了 ToString() 方法,并且 C# 编译器支持此类型的快捷方式。

when x is null x.Value will throw a System.InvalidOperationException exception (no matter what is the type of x)x为空时x.Value会抛出 System.InvalidOperationException 异常(不管 x 的类型是什么)

string + null = string : no exception string + null = string :也不例外

If you want to use .value for a nullable int.如果您想将 .value 用于可为空的 int。 you can check the value like this你可以像这样检查值

int? x = null;
int valXX = x.HasValue ? x.Value : 0;
Console.WriteLine("The value of 'x' is ->" + x + "<-");
Console.WriteLine("The value of 'valXX' is ->" + valXX + "<-");

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM