@bool = !@bool; public static void Toggl-6ren">
gpt4 book ai didi

c# - 谈论 C# 7.2 引用扩展方法时, "this ref"和 "ref this"有什么区别?

转载 作者:行者123 更新时间:2023-11-30 14:44:46 26 4
gpt4 key购买 nike

考虑以下扩展方法:

public static void Toggle(this ref bool @bool) => @bool = !@bool;

public static void Toggle2(ref this bool @bool) => @bool = !@bool;

这些只是切换一个 ref bool 变量值。测试:

class Foo
{
private bool _flag;
public void DoWork()
{
_flag.Toggle();
Console.WriteLine(_flag);
_flag.Toggle2();
Console.WriteLine(_flag);
}
}

我们得到:

True
False

问题:选择一种语法或另一种语法是否存在任何隐藏的差异?

最佳答案

不,它们完全一样,就像(现在)您可以使用 $@@$ 编写内插的逐字字符串文字一样。

关于c# - 谈论 C# 7.2 引用扩展方法时, "this ref"和 "ref this"有什么区别?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57531097/

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