This In-depth Tutorial Explains all About C# Using Statement And Virtual Method. You will Also Learn the Difference Between Abstract And Virtual Methods:
The Using block mainly helps in managing resources, it allows the system to manage its resources by specifying the scope of the object and its resource requirement.
The .Net Framework offers different ways for resource management for objects using a garbage collector. It means that you don’t need to explicitly allocate and remove memory objects. Cleaning operation for any unmanaged object will be handled by using destructor.
To help programmers to achieve this, C# using statement provides a condition for the destruction of the object.
=> Check Out The Free C# Training Guide Here.
To achieve auto-destruction of the object, C# offers a dispose method that can be called when the object is not required anymore. The using statement in C# defines a conditional boundary for the existence of the object. Once the execution sequence leaves the using boundary, the .Net framework will know that its time to destroy that object.
Table of Contents:
C# Using Statement
Implement IDisposable Interface For Using
The C# Using statement allows the programmers to implement several resources in one statement. All the objects defined inside the using code block should implement the IDisposable interface, and this allows the framework to call the dispose methods for the specified objects inside the statement once it is exited.
Example
Using statements can be combined with a type that can implement IDisposable like StreamWriter, StreamReader, etc.
Let’s have a look at a simple program:
public class Program { public static void Main(string[] args) { using (SysObj so = new SysObj()) { Console.WriteLine("Inside using statement"); } Console.WriteLine("Outside of the using statement block"); } } class SysObj : IDisposable { public void Dispose() { Console.WriteLine("Dispose method"); } }
Output
The output of the above program:
Inside using statement
Dispose method
Outside of the using statement block
Explanation
In the above example, when the program is executed, first the “SysObj” instance is allocated in the memory heap. Then the using block starts executing and prints the output that we defined inside the console. Next, as the Using statement block gets over, the execution is immediately transferred to the dispose method.
Then the code exits the statement block and prints the outside statement to the console.
C# Virtual Method
What Is A Virtual Method?
A virtual method is a class method that offers functionality to the programmer to override a method in the derived class that has the same signature. Virtual methods are mainly used to perform polymorphism in the OOPs environment.
A virtual method can have an implementation in both derived and base classes. It is mainly used when a user needs to have more functionality in the derived class.
A virtual method is first created in a base class and then it is overridden in the derived class. A virtual method can be created in the base class by using the “virtual” keyword and the same method can be overridden in the derived class by using the “override” keyword.
Virtual Methods: Few Points To Remember
- The virtual method in the derived class has the virtual keyword and the method in derived class should have an override keyword.
- If a method is declared as a virtual method in the base class, then it’s not always required by the derived class to override that method i.e. its optional to override a virtual method in the derived class.
- If a method has the same definition in both the base and derived class then it’s not required to override the method. Override is only required if both have a different definition.
- The overriding method allows us to use more than one form for the same method, hence it also shows polymorphism.
- All the methods are non-virtual by default.
- A virtual modifier cannot be used together with Private, Static, or Abstract modifiers.
What Is The Use Of Virtual Keyword In C#?
The virtual keyword in C# is used to override the base class member in its derived class based on the requirement.
A virtual keyword is used to specify the virtual method in the base class and the method with the same signature that needs to be overridden in the derived class is preceded by override keyword.
Difference Between Abstract Method And Virtual Method
Virtual methods contain implementation and allow the derived class to override it whereas the abstract method does not offer any implementation and it forces the programmers to write override methods in the derived class.
Hence, in simple words, the abstract methods don’t have any code inside them whereas the virtual method has its own implementation.
Difference Between Virtual And Override In C#
The virtual keyword is usually followed by the signature of the method, property, etc. and allows it to be overridden in the derived class. The override keyword is used in the derived class with the same method/property signature as in the base class to achieve override in the derived class.
Is It Mandatory To Override Virtual Method In C#?
The compiler will never enforce programmers to override a virtual method. It’s not always required by the derived class to override the virtual method.
Example
Let’s have a look at an example to understand more clearly about the virtual methods.
In this example, we will be using two different methods in the base class, the first one is a non-virtual method and the other one is a virtual method with the virtual keyword. Both these methods will be overridden in the derived class.
Let us have a look:
Program
using System; public class Program { public static void Main(string[] args) { calculate calc = new calculate (); numbers nmbr = calc; calc.addition(); nmbr.addition(); calc.subtraction(); nmbr.subtraction(); } } public class numbers { public void addition(){ Console.WriteLine("This is addition method"); } public virtual void subtraction(){ Console.WriteLine("This is subtraction method"); } } public class calculate : numbers { public void addition(){ Console.WriteLine("This is addition method in the derived class"); } public override void subtraction(){ Console.WriteLine("This is subtraction method override in derived class"); } }
Output
The output of the above program is:
This is addition method in the derived class
This is addition method
This is subtraction method override in derived class
This is subtraction method override in derived class
Explanation
In the above example, we have two classes i.e. Number and Calculate. The base class Number has two methods i.e. addition and subtraction where addition is a non-virtual method and subtraction is a virtual method. Hence, when we execute this program the base class virtual method “addition” is overridden in the derived class Calculate.
In another class “Program” we create an entry point to create an instance of the derived class Calculate and then we assign the same instance to the instance object of the base class.
When we call the virtual and non-virtual methods by using the class instances then we see that the virtual method got overridden by using both the instances whereas the non-virtual method was overridden only while calling the derived class.
Conclusion
The using statement in C# is mainly used for resource management. The using statement defines a conditional boundary for the existence of an object.
Once the execution moves out of the statement block, it tells the framework to destroy any object that was created inside the statement block. Code defined inside the statement should also implement an IDisposable interface to allow the .Net framework to call the dispose method for the defined objects.
A virtual method allows the user to override a method in the derived class that has the same signature as the method in the base class. The virtual method can be used to achieve polymorphism in object-oriented programming languages.
A virtual method is mainly used when extra functionality is required in the derived class. Virtual methods cannot be private static or abstract. It is defined by using a virtual keyword in the base class and override keyword in the derived class.
=> Watch Out The In-Depth C# Training Tutorials Here