Is it valid and legal and to down-cast in the following case:
public interface IA {
string Property1 {get;}
}
public class B {
public string Name {get;set;}
}
// class A doesn't have it's own fields, all IA methods/properties are implemented through B methods
public class A:B,IA
{
public string Property1
{
get {return this.Name}
}
}
.....
B b = new B();
A a = (A)b; // will it work ?
Or is it better to use composition/aggregation?
public class A :IA
{
private B b;
....
public string Property1 {get {return b.Name;} }
public A(B b) {this.b = b}
}
....
B b= new B();
A a = new A(b);