Reference Types in C#

A variable representing an object of Reference type contains reference or address of the actual data. Reference types are allocated on the managed heap. Different reference types are:
 
? The Object Type

? The class Type

? Interfaces

? Delegates

? The string type

? Arrays

 
The object type
 
The object class type is the ultimate base class of all other types. Because it is the base class for all objects, you can assign values of any type to it. Every type in C# directly or indirectly derives from the object class type. It is not equivalent to void* as in C++. The object type is used when a value type is boxed.
 
Class types
 
A class type defines a data structure that contains data members (constants, fields, and events), function members (methods, properties, indexers, operators, constructors, and destructors), and nested types. Class types support inheritance, a mechanism whereby derived classes can extend and specialized base classes. Only single inheritance is allowed. However a class in C# can derive from multiple interfaces.
 
The string type
 
The string type is a sealed class type that inherits directly from object. Instances of the string class represent Unicode character strings. Values of the string type can be written as string literals C# has base type string for manipulating string data.
 
Its usage is simple
 
string author=”Ujjwal Mehta”;
 
Even we can access a single character with the help of accessing the indexer.
 
char first=author[0];
 
Interface types
 
An interface declares a reference type that has abstract members only. Only the signature exists, but there is no implementation code at all. An implementation of this is that we cannot instantiate an interface, only an object that derives from the interface.
 
Example:
 
interface Inter
{
    void showInter();
}

 
As I said we cannot instantiate an object from this definition, but we can derive a class from it. However that class must implement the showInter abstract method. The main difference between interface members and class members is that interface members do not have an implementation.
 
class Ainter:Inter
{
   public void showInter()
   {
      Console.WriteLine("IMPLEMENTATION");
   }
}
 
Array types
 
An array is a data structure that contains a number of variables which are accessed through computed indices. The variables contained in an array, also called the elements of the array, are all of the same type, and this type is called the element type of the array.
 
Example :
 
using System;
class Array 
{
  public static void Main() 
  {
     int[] Ints = { 25, 9, 79 };
     string[] myStr = new string[4];

     Console.WriteLine("Ints[0]: {0}, Ints[1]: {1}, Ints[2]: {2}",
     Ints[0], Ints[1],Ints[2]);

     myStr[0] = "gnana arun ganesh";
     myStr[1] = "gnanavel";
     myStr[2] = "vadi";
     myStr[3] = "saru";

    Console.WriteLine("myStr[0]: {0}, myStr[1]: {1}, myStr[2]:{2}, 
myStr[3]: {3}",myStr[0], myStr[1], myStr[2]),mystr[3]);
  }
}

 
Delegate types
 
A delegate is a data structure that refers to a static method or to an object instance and an instance method of that object. A delegate encapsulate a method with a certain signature.
 
The closest equivalent of a delegate in C or C++ is a function pointer, but whereas a function pointer can only reference static functions, a delegate can reference both static and instance methods. In the latter case, the delegate stores not only a reference to the method’s entry point, but also a reference to the object instance for which to invoke the method.
 
Unlike function pointers, delegates are object-oriented, type-safe, and secure. Delegates are reference types that derive from a common base class: System.Delegate. A delegate instance encapsulates a method – a callable entity. For instance methods, a callable entity consists of an instance and a method on the instance.
 
If you have a delegate instance and an appropriate set of arguments, you can invoke the delegate with the arguments. An interesting and useful property of a delegate is that it does not know or care about the class of the object that it references. Any object will do; all that matters is that the method’s signature matches the delegate’s. This makes delegates perfectly suited for “anonymous” invocation. This is a powerful capability.
 
There are three steps in defining and using delegates: declaration, instantiation, and invocation. Delegates are declared using delegate declaration syntax. A delegate that takes no arguments and returns void can be declared with
 
delegate void SimpleDelegate();
 
A delegate instance can be instantiated using the new keyword, and referencing either an instance or class method that conforms to the signature specified by the delegate. Once a delegate has been instantiated, it can be called using method call syntax.
 
Example :
 
class Test
{
	static void A() 
	{
		System.Console.WriteLine("Test.A");
	}
	static void Main() 
	{
		SimpleDelegate d = new SimpleDelegate(A);
		d();
	}
}

A SimpleDelegate instance is created and then immediately invoked. Of course, there is not much point in instantiating a delegate for a method and then immediately calling via the delegate, as it would be simpler to call the method directly. Delegates show their usefulness when their anonymity is used. For example, we could define a MultiCall method that can call repeatedly call a SimpleDelegate.
 
void MultiCall(SimpleDelegate d, int count) 
{
	for (int i = 0; i < count; i++)
	{
		d();
	}
}
 
Parameter Type
 
There are four different types of parameters in C#. They are:
 
? Value parameters

? Ref parameters

? Out parameters

? Parameter arrays (Params)

 
1. Value parameters
 
A parameter declared with no modifiers is a value parameter and this is the default parameter type in C#. In this type of parameters actual value is passed to the function, and the changes made in the function not affect the original value in the calling function.
 
static void fnValue( int i)
{ 
 	i = i + 1; 
}
 
2. Ref parameters
 
Ref parameters are input/output parameters. This type of parameters are declared with the modifier “ref” . these parameters are used to pass the values back and fourth between the called function and calling function. Here the actual reference of the variable is passed to the function.
 
static void fnRef( ref int i) 
{ 
 	i = i + 1; 
} 
 
3. Out parameters
 
Out parameters are Similar to a reference parameter, these parameters are declared with the out modifier. Output parameters are typically used in methods that produce multiple return values. The out put parameters can only pass back a value from a function. Every output parameter of a method must be assigned before the method returns.
 
static void fnOut ( out int k, out int i ) 
{ 
 	k =5; 
 	i=9; 
} 
 
4. Parameter arrays
 
This type of parameters is declared with the modifier params. These types of parameters are very useful when we need to pass the number of arguments to the function. In the parameter list the parameter array must be the last parameter and it must be of a single-dimensional array type. Params parameters are input type parameters.
 
Using of out and ref parameter
 
The out and the ref parameters are used to return values in the same variables, that you pass an argument of a method. These both parameters are very useful when your method needs to return more than one values.
 
The out Parameter
 
The out parameter can be used to return the values in the same variable passed as a parameter of the method. Any changes made to the parameter will be reflected in the variable.
 
public class mathClass
{
 	public static int TestOut(out int iVal1, out int iVal2)
 	{
 		iVal1 = 10;
 		iVal2 = 20;
 		return 0;
 	} 
 	public static void Main()
 	{
 		int i, j;  // variable need not be initialized
 		Console.WriteLine(TestOut(out i, out j));
 		Console.WriteLine(i);
 		Console.WriteLine(j);
 	}
}
 
The ref parameter
 
The ref keyword on a method parameter causes a method to refer to the same variable that was passed as an input parameter for the same method. If you do any changes to the variable, they will be reflected in the variable.
 
You can even use ref for more than one method parameters.
 
namespace TestRefP
{ 
 	using System; 
 	public class myClass 
 	{ 
 		public static void RefTest(ref int iVal1 ) 
 		{ 
 			iVal1 += 2; 
 		} 
 		public static void Main() 
 		{ 
 			int i;  // variable need to be initialized 
 			i = 3; 
 			RefTest(ref i ); 
 			Console.WriteLine(i); 
 		} 
 	} 
}