.net - c# references a uses b that inherits from c -


i have classes a,b,c this:

//class in console application has reference class library has class b public class {     public void usebobject()     {        b binstance=new b();   // error c defined in assembly not referenced must add  reference contains c...      } }    //class library reference library contains c class public class b : c {    public string name{get;set;} } 

my question if b has reference c , has reference b why need have reference c? not make sense no?

assume class c defined below, defines property called propertyinc

public class c {    public string propertyinc {get;set;} } 

then when use c's public members through b's instance this.

public void usebobject() {    b binstance=new b();    binstance.propertyinc = "whatever"; } 

how come compiler know propertyinc? did gave clue type is? or how compiler chance know whether such property exist ?

well, argue compiler can find using assembly reference of asselblyb(assembly b lives) there no guarantee referenced assembly there in the same path or whatsoever. if not exist fail @ runtime :)

also note compiler allow compile if don't have inheritance relationship, if need access b.cmember gonna add reference assembly c exist.

public class b {    private c cmember{get;set;}//this compile    public string name{get;set;} } 

hope helps.


Comments

Popular posts from this blog

PHPMotion implementation - URL based videos (Hosted on separate location) -

javascript - Using Windows Media Player as video fallback for video tag -

c# - Unity IoC Lifetime per HttpRequest for UserStore -