Skip to content

In parameter modifiers C# 7.2correctnessperformance

Allow arguments to be passed by reference to avoid modification.

WARNING

This page is incomplete.

C# 7.2 brings the in parameter modifier. The compiler will issue an 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