Back to notes -- Keyboard shortcut: 'u'        next -- Keyboard shortcut: 'n'  Slide program -- Keyboard shortcut: 't'    Demonstrations of references in C#.Lecture 2 - slide 11 : 43
Program 1
using System;

public class C {
  public double x, y;
}

public class ReferenceDemo {

  public static void Main(){

    C cRef, anotherCRef;  
    cRef = null;  
    Console.WriteLine("Is cRef null: {0}", cRef == null);      

    cRef = new C();                                            
    Console.WriteLine("Is cRef null: {0}", cRef == null);

    Console.WriteLine("x and y are ({0},{1})", cRef.x, cRef.y); 
    anotherCRef = F(cRef);      
  }                             

  public static C F(C p){
    Console.WriteLine("x and y are ({0},{1})", p.x, p.y);
    return p;
  }

}
 
 
 
 
 
 
 
 
 
 
Two variables of type C, both uninitialized.
A variable of reference type is assigned to null
We check if a reference is null.
 
We assign a variable to a reference to an object.
 
 
We access public members of the referenced object.
We pass the reference to a parameter,
and return it as the result.