What are Reference Types in .NET?

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.

| How do you prevent a class from overriding in .NET? | How are classes related to objects in .NET Application | How are Delegates different from Events in .NET? | How are system exceptions different from application exceptions in .NET? | How are Value Types different from Reference Types in .NET? | How can a finalize method be suppressed in .NET? | How can you call Stored Procedure in ADO.NET? | How can you force Dispose method to be called automatically in .NET? | How do you call a Base Class Constructor from Derived Class Constructor in .NET? | How do you connect your VB.NET application to SQL Server? | How do you implement Cloning in .NET? | How do you implement Façade Design Pattern in .NET? | How do you implement MVC Pattern in ASP.NET? | How do you install .NET Assembly in GAC? | How is shadowing different from overriding in .NET? | How to prevent a particular .NET DLL from being decompiled? | Illustrate Delay Signing Process of an Assembly in .NET? | What are Reference Types in .NET? | What are the advantages of C#? | What are the advantages of VB.NET? | What are the differences between Namespace and Assembly in .NET? | What are the similar features between class and structure in .NET? | What are Value Types in .NET? | What do you mean by mixed mode authentication in .NET? | What do you mean by Satellite Assembly in .NET? | What do you mean by shadowing in .NET? | What is CTS in .NET? | What is ILDASM in .NET? | What is Managed Code in .NET? | What is Manifest in .NET? | What is MSIL in .NET Framework? | What is the importance of finalize method in .NET? | What is the need for Visitor Pattern in C#? | What is the purpose of bindingRedirect tag in web.config file of .NET? | What is the purpose of CodeDom in .NET? | What is the purpose of dispose method in .NET? | What is the purpose of Ngen.exe in .NET? | What is the purpose of Strong Name in COM Components of .NET? | What is the purpose of virtual keyword in .NET? | What Object Oriented Principles can be incorporated in .NET Application? |


“Amazon and the Amazon logo are trademarks of Amazon.com, Inc. or its affiliates.”

| Privacy Policy for www.dotnet-guide.com | Disclosure | Contact |

Copyright - © 2004 - 2024 - All Rights Reserved.