.net - Member of type multiple interfaces -
i trying declare property (or function matter) of type satisfies several interfaces. i'm assuming can't done in c# or vb. question is, if makes sense define type implements multiple interfaces, why cant define member of such? ex. can this
interface ibar { string barmember; } interface ifoo { string foomember; } class foobar : ibar, ifoo { public string barmember{get;set;} public string foomember{get;set;} }
so why cant this
class someclass { public {ibar, ifoo} foobarmember {get;set;} }
in case foobar satisfy someclass.foobarmember?
the reason need need member satisfies interface requirements. dont care actual concrete class is. know can combine both of interfaces creating new interface combines both of them, why should have that?
ok, understood question: generics rescue!
class someclass<t> t : ifoo, ibar { public t foobarmember { get; set; } }
now, foobarmember
of type t
, type must implement both ifoo
, ibar
.
consider this:
class : ibar { public string somemember { get; set; } } class b : ifoo { public string somemember { get; set; } } class c : ifoo, ibar { public string somemember { get; set; } }
three classes, a
implements ibar
, b
implements ifoo
, c
implements both. now, take following code:
someclass<a> aa = new someclass<a>(); // doesn't compile someclass<b> bb = new someclass<b>(); // doesn't compile someclass<c> cc = new someclass<c>(); // works fine
this means can so:
someclass<c> cc = new someclass<c>(); cc.foobarmember = new c();
Comments
Post a Comment