繁体   English   中英

来自Nullable结构的方法.Value的Null条件运算符

[英]Null Conditional Operator with method .Value from Nullable structure

我正在尝试学习null条件运算符。 从我学到的书中,例如,如果我将string []数组传递给方法,我可以代替使用条件范围,使用空条件运算符来检查传递的string []数组是否为空,因为例如在方法中我会想要使用.Length属性,如果我向它传递null,它将导致运行时错误。

我理解的所有这些,但是当我想使用来自通用Nullable结构的nullable int和.Value属性时,它已经在我身上抛出了编译器错误。

我想问题就是,我不明白这是什么'?' 在后缀变量之后做的是null。 我认为它只是跳过它之后的属性或方法,但也许它传递null并且只是隐藏异常。 我真的不知道,所以我想更深入地问这个Null条件运算符到底是什么,为什么这个可以为空的int示例导致错误。

代码有错误:

 class Program
{
    static void Main(string[] args)
    {
        string text = null;
        Console.WriteLine("Variable contains: {0}", text?.Length); //All OK

        int? nullableInt = null;
        Console.WriteLine("Variable contains: {0}", nullableInt?.Value); //Compiler error

        Console.ReadLine();
    }
}

编辑:CompilerError消息:

'int'不包含'Value'的定义,并且没有扩展方法'Value'可以找到接受类型'int'的第一个参数(你是否缺少using指令或汇编引用?)

Edit2:这篇文章是重复的: null条件运算符不能使用可空类型? 但是我无法理解这篇文章中的问题,但我设法得到它,感谢来自@adjan https://stackoverflow.com/a/44613674/7477611的帖子,他解释得很好,但在我的尝试中学习它,我为一个稍后会读到这个的人做了一个更好的解释,也许是为了和我一样的人。

如果使用'?' 可操作类型的运算符,例如int?.Member(Member - some method,property ...),如果变量为null,则跳过成员,但如果它有例如5的值,则运算符'?' return variable.GetValueOrDefault(),它只返回int值。 (返回Nullable的底层类型),这几乎是@adjan所说的。 但我将在这里介绍代码,为我解释了一切:

 static void Main(string[] args)
    {
        int? number = null;

        variable?.Member

        // in fact operator '?' is doing folowing:
        if (number.HasValue)
        {
            Console.WriteLine(number.GetValueOrDefault().GetType()); //System.Int32
        }
        else
        {
            Console.WriteLine(number); //null
        }
    }

正如您所看到的,number.GetValueOrDefault()返回System.Int32数据类型,如果您想使用.Value属性,则它必须是Nullable类型,而System.Int32不是可空类型,因此它无法访问.Value成员。 这就是它导致错误的原因。

希望它有所帮助:)谢谢你的答案。 :)

variable?.Member类型的值类型T上的variable?.Member实际上转换为大致类似的函数

if (variable.HasValue)
    return variable.GetValueOrDefault().Member;
else
    return null;

variable.GetValueOrDefault()属于T类型,而不是Nullable<T> 只有Nullable<T>有一个成员Value (除非它是你自己的包含成员Value的结构,但是例如你的代码中的int不是)。

nullableInt?.Something

语法糖是:

nullableInt is null ? null : nullableInt.Value.Something

因此,没有必要将.Value放在那里。 因为如果你这样做,你实际上是在做nullableInt.Value.Value

要由什么等效代码nullableInt?.ValuenullableInt (本身)。

也可以看看:

null条件运算符不使用可空类型?

在您的代码中,您尝试访问nullableInt的成员Value ,但由于此成员不存在,因此无法编译此代码。

?. 运营商是一个访问者。 它返回null在运行时,如果访问的成员存在,没有设置或设置为null 相比之下,标准访问器(。)将在此方案中引发Null reference exception

尝试用像nullableInt ?? 0这样的代码替换你的代码nullableInt ?? 0 nullableInt ?? 0 ?? 是null-coalescing运算符,将用空字符串替换null。

可空值类型是特殊类型。 当你使用nullableInt? ,您的结果实际上是int类型,它没有Value属性。

空条件运算符仅对引用类型和string有意义,作为不可变引用类型的特例。

以下代码说明了该问题:

string[] temp = null;
int cc = -1;
if (temp == null)
{
    cc = 0;
}
else
{
    cc = temp.Count();
}

空数组(count()= 0)和数组为null之间存在差异。

您的代码变为:

Console.WriteLine("Variable contains: {0}", args?.Count() ?? 0); //All OK
Console.WriteLine("Variable contains: {0}", nullableInt?.ToString() ?? "woops"); //now ok

暂无
暂无

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

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