amy's lab

August 5, 2008

C# Partial Class – Code Generation

Filed under: C# — Tags: , — amy @ 19

I found C# Partial Class to be very useful in code generation.  I could create a new class with the same class name and add my custom methods to that class, without having to worry about the code generator erasing what I wrote.  Before, code generators usually put some special tags in the code to identify an area where only the code generator can modify.  This is fine, but sometimes, either the code generator or the developer would make mistakes and make the code uncompilable.  With C# Partial Class, I could have something like this:

// MyClassGen.cs - this is generated.
// The file ends with "Gen" to denote a generated class.
public partial class MyClass
{
    // put generated code here
}

// MyClass.cs - this is where I add my custom methods
public partial class MyClass
{

}

This probably would be the only time that I would use C# partial class.

June 21, 2008

C# region / endregion

Filed under: C#, How Not To Program — Tags: — amy @ 26

Use #region sparingly. Do not use #region inside a method.

From MSDN website:

#region lets you specify a block of code that you can expand or collapse when using the outlining feature of the Visual Studio Code Editor.

When I write code, why should I care how it will be displayed in the Visual Studio Code Editor? Again, like Partial Class, I think #region is unnecessary and open to abuse. With #region, I could write code like this:

class BigClass
{
    public void BigMethod(int val)
    {
        #region Perform Logic on X
        // begin to write 100 lines of code
        #endregion

        #region Perform Logic on Y
        // begin to write 100 lines of code
        #endregion

        #region Perform Logic based on val
        switch (val)
        {
            case 1:
                #region Handle Case 1
                // write 100 lines of code to handle case 1
                #endregion
                break;
            case 2:
                #region Handle Case 2
                // write 100 lines of code to handle case 2
                #endregion
                break;
            case 3:
                #region Handle Case 3
                // write 100 lines of code to handle case 3
                #endregion
                break;
            default:
                #region Handle the default case
                // write 100 lines of code to handle the default case
                #endregion
                break;
        }
        #endregion
    }
}

As you can see, the method could get really long. If used with Partial Class, the class could get really big. It would be a nightmare maintaining code like this.

Use #region sparingly. Do not use #region inside a method.

C# Partial Class

Filed under: C#, How Not To Program — Tags: — amy @ 54

Do not use Partial Class.

From MSDN website:

It is possible to split the definition of a class or a struct, or an interface over two or more source files. Each source file contains a section of the class definition, and all parts are combined when the application is compiled.

So I could have something like this:

// BigClass1.cs
public partial class BigClass
{
    // put lots of methods, properties, etc here.
}

// BigClass2.cs
public partial class BigClass
{
    // put lots of methods, properties, etc here.
}

...

// BigClass50.cs
public partial class BigClass
{
    // put lots of methods, properties, etc here.
}

Obviously, it is really hard to maintain code like this. It’s hard to see what methods, properties, etc. are in a class. Although you can use the method drop down in Visual Studio to see all methods, it is still hard to find a particular method when the drop down contains many methods. What happens if you don’t have Visual Studio around and have to use a simple text editor?

Do not use Partial Class.

Blog at WordPress.com.