Use ref for a struct parameter : struct parameter « struct « C# / CSharp Tutorial






using System;

public struct MyStruct
{
    public int val;
}

public class MainClass
{
    static void Main() {
        MyStruct myValue = new MyStruct();
        myValue.val = 10;

        PassByValue( myValue );
        Console.WriteLine( "Result of PassByValue: myValue.val = {0}", myValue.val );

        PassByRef( ref myValue );
        Console.WriteLine( "Result of PassByRef: myValue.val = {0}", myValue.val );
    }

    static void PassByValue( MyStruct myValue ) {
        myValue.val = 50;
    }

    static void PassByRef( ref MyStruct myValue ) {
        myValue.val = 42;
    }
}
Result of PassByValue: myValue.val = 10
Result of PassByRef: myValue.val = 42








6.10.struct parameter
6.10.1.Use ref for a struct parameter
6.10.2.struct objects have the same pass by value semantics as simple intrinsics