15

If I have partial classes in C#, what should the file names be?

The class is called partial class Logic and would exist out of two or maybe three separate files.

João Angelo
  • 56,552
  • 12
  • 145
  • 147
Simon Verbeke
  • 2,905
  • 8
  • 36
  • 55

4 Answers4

24

For a class called Logic, I'd probably go for Logic.something.cs (where "something" is different for each file). This would be in the same style as the partial classes that Visual Studio generates (eg. the .designer.cs files for forms)

Daniel Lo Nigro
  • 3,346
  • 27
  • 29
  • 2
    Indeed, this would differentiate _why_ it's a partial class, and the "why" is the important part of naming conventions. The classic example with partial classes is that one part is generated and the other is manual. So `Logic.generated.cs` and `Logic.cs` would be fairly intuitive. – David Dec 30 '11 at 12:49
  • I like this idea, it makes it easily identifiable from the Designer! – Jonathan Dec 30 '11 at 13:02
2

I would only use Partial Classes if I am generating code (CodeSmith, MyGeneration, Template Files (TT)). Microsoft have done a good job of doing this with generated proxies and datasets etc. As the code will be generated again in teh future it allows you to seperate out logic which you want to include but dont want overwritten.

Standard Based Upon Microsoft .NET Library Standards http://10rem.net/articles/net-naming-conventions-and-programming-standards---best-practices

Pascal Case, no underscores or leading "C" or "cls".

Classes may begin with an "I" only if the letter following the I is not capitalized, otherwise it looks like an Interface.

Classes should not have the same name as the namespace in which they reside. Any acronyms of three or more letters should be pascal case, not all caps. Try to avoid abbreviations, and try to always use nouns. Why: This convention is consistent with the .NET Framework and is easy to read.

Examples Include

  • Widget
  • InstanceManager
  • XmlDocument
  • MainForm
  • DocumentForm
  • HeaderControl
  • CustomerListDataSet

Here is an MS link on naming conventions http://msdn.microsoft.com/en-us/library/xzf533w0(v=vs.71).aspx

Jonathan
  • 2,318
  • 7
  • 25
  • 44
0

I believe that you can choose the name of the files as you want to, as long the partial classes are placed in the same namespace!

Pang
  • 9,564
  • 146
  • 81
  • 122
aweis
  • 5,350
  • 4
  • 30
  • 46
-2

Whatever you want but:

  • Don't use partial classes. If your class is that big, better to split it into separate classes somehow.
  • If you insist on partial classes, split them logically, so you could have Logic_Boolean.cs, etc.
Ray
  • 45,695
  • 27
  • 126
  • 169
  • 1
    As a comment on your first argument: I'm not that good with classes yet, so to keep it organised I'd like to split it in multiple files :) – Simon Verbeke Dec 30 '11 at 12:51
  • @SimonVerbeke fair enough. But something to keep in mind. – Ray Dec 30 '11 at 12:54
  • another reason is that I'm working with Unity3D, and its scripts require that any logic you want to execute is placed in some predetermined methods. I wanted to seperate those: `Update()` in one file, `OnGUI()` in another, ... It's also a rather small project with a lot of code that doesn't return anywhere else. – Simon Verbeke Dec 30 '11 at 13:01
  • Partial classes have their place. I use them to add attributes to my entity framework generated classes so I don't have to add in the attributes every time I regenerate the code – reggaeguitar Nov 14 '14 at 20:30
  • 2
    What if you have an API class that has hundreds of methods on it? Would it not make sense to split similar API methods into their own partial file for easier maintenance? How would you handle that? Hopefully not with multiple inheritance – crush Feb 19 '15 at 20:51