Mega Code Archive

 
Categories / C# Tutorial / Struct
 

Use ref for a struct parameter

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