[英]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
约束并删除字典类型的类型参数中的可为空注释来实现此目的。
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.