Objects can be passed to methods

image_pdfimage_print

   

/*
C#: The Complete Reference 
by Herbert Schildt 

Publisher: Osborne/McGraw-Hill (March 8, 2002)
ISBN: 0072134852
*/
// Objects can be passed to methods.  
 
using System; 
 
class MyClass {  
  int alpha, beta; 
  
  public MyClass(int i, int j) {  
    alpha = i;  
    beta = j;  
  }  
  
  /* Return true if ob contains the same values 
     as the invoking object. */ 
  public bool sameAs(MyClass ob) {  
    if((ob.alpha == alpha) & (ob.beta == beta)) 
       return true;  
    else return false;  
  }  
 
  // Make a copy of ob. 
  public void copy(MyClass ob) { 
    alpha = ob.alpha; 
    beta  = ob.beta; 
  } 
 
  public void show() { 
    Console.WriteLine("alpha: {0}, beta: {1}", 
                      alpha, beta); 
  } 
}  
  
public class PassOb {  
  public static void Main() { 
    MyClass ob1 = new MyClass(4, 5);  
    MyClass ob2 = new MyClass(6, 7);  
  
    Console.Write("ob1: "); 
    ob1.show(); 
 
    Console.Write("ob2: "); 
    ob2.show(); 
 
    if(ob1.sameAs(ob2))  
      Console.WriteLine("ob1 and ob2 have the same values."); 
    else 
      Console.WriteLine("ob1 and ob2 have different values."); 
 
    Console.WriteLine(); 
 
    // now, make ob1 a copy of ob2 
    ob1.copy(ob2); 
 
    Console.Write("ob1 after copy: "); 
    ob1.show(); 
 
    if(ob1.sameAs(ob2))  
      Console.WriteLine("ob1 and ob2 have the same values."); 
    else 
      Console.WriteLine("ob1 and ob2 have different values."); 
 
  }  
} 

           
          


Passing Parameters By Value and By Ref

image_pdfimage_print

/*
* C# Programmers Pocket Consultant
* Author: Gregory S. MacBeth
* Email: gmacbeth@comporium.net
* Create Date: June 27, 2003
* Last Modified Date:
*/
using System;

namespace Client.Chapter_5___Building_Your_Own_Classes
{
public class PassingParametersByValueandByRef
{
static void Main(string[] args)
{
int MyInt = 5;

MyIntArray = new int[10];
ObjectCount++;
Method2();
Method2(1, 2);
MyMethodRef(ref MyInt);
Method2(new int[] { 1, 2, 3, 4 });
}
static private int MyInt = 5;
static public int MyInt2 = 10;
static public int[] MyIntArray;
private static int ObjectCount = 0;
static public int MyMethodRef(ref int myInt)
{
MyInt = MyInt + myInt;
return MyInt;
}
static public int MyMethod(int myInt)
{
MyInt = MyInt + myInt;
return MyInt;
}
static private long MyLongMethod(ref int myInt)
{
return myInt;
}
static public void Method2(params int[] args)
{
for (int I = 0; I < args.Length; I++) Console.WriteLine(args[I]); } } } [/csharp]

Parameter out and reference

image_pdfimage_print

   


using System;  


class ReferenceAndOutputParamtersTest
{
   static void Main( string[] args )
   {

      int y = 5;
      int z; 

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

      SquareRef( ref y ); // must use keyword ref
      SquareOut( out z ); // must use keyword out

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

   } 
   static void SquareRef( ref int x )
   {
      x = x * x;
   }

   static void SquareOut( out int x )
   {
      x = 6;
      x = x * x;
   } 

   static void Square( int x )
   {
      x = x * x;
   } 

}
           
          


Iff operator in C#

image_pdfimage_print
   
 

//Octavalent Extension Methods
//http://sdfasdf.codeplex.com/
//Library of extension methods for .Net create by Octavalent (www.octavalent.nl)


namespace System.OctavalentExtensions
{
    public static class BooleanExtensions
    {
        public static object Iff(this bool value, object valueIfTrue, object valueIfFalse)
        {
            if (value)
                return valueIfTrue;

            return valueIfFalse;
        }
    }
}