Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there Virtual class in C#?

Tags:

c#

I have read about virtual methods in C#. Is there any concept called virtual class in C#? Is abstract class in C# and virtual class in C++ the same?

like image 305
Tejus Avatar asked Feb 27 '13 04:02

Tejus


People also ask

Can a class have virtual?

A class may have virtual destructor but it cannot have a virtual constructor.

How do you declare a virtual class?

To resolve this ambiguity when class A is inherited in both class B and class C, it is declared as virtual base class by placing a keyword virtual as : Syntax for Virtual Base Classes: Syntax 1: class B : virtual public A { }; Syntax 2: class C : public virtual A { };

Why virtual constructor is not possible?

Constructor can not be virtual, because when constructor of a class is executed there is no vtable in the memory, means no virtual pointer defined yet. Hence the constructor should always be non-virtual.

Is virtual base class same as virtual class?

In object-oriented programming, a virtual base class is a nested inner class whose functions and member variables can be overridden and redefined by subclasses of an outer class. Virtual classes are analogous to virtual functions.


2 Answers

There is no such thing in C# - and it's really not necessary since multiple implementation inheritance is not supported. Making a class abstract really only means that you cannot create instances of that class and they might not be fully implemented (e.g. might contain abstract methods).

like image 98
BrokenGlass Avatar answered Nov 09 '22 08:11

BrokenGlass


There are no virtual classes in C#. Abstract class is not the same since you cannot instantiate an abstract class.

You can do the opposite of marking something virtual by marking it sealed, which prevents it from being inherited.

like image 7
si618 Avatar answered Nov 09 '22 08:11

si618