Skip to content

In parameter modifiers C# 7.2correctnessperformance

Pass parameters by reference without allowing modifications.

C# 7.2 brings in to parameter modifiers telling the compiler to error if an attempt is made to modify the value within the method.

Code

C#

public string Capitalize(in string input)
{
    input = input.ToUpper();
    return input;
}
C#
public string GetValue(string key, string defaultValue)
{
    if (store.TryGetValue(key, out var value))
    {
        return value;
    }

    store.SetValue(key, defaultValue);
    return value;
}

Notes

WARNING

in parameters are superseded by the ref readonly C# 12.0 which guarantees no copying of the value.

More information