开发者

When to create Generic and partial class

开发者 https://www.devze.com 2023-02-25 10:23 出处:网络
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).

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).

Thes开发者_StackOverflow中文版e 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:

  1. can create a base class then drive child classes
  2. can create a main class, rest as a inner class
  3. can create a abstract class and rest as different classes
  4. can create 3 partial classes
  5. 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 Employee and Manager where Manager derives from Employee you should use this solution. Employee is 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, SalariedEmployee and ExternalEmployee where Employee is 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.


Partial Class - allows you to produce one class that is implemented in multiple files within the same assembly. As slfan pointed out, this is very useful when part of the class is being generated and part is hand written.

Generic Class - allows you to produce one class that has a single implementation that works a specific way, but does not care what it works on. The perfect example of a this is a List. List doesn't care what is actually in the list, but it knows how to get the first element or remove the third.

Abstract Class - allows you to produce multiple sub classes of the single abstract class. The abstract class depends on the subclasses to implement some of the methods. This differs from a generic class because an abstract class knows something needs to happen, but it does not know what that something actually is, the subclass knows.

0

精彩评论

暂无评论...
验证码 换一张
取 消