It is not clear why C# doesn't allow calling a method passing a literal together with the in
parameter modifier; at the same time, when the literal passed to the method without the in
parameter modifier the code compiles.
Here is a code example that demonstrates this behavior (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);
}
}