My C# Naming Conventions For Partial Class Files

Following up on a previous post, this time I'll give you my naming conventions for partial class files.

There are two main reasons for me to break a class definition into more than one file. The main one is when I have inner classes and the other is when the file is getting too big.

How should I name the files? The usual tendency is to use a “.” as separator. This looks nice until you came across something like this:

public class MyComponent

{

    private class Activation

    {

        // Class implementation.

    }

 

    private class Deployment

    {

        // Class implementation.

    }

 

    #region Event Handling

 

    // Event handling code.

 

    #endregion

}

If I would extract the inner classes and use the “.” as separator, I would end up with the following list of files:

  • MyComponent.Activation.cs
  • MyComponent.cs
  • MyComponent.Deployment.cs

And, what about the long event handling code? Should I do the same? If so, I will end up with the following list of files:

  • MyComponent.Activation.cs
  • MyComponent.cs
  • MyComponent.Deployment.cs
  • MyComponent.EventHandling.cs

This doesn’t look very nice, does it?

So, here is my proposal:

  • “-“ separator for code from the top class
  • “+” separator for inner classes

This way, I end up with the following list of files:

  • MyComponent.cs
  • MyComponent-EventHandling.cs
  • MyComponent+Activation.cs
  • MyComponent+Deployment.cs

And this looks a lot nicer.

2 Responses to My C# Naming Conventions For Partial Class Files