c# - Can I Override with derived types? -


as far know not possible following in c# 2.0

public class father {     public virtual father somepropertyname     {                 {             return this;         }     } }  public class child : father {     public override child somepropertyname     {                 {             return this;         }     } } 

i workaround problem creating property in derived class "new", of course not polymorphic.

public new child somepropertyname 

is there solution in 2.0? features in 3.5 address matter?

this not possible in .net language because of type-safety concerns. in type-safe languages, must provide covariance return values, , contravariance parameters. take code:

class b {     s get();     set(s); } class d : b {     t get();     set(t); } 

for get methods, covariance means t must either s or type derived s. otherwise, if had reference object of type d stored in variable typed b, when called b.get() wouldn't object representable s -- breaking type system.

for set methods, contravariance means t must either s or type s derives from. otherwise, if had reference object of type d stored in variable typed b, when called b.set(x), x of type s not of type t, d::set(t) object of type did not expect.

in c#, there conscious decision disallow changing type when overloading properties, when have 1 of getter/setter pair, because otherwise have inconsistent behavior ("you mean, can change type on 1 getter, not 1 both getter , setter? why not?!?" -- anonymous alternate universe newbie).


Comments

Popular posts from this blog

c++ - How do I get a multi line tooltip in MFC -

asp.net - In javascript how to find the height and width -

c# - DataTable to EnumerableRowCollection -