
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.
This C# article reviews common warnings from FxCop. It indicates how you can fix them.
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.
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 LanguageHere we explore the unnecessary initialization warning in FxCop. Reference types are initialized to null if they are member variables, and int values are automatically initialized to 0. FxCop should complain, and it does sometimes. 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;
}Here we look at casting performance FxCop warnings. I have seen this error in example code in books. The key here is that the as operator will return null if it fails. Here we look at the slow code that triggers this warning, and then the faster code that avoids multiple casts. In FxCop, this is error CA1800, DoNotCastUnnecessarily.
Is Cast Example As Cast ExampleUnnecessary 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;
}
}
}Here we solve the unnecessary casting warning. The next block here is how to solve 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)
{
// ...
}
}
}
Here we note some of the different ways you can test empty strings. To solve this, 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. Microsoft encourages it, and it makes for simpler code. This relates to warning CA1820, TestForEmptyStringsUsingStringLength.
string.IsNullOrEmpty Method String Length Property
Here 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. Every character in your C# code can have an impact far greater than you might think. FxCop is currently included in the Windows SDK.
.NET Framework Info