【问题标题】:avoid Value property for nullable value types?避免可空值类型的 Value 属性?
【发布时间】:2026-01-09 12:15:01
【问题描述】:

如果我有一个可以为 null 的值类型,我总是必须使用它的 Value 属性,即使在我检查了它是否为 null 之后也是如此。有没有一种简洁的方法?

public void Foo(SomeStruct? s)
{
    if (s != null)
    {
        DoIt(s.Value.x + s.Value.y + s.Value.z);
    }
}

显而易见的方法是定义一个新变量,这使得它影响的每个变量的代码更长,并且我发现这使得代码更难阅读:

    if (s != null)
    {
        var sv = s.Value;
        DoIt(sv.x + sv.y + sv.z);
    }

我想到的另一件事是模式匹配,但这有运行时类型检查的缺点:

    if (s is SomeStruct sv)
    {
        DoIt(sv.x + sv.y + sv.z);
    }

我是否忽略了某些东西(除了我可能应该首先避免空变量的事实)?

【问题讨论】:

    标签: c# struct pattern-matching nullable c#-8.0


    【解决方案1】:

    如果您可以使用 C# 8 - 您可以使用实际转换为 HasValue 的属性模式检查:

        if(s is {} sv)
        {
            Console.WriteLine(sv);
        }
    

    会被编译器变成这样的东西(sharplab):

        int num;
        if (s.HasValue)
        {
            valueOrDefault = s.GetValueOrDefault();
            num = 1;
        }
        else
        {
            num = 0;
        }
        if (num != 0)
        {
            Console.WriteLine(valueOrDefault);
        }
    

    【讨论】:

    • 您可能会记下您显示的代码的版本限制。我们很多人都在使用旧版本的编译器。升级Visual Studio很容易,升级公司构建系统需要更长的时间。
    • 虽然生成的代码乍一看看起来效率低下,但我检查了在发布配置中它确实生成了与我进行正常空检查时相同的代码。
    • 语法有点丑陋和不直观..但它正是我想要的:)
    【解决方案2】:

    if (s is SomeStruct sv)不会导致运行时类型检查。这只是一个直接的HasValue 检查

    例如见this on Sharplab

    int? x=5;
    if (x is int xv)
    {
        xv.ToString();
    }
    

    这编译成等价于:

    int? x=5;
    int xv;
    if (x.HasValue)
    {
        xv = x.GetValueOrDefault();
        xv.ToString();
    }
    

    请注意,GetValueOrDefault 已完全优化,不会检查 bool HasValue 标志。

    实际IL如下

            IL_0000: ldloca.s 0
            IL_0002: ldc.i4.5
            IL_0003: call instance void valuetype [System.Private.CoreLib]System.Nullable`1<int32>::.ctor(!0)
            IL_0008: ldloca.s 0
            IL_000a: call instance bool valuetype [System.Private.CoreLib]System.Nullable`1<int32>::get_HasValue()
            IL_000f: brfalse.s IL_0021
    
            IL_0011: ldloca.s 0
            IL_0013: call instance !0 valuetype [System.Private.CoreLib]System.Nullable`1<int32>::GetValueOrDefault()
            IL_0018: stloc.1
            IL_0019: ldloca.s 1
            IL_001b: call instance string [System.Private.CoreLib]System.Int32::ToString()
            IL_0020: pop
    

    【讨论】:

    • 好的,对于值类型,不需要类型检查。
    最近更新 更多