Skip to content

12.6.6.1: Defensive copy of input parameter #894

@KalleOlaviNiemitalo

Description

@KalleOlaviNiemitalo

Describe the bug

In the C# 7.x draft, 12.6.6.1 (Function member invocation / General) describes how a copy of a value-type instance "not classified as a variable" is made and used instead of the original. This defensive copying should also apply to an input parameter unless its type is a readonly struct.

Example

using System;
struct S {
    public int I;
    public void M() { I++; }
}
readonly struct R {
    public void M() {}
}
class C {
    static void Main() {
        N(new S(), new S(), new R());
    }
    static void N(S s1, in S s2, in R r) {
        s1.M(); // no copying
        Console.WriteLine(s1.I); // 1
        
        s2.M(); // copy required
        Console.WriteLine(s2.I); // 0

        r.M(); // no copying
    }
}

Expected behavior

In 12.6.6.1, declare:

If E is not classified as a variable, or if E is an input parameter (15.6.2.3) and V is not a readonly struct type (16.2.2), then a temporary local variable of E’s type is created and the value of E is assigned to that variable.

Additional context

None.


Associated WorkItem - 157261

Metadata

Metadata

Assignees

Labels

No labels
No labels

Type

No type

Projects

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions