C# (C Sharp) Unified Type System: Reference TypesReference types form part of unified type system. They aim at providing referential identification for instances. In value types, when two value type variables have the same data then those value types are said to be identical. But in reference types irrespective of the data equivalence, the two instances are said to be different. Reference type variables do not directly contain the data.
Instead they hold the address where the data is put up. Data will be available in a particular address of the managed heap. All reference type variables are implicitly assigned with the value null. Accessing reference type variables that are not explicitly initialized will throw null pointer exception. Reference Types supported by C# are: Class Class: Class is a data structure that is used to establish encapsulation and other object oriented behavior. Class has members that can either be properties or methods accessing those properties. Each class and its members have its visibility based on the modifier defined. Here is a simple class: public class
testClass { This class has a global visibility. It is visible across all packages since the modifier public is used. This class has a property prop1 and two methods to set and get values from it. If you want to access members from outside the class, then you have to create instances of the class. Each instance
of the class will be declared and accessed using a reference type variable.
In the example given below, an object of testClass is created: In this example, obj1 is a reference type variable pointing to an instance of testClass. Using this reference type variable you can access members of testClass. Each instance created has its parent class to be System.Object either directly or as the root class in the inheritance tree. Not just instances, any variable created for any type will be derived from System.Object. String: Rather than a separate type, even String forms part of a class. If you want to record a collection of characters in one single data type then you can use System.String class. This class cannot be inherited. Thats why it is one among the sealed classes. Here is a simple example to demonstrate the usage of String class: public class
testString( ) { } This example illustrates only two methods of String class. There are all together 44 methods available in System.String class. Some of them are: Clone, Concat, Copy, GetHashCode, ToLower, Split, Trim. Interface: If you want to derive characteristics of a class into another, you can achieve it using inheritance. What if you want to derive characteristics from two classes? You cannot do it. This is because a class can be inherited from only one class. However, you can achieve this functionality using interface. Interface is an abstract representation which has properties and methods. These members will only be declared but not defined. Class that implements this interface has to define all members of the interface. A class has a liberty to implement more than one interface. Here is an example in which a class is implementing more than one interface: public interface
ITestInterface1{ Delegates: If you have to call an anonymous method or call a method that can be determined only at run time and not at compile time, then you can wrap them inside a delegate and declare a reference type for it. Delegates are mainly used for handling events. Consider the following example: public delegate
void testDelegate( ); public static
void Main( ) { Here a delegate deleg1 is wrapping a method called testMethod1. Output of this code will be: Executing testMethod1
|