Pass valuel by pointer

   
 
using System;

public class Starter {
    public unsafe static void Main() {
        int val = 5;
        int* pA = &val;
        int* pB;
        pB = MethodA(pA);
        Console.WriteLine("*pA = {0} | *pB = {0}", *pA, *pB);
    }

    public unsafe static int* MethodA(int* pArg) {
        *pArg += 15;
        return pArg;
    }
}

    


creates instances of a value and a reference type

   
 

using System;

class Starter {

    static void Main() {
        int localvalue = 5;
        MyClass objZ = new MyClass();
        DisplayType(localvalue);
        DisplayType(objZ);
    }

    static void DisplayType(object parameterObject) {
        Type parameterType = parameterObject.GetType();
        string name = parameterType.Name;
        Console.WriteLine("Type is " + name);
        if (name == "MyClass") {
            ((MyClass)parameterObject).Display();
        }
    }

}

class MyClass {

    public void Display() {
        Console.WriteLine("MyClass::Display");
    }
}

    


Passing parameters to methods

   
 

using System;

class ParameterTest {
    static void SomeFunction(int[] ints, int i) {
        ints[0] = 100;
        i = 100;
    }

    public static void Main() {
        int i = 0;
        int[] ints = { 0, 1, 2, 4, 8 };

        Console.WriteLine("i = " + i);
        Console.WriteLine("ints[0] = " + ints[0]);
        Console.WriteLine("Calling SomeFunction...");

        SomeFunction(ints, i);
        Console.WriteLine("i = " + i);
    }
}

    


Reference, output and value parameters.

   
 




using System;

class ReferenceAndOutputParameters
{
   public void aMethod()
   {
      int y = 5; 
      int z; 

      Console.WriteLine( "Original value of y: {0}", y );
      Console.WriteLine( "Original value of z: uninitialized
" );

      SquareRef( ref y );  
      SquareOut( out z );  

      Console.WriteLine( "Value of y after SquareRef: {0}", y );
      Console.WriteLine( "Value of z after SquareOut: {0}
", z );

      Square( y );
      Square( z );

      Console.WriteLine( "Value of y after Square: {0}", y );
      Console.WriteLine( "Value of z after Square: {0}", z );
   } 
   void SquareRef( ref int x )
   {
      x = x * x; 
   } 
   void SquareOut( out int x )
   {
      x = 6; 
      x = x * x; 
   } 
   void Square( int x )
   {
      x = x * x;
   }
} 
class ReferenceAndOutputParamtersTest
{
   static void Main( string[] args )
   {
      ReferenceAndOutputParameters test = new ReferenceAndOutputParameters();
      test.aMethod();
   }
}


           


Provides a simple example of function overloading


   

/*
C# Programming Tips & Techniques
by Charles Wright, Kris Jamsa

Publisher: Osborne/McGraw-Hill (December 28, 2001)
ISBN: 0072193794
*/

//
// Overload.cs -- Provides a simple example of function overloading
//
//                Compile this program with the following command line:
//                    C:>csc Overload.cs
//
namespace nsOverload
{
    using System;
    
    public class clsMainOverload
    {
        static public void Main ()
        {
            int iVal = 16;
            long lVal = 24;
            Console.WriteLine ("The square of {0} is {1}
",
                               iVal, Square(iVal));
            Console.WriteLine ("The square of {0} is {1}",
                               lVal, Square(lVal));
        }
        static int Square (int var)
        {
            Console.WriteLine ("int Square (int var) method called");
            return (var * var);
        }
        static long Square (long var)
        {
            Console.WriteLine ("long Square (long var) method called");
            return (var * var);
        }
    }
}


           
          


Define function


   

/*
Learning C# 
by Jesse Liberty

Publisher: O'Reilly 
ISBN: 0596003765
*/
 using System;
public class Functions
 {
     static void Main()
     {
         Console.WriteLine("In Main! Calling SomeMethod()...");
         SomeMethod();
         Console.WriteLine("Back in Main().");

     }
     static void SomeMethod()
     {
         Console.WriteLine("Greetings from SomeMethod!");
     }
 }