What are the benefits to using a partial class as opposed to an abstract one?

允我心安 提交于 2019-12-01 16:11:05

Partial classes have nothing to do with object inheritance. Partial classes are just a way of splitting the source code that defines a class into separate files (this is for example done when you create a new form in your Windows Forms application - one file is "your" code, another file .designer.cs contains the code that VS2008 manages for you).

A good usage example is when one side of the partial class is generated (such as an ORM)

The great thing about a partial class is that you can take an existing class and add on to it. Now this sounds a lot like inheritance, but there are a lot of things that inheritance can't do that partial classes will.

Here's one think about the Linq to SQL classes generated for you. They are autogenerated meaning you shouldn't modify them. Without a partial class, you can't attach an interface. You could create a new class and derive that from the Linq to sql class, but that really doesn't get you anything because you can't upcast the linq to sql class to your class with the interface.

Partial classes should be restricted to using with auto-generated code, where the other code cannot be modified. Using it as a substitute for inheritance or adding functionality are not best practices.

If you have a large class, its already wrong. Code should be refactored into multiple "real" classes instead of multiple files. Large classes in general signifies the class is doing too many things and violates SRP (Single Responsibility Principle).

Partial class are now used heavily in ASP.Net to allow two source files the mark-up based example.aspx and the code based example.aspx.cs so that methods and variable defined in each are visible to each. in the example.aspx

<custom:exampleControl id="exampleCntr" property="<%#getProperty()%>" />

in the example.aspx.cs

private object GetProperty(){ // called from aspx
    return DateTime.Now;
}

private void DoStuff(){
    ExampleControl c = exampleCntr; //exampleCntr is defined in aspx.
}

The bi-directional nature of this cannot be recreated with abstract classes.

It sounds like your question is what the difference is between

partial class Foo
{
  PART ONE
}
partial class Foo
{
  PART TWO
}

and

astract class FooBase
{
  PART ONE
}
partial class Foo : FooBase
{
  PART TWO
}

While they may appear somewhat similar, and in some cases the latter construct could be used in place of the former, there are at least two problems with the latter style:

-1- The type FooBase would likely have to know the identity of the concrete type which was supposed to derive from it, and always use variables of that type, rather than of type FooBase. That represents an uncomfortably-close coupling between the two types.

-2- If type Foo is public, type FooBase would have to also be public. Even if all the constructors of FooBase are internal, it would be possible for outside code to define classes which derived from FooBase but not Foo; constructing instances of such classes would be difficult, but not impossible.

If it were possible for a derived type to expand the visibility of a base type, these issues wouldn't be overly problematical; one would regard FooBase as a "throwaway" identifier which would appear exactly twice: once in its declaration, and once on the declaration line for Foo, and figure that every FooBase would be a Foo in disguise. The fact that FooBase could not use Foo instance members on this without a typecast could be irksome, but could also encourage a good partitioning of code. Since it isn't possible to expand the visibility of a base type, however, the abstract-class design seems icky.

Purpose of partial classes is to allow a class's definition to span across multiple files. This can allow better maintainability and separation of your code.

We use partial classes to split up our larger classes. That way it's easier to check out a part of the code with Sourcesafe. This limits the cases where four developers need to access the same file.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!