c# - Implement interface using concrete implementations of abstract interface members -


is there specific reason why following not possible?

class classofints : ihaveints {     public myint intholder { get; }     // solves use case i'm unsure why necessary     // iint ihaveints.intholder { => intholder; } }  interface ihaveints {     iint intholder { get; } }   class myint : iint {     public int theint { get; } }  interface iint {     int theint { get; } } 

i think above code implements ihaveints since myint implements iint.

is there specific reason why following not possible?

well, short answer is: "because c# specification doesn't allow it". longer answers typically involve amount of speculation in thought process of c# language designers. makes such questions opinion based.

however, did make deliberate choice interface members have implemented precisely declared, , choice why can't that. reason behind choice have special-case read-only properties, because allowing property implemented way writeable property unsafe. allow that, you'd able assign any iint value property expects myint values.

that said, depending on you're trying do, might able use generic type variance support scenario. compile fine:

public class classofints : ihaveints<myint> {     public myint intholder { get; } }  public interface ihaveints<out t> t : iint {     t intholder { get; } } 

declared way, following works fine:

static void m() {     ihaveints<iint> haveints = new classofints(); } 

this semantically equivalent trying do. is, when using interface type, have property of type iint, want implement property member returns value of type myint.


Comments

Popular posts from this blog

angular - Ionic slides - dynamically add slides before and after -

minify - Minimizing css files -

Add a dynamic header in angular 2 http provider -