TheDeveloperBlog.com


C# FxCop Performance Warnings

FxCop has flagged some warnings. We can examine some FxCop warnings related to performance and fix them. FxCop is a static analysis tool by Microsoft that helps you improve your code.

Tip: It is useful to run FxCop on your projects until you become familiar with all its corrections. You have the right to remain silent.


Intro. First, FxCop performs analysis of the intermediate language to notify you of possible or definite flaws in your code. Detailed analysis of functions, and profiling of them, can be useless.

However: If this analysis can bring you a more nuanced understanding of the code, it is worthwhile.

Intermediate Language

Initialization. We explore the unnecessary initialization warning. Reference types are initialized to null if they are member variables. On the other hand, value types are automatically initialized to 0.

Info: FxCop should complain, and sometimes it does. In FxCop this is error CA1805, DoNotInitializeUnnecessarily.

Example of unnecessary initialization: C#

// These assignments are not necessary.
class Box
{
    int _cat = 0;
    string _make = null;
}


Casts. Next we look at casting performance FxCop warnings. The key here is that the as-operator will return null if it fails. We see the slow code that triggers this warning, and then the faster code that avoids multiple casts.

IsAs

Info: In FxCop, this is error CA1800, DoNotCastUnnecessarily. In the example after this one we fix it.

Unnecessary casts: C#

class Program
{
    class CertainClass
    {
    }

    static void Main()
    {
	// This causes extra casting.
	object exampleObject = new CertainClass();
	if (exampleObject is CertainClass)
	{
	    CertainClass certainObject = exampleObject as CertainClass;
	}
    }
}

Next, we solve the unnecessary casting warning. The next block solves the "DoNotCast" warning by using "as" to cast and then testing for null. This eliminates one cast in the success case, and doesn't cost much if the cast fails.

Unnecessary casts fix: C#

class Program
{
    class CertainClass
    {
    }

    static void Main()
    {
	// This is more efficient.
	object exampleObject = new CertainClass();
	CertainClass certainObject = exampleObject as CertainClass;
	if (certainObject != null)
	{
	    // ...
	}
    }
}


Empty strings. There are many ways you can test empty strings. I suggest that you use the string.IsNullOrEmpty method, which is a static function you call using the string class. It returns true or false depending on whether the string is empty.

Tip: Microsoft encourages it, and it leads to simpler code. This relates to warning CA1820, TestForEmptyStringsUsingStringLength.

IsNullOrEmptyString Length

Summary. We looked at some performance warnings you might see in FxCop. Start using Microsoft FxCop every few days on your programs. It will teach you a lot and show you ways to analyze and improve your code.

Note: Every character in your C# code can have an impact far greater than you might think. FxCop is included in the Windows SDK.