简体   繁体   English

C# 可为空的引用类型:此函数能否在不覆盖空检查的情况下接受具有可为空值和不可为空值的字典?

[英]C# Nullable Reference Types: can this function accept a dictionary with both nullable and non-nullable value without overriding null checking?

I am trying to write a function that will convert a Dictionary's TryGet into a "lookup" object in C# 9.0.我正在尝试编写一个函数,将字典的TryGet转换为 C# 9.0 中的“查找”对象。 The (simplified version of) code I have so far looks like this:到目前为止,我拥有的(简化版)代码如下所示:

class C1
{
    [Test]
    public void METHOD()
    {
        var lookupValue1 = Lookup(new Dictionary<string, string?>(), "a");
        var lookupValue2 = Lookup(new Dictionary<string, string>(), "a");
    }

    public LookupValue<TV> Lookup<TK, TV>(Dictionary<TK, TV?> d, TK key) 
        where TK : notnull
        where TV : notnull
    {
        if (d.TryGetValue(key, out var result))
        {
            return new LookupValue<TV>(result);
        }
        else
        {
            return new LookupValue<TV>(default);
        }
    }

    public record LookupValue<T>(T? Value) where T : notnull;
}

But this code won't compile, specifically, this line: var lookupValue2 = Lookup(new Dictionary<string, string>(), "a");但是这段代码不会编译,特别是这一行: var lookupValue2 = Lookup(new Dictionary<string, string>(), "a");

I know I can override the null reference checking by using a !我知道我可以通过使用! operator: var lookupValue2 = Lookup(new Dictionary<string, string>()!, "a");运算符: var lookupValue2 = Lookup(new Dictionary<string, string>()!, "a"); but this is supposed to be a public API and I son't want the users of the API to have to do this.但这应该是一个公共 API,我不希望 API 的用户必须这样做。 Also, I know I can create two methods named differently in the same class or methods named the same in separate classes, but I'd like to avoid that if possible.另外,我知道我可以在同一个类中创建两个名称不同的方法,或者在不同的类中创建两个名称相同的方法,但如果可能的话,我想避免这种情况。

Is there any way to make this Lookup function accept a dictionary with a nullable value type as well as one with non-nullable value type and preserve the signature of the LookupValue record?有什么方法可以让这个Lookup函数接受一个具有可为空值类型的字典和一个具有不可空值类型的字典并保留LookupValue记录的签名?

You've written a function which demands a Dictionary whose values are nullable.您已经编写了一个函数,它需要一个值可以为空的 Dictionary。 In other words, the value for a key might be null, and it's allowed to set a null value for a key.换句话说,键的值可能为空,并且允许为键设置空值。 So there's no reasonable way for such a function to accept a Dictionary whose values are non-nullable.因此,这样的函数没有合理的方法来接受值不可为空的 Dictionary。

Instead, you could consider making the function accept a dictionary with any kind of value type.相反,您可以考虑使函数接受具有任何类型值类型的字典。 You can do this by removing the notnull constraint on TV and removing the nullable annotation in the type argument to the dictionary type.您可以通过删除TV上的notnull约束并删除字典类型的类型参数中的可为空注释来实现此目的。

SharpLab 夏普实验室

class C1
{
    [Test]
    public void METHOD()
    {
        var lookupValue1 = Lookup(new Dictionary<string, string?>(), "a");
        var lookupValue2 = Lookup(new Dictionary<string, string>(), "a");
    }

    public LookupValue<TV?> Lookup<TK, TV>(Dictionary<TK, TV> d, TK key) 
        where TK : notnull
    {
        if (d.TryGetValue(key, out var result))
        {
            return new LookupValue<TV?>(result);
        }
        else
        {
            return new LookupValue<TV?>(default);
        }
    }

    public record LookupValue<T>(T Value);
}

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

相关问题 C#8中Non-Nullable引用类型的默认值是多少? - What is the default value of Non-Nullable reference types in C# 8? C# 8 中不可为空的引用类型在运行时可以为空吗? - Can a non-nullable reference type in C# 8 be null in runtime? C# 8.0 不可为空的引用类型和选项模式 - C# 8.0 non-nullable reference types and options pattern C# 9.0 记录 - 不可为空的引用类型和构造函数 - C# 9.0 records - non-nullable reference types and constructor C#8 可为空和不可为空的引用类型 - 仅当输入可为空时才可空输出 - C#8 nullable and non-nullable reference types - nullable output only if input is nullable 在 C# 中创建不可为 Null 的类型 - Create Non-Nullable Types in C# 为什么我会收到这些奇怪的 C# 8 “可空引用类型”警告:在退出构造函数之前,不可空字段必须包含非空值。? - Why am I getting these strange C# 8 “nullable reference types” warnings: Non-nullable field must contain a non-null value before exiting constructor.? 不可为空的引用类型的默认值 VS 不可为空的值类型的默认值 - Non-nullable reference types' default values VS non-nullable value types' default values 如何在C#中接近不可为空的引用类型? - How can I get close to non-nullable reference types in C# today? 带有不可空参数的复合空检查 - Compounded Null Checking With Non-Nullable Parameter
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM