首页 > 代码库 > C# virtual 这个关键字
C# virtual 这个关键字
The virtual
keyword is used to modify a method, property, indexer, or event declaration and allow for it to be overridden in a derived class. For example, this method can be overridden by any class that inherits it:
public virtual double Area() { return x * y; }
You cannot use the virtual
modifier with the static
, abstract, private
, or override
modifiers. The following example shows a virtual property:
class MyBaseClass { // virtual auto-implemented property. Overrides can only // provide specialized behavior if they implement get and set accessors. public virtual string Name { get; set; } // ordinary virtual property with backing field private int num; public virtual int Number { get { return num; } set { num = value; } } } class MyDerivedClass : MyBaseClass { private string name; // Override auto-implemented property with ordinary property // to provide specialized accessor behavior. public override string Name { get { return name; } set { if (value != String.Empty) { name = value; } else { name = "Unknown"; } } } }
C# virtual 这个关键字
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。