Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix copy-paste error method-parameters.md #42676

Closed
wants to merge 1 commit into from

Conversation

andre1110
Copy link

@andre1110 andre1110 commented Sep 21, 2024

When you assign new instance to the value type paramenter by reference, the changes are visible from the caller method

var instance = new TestStruct { Property = 0 };
AssignNewInstance(ref instance);
Console.WriteLine(instance.Property);
// 42

void AssignNewInstance(ref TestStruct local)
{
    local = new TestStruct { Property = 42 };
    return;
}
struct TestStruct
{
    public int Property { get; set; }
}

Internal previews

📄 File 🔗 Preview link
docs/csharp/language-reference/keywords/method-parameters.md docs/csharp/language-reference/keywords/method-parameters

@andre1110 andre1110 requested review from BillWagner and a team as code owners September 21, 2024 11:20
@dotnet-policy-service dotnet-policy-service bot added the community-contribution Indicates PR is created by someone from the .NET community. label Sep 21, 2024
@BillWagner
Copy link
Member

Hi @andre1110

The current text is actually correct (although it could use some further clarification). See my notes in #32853

@BillWagner BillWagner closed this Sep 23, 2024
@andre1110
Copy link
Author

Hello @BillWagner
I tend to think that you are wrong for 2 reasons:

  1. Your code snippet in Pass a value type by reference: assigning the parameter to refer to a different object (learning.microsoft.com) #32853 demonstrates 'ref assignment' and not 'assignment' per se. IMHO when reading the sentence If the method assigns the parameter to refer to a different object I can only naturally imagine the code similar to what I pasted in the original post.
  2. If we assume you are correct, then the same 'ref assignment' for a reference type instance should yield different results according to the documentation: When you pass a reference type by reference: If the method assigns the parameter to refer to a different object, those changes are visible from the caller. However, in fact, it does not:
    static MyClass staticInstance = new MyClass { Id = 5 };
    
    var localInstance = new MyClass { Id = 3 };
    AssignNewMyClass(ref localInstance);
    Console.WriteLine("The value outside of the method: {0}", localInstance);
    // The value outside of the method: 3
    
    static void AssignNewMyClass(ref MyClass x){
        x = ref staticInstance;
        Console.WriteLine("The value inside the method: {0}", x);
        // The value inside the method: 5
    }
    
    class MyClass
    {
        public int Id { get; set; }
        public override string ToString() => Id.ToString();
    }

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
community-contribution Indicates PR is created by someone from the .NET community.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants