Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to extend a Class in C#? [closed]

Tags:

I have my own Object, and I'd like to extend it, saving data from a person and adding new info.

So the code would be:

public class Student : Person {     public string code { get; set; } } 

but when I try to init it and add the new value:

Person person = new Person("Paul", "Catch"); Student student = (Person)person; student.code = "1234"; 

I got System.InvalidCastException: Unable to cast object of type 'MyLayer.Person' to type 'Student'.

Am I missing some point?

EDIT: maybe I wrong putting that Person class. You must suppose it become from a DB as object, such as Person person = new MyPersons().First();

So I won't to populate the new one with properties one by one, just extend one property thanks to the new object that extend the old one.

like image 315
markzzz Avatar asked Mar 06 '13 15:03

markzzz


People also ask

How do you extend classes?

Definition and UsageThe extends keyword extends a class (indicates that a class is inherited from another class). In Java, it is possible to inherit attributes and methods from one class to another. We group the "inheritance concept" into two categories: subclass (child) - the class that inherits from another class.

How do you extend a class in C#?

You can use extension methods to extend a class or interface, but not to override them. An extension method with the same name and signature as an interface or class method will never be called. At compile time, extension methods always have lower priority than instance methods defined in the type itself.

When should you extend a class?

You extend a class when you want the new class to have all the same features of the original, and something more. The child class may then either add new functionalities, or override some funcionalities of the parent class.

Why do we use extension methods in C#?

In C#, the extension method concept allows you to add new methods in the existing class or in the structure without modifying the source code of the original type and you do not require any kind of special permission from the original type and there is no need to re-compile the original type.


1 Answers

You cannot convert a Person to Student directly.

Inheritance in OOP comes with hierarchy level, i.e. you can cast the derived class to base class, but the opposite is not possible. You cannot cast base class to derived class

One possible solution is :

Create Student from Person using constructor overload.

public class Person {     public string FName { get; set; }     public string LName { get; set; }      public Person(string fname, string lname)     {         FName = fname;         LName = lname;     } }  public class Student : Person {     public Student(Person person, string code)         : base(person.FName, person.LName)     {         this.code = code;     }     public Student(Person person)         : base(person.FName, person.LName)     {      }      public string code { get; set; } }    static class Program {     static void Main(string[] args)     {         Person person = new Person("Paul", "Catch");          // create new student from person using          Student student = new Student(person, "1234");          //or         Student student1 = new Student(person);         student1.code = "5678";          Console.WriteLine(student.code); // = 1234         Console.WriteLine(student1.code); // = 5678     } } 
like image 73
Parimal Raj Avatar answered Oct 26 '22 02:10

Parimal Raj