answersLogoWhite

0


Best Answer

They are not comparable, but may have some relationship between them.

An abstract class is a class, while a virtual function (or method) is a method. A method must exist within a class. Hence, a class has methods, and the methods may be defined as virtual functions.

A virtual function must be defined in a class, but that class does not have to be an abstract class. However, the purpose of a virtual function in C# is to provide a default behavior/implementation, while allowing the derived class to override that default implementation, hence it makes no sense to define a virtual function in a sealed class (a leaf, that is, no class can extend from it, and it is not an abstract class)

Example:

public class Parent {

public virtual string MostCommonPhrase() {

return "You better listen to me...";

}

}

public class Child : Parent {

public override string MostCommonPhrase() {

return "You never listen to me...";

}

}

User Avatar

Wiki User

13y ago
This answer is:
User Avatar

Add your answer:

Earn +20 pts
Q: What is the difference between abstarct class and virctulfunction in C sharp?
Write your answer...
Submit
Still have questions?
magnify glass
imp