找回密码
 立即注册
首页 业界区 安全 C# 使用 using 关键字间接实现只读局部变量的方法 ...

C# 使用 using 关键字间接实现只读局部变量的方法

东门芳洲 7 小时前
众所周知,在 C# 里面是没有只读局部变量的功能的。但有趣的 C# 语法让咱可以使用现有的 using 关键字间接实现只读局部变量
在 C# 里面引入只读局部变量是一个存在 C# 语言设计仓库里面很久的讨论,详细请看 https://github.com/dotnet/csharplang/discussions/8479
官方在 2024.10.04 明确表示不会为 C# 添加只读局部变量的语法支持
今天我看到了一位名为 Sator Imaging 的开发者给出了一个很好的实现方案,那就是使用 using 关键字间接实现只读局部变量。以下是我根据他的想法编写的代码
  1. void Foo()
  2. {
  3.     using var iro = 10.AsReadOnly();
  4.     using var fro = 1.1f.AsReadOnly();
  5.     int foo = iro;
  6.     int bar = 20 * iro;  // can use as 'int'
  7.     float baz = fro * fro * iro;
  8.     // error: 'using variable' is protected by the system
  9.     //iro = 20.AsReadOnly();
  10. }
  11. public readonly record struct ReadOnly<T>(T Value) : IDisposable
  12. {
  13.     void IDisposable.Dispose() { }
  14.     public static implicit operator T(ReadOnly<T> x) => x.Value;
  15. }
  16. public static class ReadOnly
  17. {
  18.     public static ReadOnly<T> AsReadOnly<T>(this T value) => new(value);
  19. }
复制代码
这个方法巧妙在于利用了 C# 标记了 using 的变量不允许被更改的现有功能。通过结构体包装现有的局部变量类型对象,再配合隐式转换,从而实现比较弱的侵入性
本文代码放在 github 和 gitee 上,可以使用如下命令行拉取代码。我整个代码仓库比较庞大,使用以下命令行可以进行部分拉取,拉取速度比较快
先创建一个空文件夹,接着使用命令行 cd 命令进入此空文件夹,在命令行里面输入以下代码,即可获取到本文的代码
  1. git init
  2. git remote add origin https://gitee.com/lindexi/lindexi_gd.git
  3. git pull origin c73e4993fdc7fb8cdea2ab8367ba004003a94c27
复制代码
以上使用的是国内的 gitee 的源,如果 gitee 不能访问,请替换为 github 的源。请在命令行继续输入以下代码,将 gitee 源换成 github 源进行拉取代码。如果依然拉取不到代码,可以发邮件向我要代码
  1. git remote remove origin
  2. git remote add origin https://github.com/lindexi/lindexi_gd.git
  3. git pull origin c73e4993fdc7fb8cdea2ab8367ba004003a94c27
复制代码
获取代码之后,进入 Workbench/FacikereruyekoKiherbawjaiwha 文件夹,即可获取到源代码
更多技术博客,请参阅 博客导航

来源:程序园用户自行投稿发布,如果侵权,请联系站长删除
免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!

相关推荐

您需要登录后才可以回帖 登录 | 立即注册