If you change your design a little, everything will become much simpler. It seems that you are coming from a Java background, so let's see how C # can improve the code.
First, take the FirstName / LastName fields and move them to the base class, as any student enrollment should provide them. Secondly, C # has a function called Properties (here we can use Auto-Implemented Properties, since we donโt need verification), which is basically syntactic sugar for the get / set methods. You can recreate your student class to look like this:
public abstract class Student { public string FirstName { get; set; } public string LastName { get; set; } }
This will allow any instance of the derived class to change the firstname and lastname properties without adding additional methods:
void Main() { var student = new CollegeStudent(); student.FirstName = "Yuval"; }
In general, any instance method of an object that you create does not have to accept its own type in order to mutate itself.
source share