c# - Widening Conversion between Reference types -


i know in widening conversion, destination data type can hold value provided source data type. can understand when convert value type, can't between reference types. question arises follows scenario:

public class person {     public string name { get; set; } }  public class employee : person {     public string surname { get; set; } } static void main(string[] args) {     employee emp = new employee { name = "elvin", surname = "mammadov" };     person prs = emp; } 

as shown, person class ancestor class of empoyee. in previouse code, create reference of emplyee class, , convert employee object person. @ point, lose surname data field , value.

i want know:

  1. is countary can hold value expression?

  2. why conversion reference type direct or indirect ancestor class or interface widening conversion?

thanks reply

it called 'widening conversion' because you're becoming less specific regard references type, you're widening set of potential types. when have employee reference know can use functionality specific class, when have person reference same object know can use functionality contained in person class. if want use employee specific functionality prs reference you'll first have cast reference employee reference , if prs isn't of type employee (perhaps it's of type customer inherits person) invalidcastexception.

the surname property not go away when you're dealing person reference. cannot access because not contained within person class. basically, if have reference of base classes type, able access properties/methods contained within base class. doesn't matter employee, have person reference, object treated accordingly.

the conversion not useful in example. conversion useful when you're attempting work many child classes in generic manner. example, may have person class. in addition have employee, customer, , consultant classes inherit it. lets suppose have store object , want names of every person in store. simplest way solve problem have list<person> people in store. because employee, customer, , consultant inherit person do

peopleinmystore.add(myemployee); peopleinmystore.add(mycustomer); peopleinmystore.add(myconsultant); 

then later on can like;

foreach (person p in peopleinmystore) {      console.writeline(p.name); } 

this concept referred 'polymorphism' , can read here http://en.wikipedia.org/wiki/polymorphism_(computer_science)

my example contrived, 1 of main reasons use inheritance. give real example have test code have class called apitestsuite have dozen classes inherit of form specificapitestsuite. project builds command line executable, invoke api parameter (api=specificapiname) can like;

 apitestsuite tests;  if (args[0] == "api1")      tests = new api1testsuite();  else      tests = new api2testsuite();  tests.runtests(); 

Comments

Popular posts from this blog

java - Run a .jar on Heroku -

java - Jtable duplicate Rows -

validation - How to pass paramaters like unix into windows batch file -