Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Virtual/Abstract fields in C#

Is it possible to have a virtual/abstract field in a C# class? If so, how is it done?

like image 932
user489041 Avatar asked Sep 20 '11 19:09

user489041


People also ask

Can fields be virtual in C#?

No. Fields are just there to hold data.

Can abstract class have fields C#?

C# Abstract Class Features An abstract class can implement code with non-Abstract methods. An Abstract class can have modifiers for methods, properties etc. An Abstract class can have constants and fields.

What is a virtual property C#?

Virtual properties behave like virtual methods, except for the differences in declaration and invocation syntax. It is an error to use the virtual modifier on a static property. A virtual inherited property can be overridden in a derived class by including a property declaration that uses the override modifier.

Can fields be abstract?

An abstract class may have static fields and static methods. You can use these static members with a class reference (for example, AbstractClass.


2 Answers

A Virtual/Abstract field? No. Fields are just there to hold data. There's nothing to implement.

You can define a Virtual/Abstract Property though.

like image 104
Justin Niessner Avatar answered Sep 21 '22 12:09

Justin Niessner


You can however have virtual or abstract properties:

public abstract string ModelName { get; set; } 
like image 45
Blindy Avatar answered Sep 20 '22 12:09

Blindy