目前尚不清楚为什么C#不允许调用将文字与in参数修饰符一起传递的方法。同时,当将文字传递给不带in参数修饰符的方法时,代码将进行编译。
这是一个演示此行为的代码示例(C#7.3):
class Program
{
static void Main(string[] args)
{
string s = string.Empty;
//These two lines compile
WriteStringToConsole(in s);
WriteStringToConsole("my string");
//Error CS8156 An expression cannot be used in this context because it may not be passed or returned by reference
WriteStringToConsole(in "my string");
}
public static void WriteStringToConsole (in string s)
{
Console.WriteLine(s);
}
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
57 次 |
| 最近记录: |