Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

When should I use the Partial keyword?

I was reading the MSDN article on the Partial keyword, and this part caught my eye:

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.

[...]

All the parts that specify a base class must agree, but parts that omit a base class still inherit the base type. Parts can specify different base interfaces, and the final type implements all the interfaces listed by all the partial declarations. Any class, struct, or interface members declared in a partial definition are available to all the other parts. The final type is the combination of all the parts at compile time.

I had two questions regarding this concept:

  • Firstly, it seems that this is a way to bypass the lack of multiple inheritance in C# (aside from interfaces, of course). Are there any repercussions to doing so, aside from normal multiple inheritance issues, such as the Diamond Problem? Basically, just because I can, does it mean I should?

  • Secondly, when exactly should I split up files? Just by reading this, it feels like I should be able to declare a nested class in its own file, and partial it together with the containing class, and thereby improve readability. Is this the point of Partial, or should it only be used as described in the above article?

like image 228
Kyle Baran Avatar asked Jan 19 '13 20:01

Kyle Baran


People also ask

When would you use a partial class?

Use of Partial ClassesIf you are working on a bigger project, splitting the files over different classes helps developers work on the same project simultaneously. If you are working on an automatically generated source, then the code can be added to the class without regenerating the source file.

What is the point of partial classes?

Partial classes are portions of a class that the compiler can combine to form a complete class. Although you could define two or more partial classes within the same file, the general purpose of a partial class is to allow the splitting of a class definition across multiple files.

What is the purpose of partial keyword in VB net?

The partial keyword indicates that other parts of the class can be defined in the same namespace. All the parts should use the partial keyword. All the parts should be available at compile time to form the final type. All the parts should 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.


1 Answers

Firstly, this has nothing to do with multiple inheritance in C#. It simply allows you to split a class' implementation between files. A regular class in C# can implement multiple interfaces as well, so you're not gaining anything by using partial classes.

Secondly, partial classes are the most useful when part of a class' implementation is generated by some tool and the other portion is written by a developer. This allows you to re-generated the generated portion of the code without losing the hand coded portion of the implementation.

like image 147
Justin Niessner Avatar answered Sep 28 '22 07:09

Justin Niessner