Let’s say I have 3 classes, in these classes I have 2 common function and 1 different, same with the members (some are same. some are different).
These classes object are created in foreach loop and in one iteration only one of the class object is created.
I’m looking for better approach for creation of class:
- can create a base class then drive child classes
- can create a main class, rest as a inner class
- can create a abstract class and rest as different classes
- can create 3 partial classes
- can create a generic class
I just want to know the better approach, in case I can create the three partial classes or any generic class? – then please explain.
I am using C# 3.0
1. can create a base class then drive child classes
Good candidate. However, look at 3. below. If you have classes
EmployeeandManagerwhereManagerderives fromEmployeeyou should use this solution.Employeeis not abstract.2. can create a main class, rest as a inner class
Bad candidate. Inner classes is simply a way to scope classes. Most of the time you should avoid public inner classes, and then inner classes simply becomes an implementation detail of your class.
3. can create a abstract class and rest as different classes
Good candidate. Same as 1. except your base class cannot be instantiated. If your base class is “incomplete” and need to be derived to be fully specified you should choose this solution. If your classes are
Employee,SalariedEmployeeandExternalEmployeewhereEmployeeis the base class this solution is right. An employee is not correctly described unless you know if the employee is salaried or external.4. can create 3 partial classes
Bad candidate. A partial class is a way to split the source code of your class into several source files.
5. can create a generic class
Probably a bad candidate. Generic classes are used to create a single (generic) class that implements the same behavior with varying type parameter. You describe that your classes have different methods, and a generic class does not have “different methods” depending on the type parameter.