8可为空的引用类型的TryGetValue模式

8可为空的引用类型的TryGetValue模式

本文介绍了具有C#8可为空的引用类型的TryGetValue模式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在将一些代码移植到C#中以启用可为空的引用类型,并且我在代码中遇到了一些使用 TryGetValue 模式的函数.

I'm playing with porting some code to C# to enable nullable reference types, and I've encountered some functions in our code that use the TryGetValue pattern.

就是这样:

public bool TryGetSession(string key, out Session session) {
    session = null; // assign default
    // code which looks for a session based on the key, etc
    // return true or false if we found the session key
}

我们要在此处表达的模式是如果返回值为true,则 session 为非null.如果为false,则甚至不要尝试查看会话,这是垃圾.

The pattern which we're trying to express here is "if the return value is true, then session is non-null. If false, then don't even attempt to look at the session, it's garbage.

问题是我现在收到有关 session = null 的警告,但我不得不将某些东西放在其中,作为 out 参数必须由该函数填充.

The problem is I now get a warning on session = null, but I'm forced to put something there as out parameters MUST be populated by the function.

这里有个不错的答案吗?我的想法:

Is there a good answer here? My thoughts:

我可以放弃 TryGet 模式并包含可为空的引用类型(这是Swift等其他语言似乎所做的事情),例如

I could drop the TryGet pattern and embrace the nullable reference types (This is what other languages like Swift seem to do) e.g.

Session? GetSession(string key);

或者,我可以使用占位符值来实现我的非空"承诺

Or, I could live up to my "non-null" promise using a placeholder value e.g.

public bool TryGetSession(string key, out Session session) {
    session = new InvalidSession(); // assign default
    ...
}

还有其他选择吗?

推荐答案

如果像我这样迟到了,那么事实证明.NET团队通过诸如 System.Diagnostics.CodeAnalysis 空间中的MaybeNullWhen(returnValue:true),您可以将其用于try模式.

If you're arriving at this a little late, like me, it turns out the .NET team addressed it through a bunch of parameter attributes like MaybeNullWhen(returnValue: true) in the System.Diagnostics.CodeAnalysis space which you can use for the try pattern.

返回快速样式的可为空的引用类型效果很好,但是使用try模式可以将诸如错误之类的内容作为附加的out参数返回.

Returning a swift-style nullable reference type works well, but the try pattern lets you return things like errors as additional out parameters.

public bool TryGetSession(string key, [NotNullWhen(returnValue: true)] out Session? session, [NotNullWhen(returnValue: false)] out string? error)
{
  // etc
}


// This is okay:
if(TryGetSession("cheese", out var session, out var error))
{
  var time = session.Time;
}

// But this is not:
_ = TryGetSession("cheese", out var session, out var error);
var time = session.Time;
// "CS8602: Dereference of a potentially null reference"

更多详细信息:

这篇关于具有C#8可为空的引用类型的TryGetValue模式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-02 00:57