gpt4 book ai didi

c# - 在新的非空测试上使用 ref 原则

转载 作者:行者123 更新时间:2023-11-30 22:52:23 26 4
gpt4 key购买 nike

我不知道它到底叫什么,但现在我将它称为“非空测试”。在 C# 8 中有一个新的行为允许测试一个对象是否不为空,例如:

Foo foo = new Foo();

if(foo is { })
{
//foo is not null
}

您还可以从此对象中提取属性:

public class Foo
{
public int Bar { get; set; }
}

Foo foo = new Foo();

if(foo is { Bar: var bar })
{
//Foo is not null and bar contains the value of the property of the foo instance
}

到目前为止一切顺利,但我想它类似于这样的东西:

public bool GetBar(Foo foo, out int bar)
{
if(foo is null)
{
return false;
}

bar = foo.Bar;
return true;
}

这将被用作这样的东西:

Foo foo = new Foo();

if(GetBar(foo, out var bar))
{
//Foo is not null and bar contains the value of the property of the foo instance
}

现在我的实际问题是:有什么方法可以使用 ref 的行为吗?看起来像这样:

if(foo is { Bar: ref var bar })
{
//Foo is not null and bar contains the value of the property of the foo instance
}

如果这不存在我会理解,因为 out ref 也不存在。那么,您有什么方法可以做到这一点,或者有什么反对它的地方吗?

最佳答案

您使用的模式是来自 C#8 pattern matching feature属性模式 .

The property pattern enables you to match on properties of the object examined.

如果我没答错你的问题,你不仅希望获得属性值,还希望能够更改匹配对象中的该值。

if (foo is { Bar: ref var bar })
{
bar = 42; // now foo.Bar is 42 too
}

为了使 ref 语义起作用,它必须得到语言和编译器的支持。

但是,C# 不允许您将 ref 与属性一起使用 - 这对于任何 C# 版本都是如此。原因很明显:您的 get-set 属性 Bar(与任何其他 C# 属性一样)将被编译为两种方法:get_Barset_Bar。所以实际上你有一个方法,而不是一个值 - 因此,你不能 ref 一个方法返回的值(好吧,有 ref return,但是那些不'适用于属性(property)获取者)。

关于c# - 在新的非空测试上使用 ref 原则,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58198039/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com