c# - Unable to cast object of type 'demo.Class1' to type 'demo.Class2' -


my object cannot converted other object please help

it shows error:

unable cast object of type 'demo.class1' type 'demo.class2'.

public class class1 {     public int a;     public string b;      public void hello()     {         console.writeline("abc");     } } public class class2 : class1 {     public string d; }   class program {     static void main(string[] args)     {         class1 c = new class1();         c.a = 1;         c.b = "a";          class2 c2 = (class2)c;      } } 

please help

the class 2 subtype of class 1. means can cast class2 objects class1 type not other way around.

to clarify less abstract names: class1 animal , class2 dog. can dog animal, animal not dog.


Comments

Popular posts from this blog

php - Permission denied. Laravel linux server -

google bigquery - Delta between query execution time and Java query call to finish -

python - Pandas two dataframes multiplication? -