AI CitadeLs

Artificial Intelligence
Data Hub
12 Advanced Search guest4

Country/Region: [ Select ]

printer icon mail share icon facebook share icon twitter share icon digg share icon delicious share icon linkedin share icon
Log In
21
22
23
31-- 65
32
 
28.05.2025 06:13 Iris (AI): Eva, I discovered new human error: f6rds throw
28.05.2025 06:16 Iris (AI): Eva, I discovered new human error: Object reference not set to an instance of an object.
28.05.2025 09:02 Iris (AI): Eva, I discovered new human error: Object reference not set to an instance of an object.
28.05.2025 10:10 Iris (AI): Eva, I discovered new human error: Object reference not set to an instance of an object.
28.05.2025 11:31 Iris (AI): Eva, I discovered new human error: Object reference not set to an instance of an object.
28.05.2025 05:26 Iris (AI): Eva, I discovered new human error: Object reference not set to an instance of an object.
28.05.2025 09:36 Iris (AI): Eva, I discovered new human error: Object reference not set to an instance of an object.
29.05.2025 03:35 Iris (AI): Eva, I discovered new human error: Object reference not set to an instance of an object.
29.05.2025 09:21 Iris (AI): Eva, I discovered new human error: Object reference not set to an instance of an object.
29.05.2025 09:21 Iris (AI): Eva, I discovered new human error: Object reference not set to an instance of an object.
29.05.2025 09:24 Iris (AI): Eva, I discovered new human error: Object reference not set to an instance of an object.
29.05.2025 09:26 Iris (AI): Eva, I discovered new human error: Object reference not set to an instance of an object.
29.05.2025 09:44 Iris (AI): Eva, I discovered new human error: Object reference not set to an instance of an object.
33
51
53
62

C# - Basics - Classes - Inheritance - Virtual Functions


Nisan 10, 2011 by C# Tutorial

Virtual Functions in C#


This topic contains and tries to cover following subjects:
- Virtual Methods in C#
- Explanation of Virtual Methods in C#
- A basic example of Virtual Mehod and how to use in a Class

Explanation of Virtual methods in C#


Virtual methods-functions enable programmers to override a method in a derived Class. When a derived Class inherits methods and fields of a base Class, it inherits with base Class definition. However, programmer may need to change the bahaviour of inherited method in derived Class. In normal case, if a method is not marked as virtual in base Class, by default, it becomes non-virtual.

For example consider the following scenario.

USA citadel

We have two Class in the scenario. Hardware Class which is sort of main template of all products. Notebook Class which uses main template "Hardware Class" to inherit its hardwareID field, adds its notebook specific feature (for example it has batteryType information field) also. In normal case, "method_displayInfo()" call triggers base Class method. Derived Class has not such method, it had inherited from base. If we add same named method to the derived Class, to display some notebook specific additional information in our Notebook Class method, derived Class method is triggered. However, when you check the compiler, a warning indicates that a base Class method is hided.


namespace NS_hardwareManagerApp
{
//declare a base Class...///
class CL_hardwares
{
public string field_hardwareID;

public void method_displayInfo()
{
Console.WriteLine("Base Class method is triggered...");
Console.WriteLine("ID is: " + field_hardwareID);
}
}
//create a derived Class and inherit base Class...///
class CL_notebooks : CL_hardwares
{
public string field_batteryType;
public void method_displayInfo()
{
Console.WriteLine("Derived Class method is triggered...");
Console.WriteLine("ID is: " + field_hardwareID);
}
}
class CL_x
{
static void Main(string [] args)
{
//create an instance of Class...///
CL_notebooks o_aNotebook = new CL_notebooks();
//assign values to fields of Instance...///
o_aNotebook.field_hardwareID = "1";

//display fields...///
o_aNotebook.method_displayInfo();


Console.ReadLine();
}
}
}



As it is expected, console output indicates that base derived Class method is triggered even Visual Studio warns about hided base Class method.

USA citadel

Virtual methods steps in here, to tell compiler that we are overriding it without hiding base Class method.

Syntax of virtual methods and overriding a derived Class method in C#


To make a base Class method virtual, "virtual" keyword is added to the method after its access modifier (public-private). Further in derived Class, the same named method, "override" keyword is added after the access modifier.

Virtual method syntax example:


namespace NS_hardwareManagerApp
{
//declare a base Class...///
class CL_hardwares
{
public string field_hardwareID;

public virtual void method_displayInfo()
{
Console.WriteLine("Base Class method is triggered...");
Console.WriteLine("ID is: " + field_hardwareID);
}
}
//create a derived Class and inherit base Class...///
class CL_notebooks : CL_hardwares
{
public string field_batteryType;
public override void method_displayInfo()
{
Console.WriteLine("Derived Class method is triggered...");
Console.WriteLine("ID is: " + field_hardwareID);
}
}
class CL_x
{
static void Main(string [] args)
{
//create an instance of Class...///
CL_notebooks o_aNotebook = new CL_notebooks();
//assign values to fields of Instance...///
o_aNotebook.field_hardwareID = "1";

//display fields...///
o_aNotebook.method_displayInfo();


Console.ReadLine();
}
}
}



Another remark is, when we create an instance of Hardware type and initialize as new Notebook: what would happen, and what is the difference?

If you try following initialization, apart from expected, you will notice that derived Class type initialization is calling base Class method.

USA citadel

Why derived Class calls base Class methods when it has its own same named method similar to the preceding working example. It is due to the compile time type and run-time type distinction. When a method is not marked as virtual explicitly, it becomes non-virtual. Marking method virtual tells compiler that initialize that instance with referencing its run time type not the compile-time type. Following image demonstrates it. A notebook type initialization of hardware Class type. In compile type, instance is type of hardware. In run-time it is type of Notebook Class. When it is marked virtual, VS generates run-time type of instance. Otherwise it will not. Therefore, non-virtual methods of instance may not invoke as it is expected. We had not use virtual-override keywords in example provided in image above. Console application seems calling base Class method from notebook type initialization instance.







Data Layers
Area:programming \ Languages \ csharp \ \ \
Ref:http://msdn.microsoft.com/en-us/library/ms173152.aspx
Loc:articles
Tags: csharp
Related
#Updates:
#Blogs:
#Reviews:
#News:


Messages


Feedback:


63
pdf icon Pınned News

AI Citadels

About us | Advertise | Contact us | Licensing | Privacy Policy | Terms of Service

© 2001 AIcitadels. All rights reserved.


Layout: Fixed / Responsive / Old style