Use ref for a struct parameter : Struct parameter : Struct C# Examples


C# Examples » Struct » Struct parameter »

 

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;
        }
}
    
   
  
   



Output

Result of PassByValue: myValue.val = 10
Result of PassByRef: myValue.val = 42


HTML code for linking to this page:

Follow Navioo On Twitter

C# Examples

 Navioo Struct
» Struct parameter