Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

I have been reading Programming Microsoft® Visual C#® 2008: The Language to get a better understanding of C# and what can be done with it. I came across partial classes which I had already encountered from ASP.Net's Page class.

To me it seems that you can do what partial classes do with an abstract class and an overridden one. Obviously one team will be in control of the interface via the abstract methods but you would be relying on each other anyway. And if the goal is collaboration then isn't that what source control and other tools solve.

I am just missing the point to a partial class. Also could someone provide a real world use.

like image 312
uriDium Avatar asked Jun 29 '09 11:06

uriDium


People also ask

What is the benefit of a partial class?

With partial classes, we can spread the definition of a class over multiple files. Partial classes attempt to solve the problem of separation of designer code and implementation code. In Whidbey, partial classes are used to separate these ideas by having the designer code in one file, and the user code in another.

Is a partial class an abstract class?

An abstract class is a class with at least one method defined as abstract. This type of class cannot be instantiated. An abstract class can have one or more abstract methods and properties and other methods and properties like normal classes. A class defined in two or more files is called a partial class.

What is the purpose of partial keyword Where can we apply?

The partial keyword indicates that other parts of the class, struct, or interface can be defined in the namespace. All the parts must use the partial keyword. All the parts must be available at compile time to form the final type. All the parts must have the same accessibility, such as public , private , and so on.

What is the use of partial classes in C Plus Plus?

A partial class is a construct that supports scenarios in which you are modifying one part of a class definition, and automatic code-generating software—for example, the XAML designer—is also modifying code in the same class. By using a partial class, you can prevent the designer from overwriting your code.


2 Answers

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

like image 93
Thorsten Dittmar Avatar answered Oct 21 '22 17:10

Thorsten Dittmar


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

like image 42
Mitch Wheat Avatar answered Oct 21 '22 15:10

Mitch Wheat