Virtual/Abstract fields in C#

2019-01-23 22:42发布

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

10条回答
何必那么认真
2楼-- · 2019-01-23 23:16

You can however have virtual or abstract properties:

public abstract string ModelName { get; set; }
查看更多
\"骚年 ilove
3楼-- · 2019-01-23 23:19

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.

查看更多
劫难
4楼-- · 2019-01-23 23:27

Properties can be virtual, may be you can gain on that. At least it is heavily used in NHibernate.

Basically you have to have a method to virtualize, how should the virtual field work?

查看更多
甜甜的少女心
5楼-- · 2019-01-23 23:30

No. fields can not be virtual\abstract but properties can.

查看更多
\"骚年 ilove
6楼-- · 2019-01-23 23:31

The first sentence of the MSDN documentation answers your question:

The virtual keyword is used to modify a method, property, indexer or event declaration, and allow it to be overridden in a derived class.

http://msdn.microsoft.com/en-us/library/9fkccyh4(v=vs.80).aspx

查看更多
冷血范
7楼-- · 2019-01-23 23:33

No. You'd have to use a property to accomplish what you seem to what to do.

查看更多
登录 后发表回答