Reference
types are yet another type available in the C# unified type system. Reference
types are inherited from System.Object and they have the Object behavior.
Reference types are allocated in heap and the reference type objects point
to the address where the data is stored; it doesn't directly hold the
data. Different reference types supported by .NET are mentioned below:
" String
- They are used to accommodate set of characters in one single variable
of type string.
" Class - Class is a user defined data structure which encapsulates
properties and methods accessing those properties into a single unit.
Classes are accessed by creating instances of it. These instances are
reference types.
" Interface - Interface is a template containing only the
properties and method signatures. Methods will not be implemented. They
will be implemented by the classes which implement this interface. Interfaces
are mainly used to establish multiple inheritance wherein one class can
implement more than one interface.
" Delegates - Delegates are used to wrap events or methods
and executing them. One delegate can wrap more than one method and it
can execute all the wrapped methods in sequential order. Delegates are
also used to create and wrap anonymous methods.
Given below
is an example illustrating usage of all four reference types discussed
above:
public delegate void sampleDelegate( );
public interface ISampleInterface1{
int sampleMethod1( );
}
public interface ISampleInterface2{
void sampleMethod2( );
} class sampleClass : ITestInterface1, ITestInterface2 {
int member1 ; string member2;
public sampleClass(int member1, string member2) {
this.member1 = member1;
this.member2 = member2;
}
int sampleMethod1( ) {
return member1;
}
void sampleMethod2( ) {
Console.WriteLine("Member2 = " + member2);
}
void displayMember1( ) {
Console.WriteLine("Member1 = " + member1);
}
}
public class testClass {
public static void Main( ) {
sampleClass obj = new sampleClass(100,"Hello!" );
Console.WriteLine("Member1 = {0}", obj1.sampleMethod1( ));
obj1.sampleMethod2( );
Console.WriteLine("Executing
Delegates .");
sampleDelegate delegateObj = new sampleDelegate(obj.displayMember1);
delegateObj += new sampleDelegate(obj.sampleMethod2());
delegateObj();
}
}
Output of this code will be:
Member1 = 100
Member2 = Hello!
Executing Delegates .
Member1 = 100
Member2 = Hello!
In this example,
you have used all four reference types for the following purpose:
" Delegate - You have created a delegate called sampleDelegate
to wrap methods with return type as void and with no arguments. In testClass,
you create an instance of the reference type delegate called delegateObj.
Using this instance, you wrap two methods of sampleClass. You add more
than one method to the delegate using += operator. Now by calling delegateObj(),
you execute both the methods in the order in which they are added to the
delegate.
" Interface - In this example, you have created two interfaces
sampleInterface1, sampleInterface2. Each of them contains a method of
different name.
" Class - You create a class sampleClass which implements
both the interfaces mentioned above. Hence it has to compulsorily override
the methods sampleMethod1 and sampleMethod2 available in the interfaces.
In addition, it defines an additional method called displayMember1 to
display the member1 property value in the console. You have also created
another class called testClass containing Main method inside which instance
for sampleClass named sampleDelegate is created and the appropriate methods
are executed.
" String - You have created a string called member2 inside
sampleClass and you have set its value by passing string argument in the
constructor.