c# - 在谈论 C# 7.2 ref 扩展方法时,“this ref”和“ref this”有什么区别?
问题描述
考虑以下扩展方法:
public static void Toggle(this ref bool @bool) => @bool = !@bool;
public static void Toggle2(ref this bool @bool) => @bool = !@bool;
这些只是切换一个 ref 布尔变量值。测试:
class Foo
{
private bool _flag;
public void DoWork()
{
_flag.Toggle();
Console.WriteLine(_flag);
_flag.Toggle2();
Console.WriteLine(_flag);
}
}
我们得到:
True
False
问题:选择一种语法或另一种语法有什么隐藏的区别吗?
解决方案
不,它们完全相同,就像(现在)您可以使用$@
或编写插入的逐字字符串文字@$
。
推荐阅读
- mongodb - 在集合中没有索引的情况下使用 golang mongo 时无法通过 tier.Next(&result) 获取值,如果索引 stetted 则获取值
- svn - 有没有办法设置乌龟颠覆以在本地时区显示日志历史日期信息
- php - 尝试为 php 安装 geoip 扩展,找不到错误 pecl5.6 命令
- angular - 使用 ng-packagr 时出错:找不到模块:错误:无法解析
- asp.net - Task.Factory.StartNew 在没有调试模式的情况下运行时不起作用
- gensim - gensim 中 malletmodel2ldamodel 后的主题词分布问题
- regex - sed to edit a line on a file
- django - Django 查询集对象查询花费太多时间?
- python - 在 for 循环中向前 x 个项目添加不同的特定项目,而不使用 enumerate
- regex - 需要一个不接受第二个点的十进制数的正则表达式