A rule to direct use of string.IsNullOrEmpty?

Jun 24, 2011 at 5:35 AM

One of the FXCOP code optimisations is that code like

      if (stringVar == string.Empty)
      {
        Console.WriteLine("stringVar is blank");
      }

should become

      if (string.IsNullOrEmpty(stringVar))
      {
        Console.WriteLine("stringVar is blank");
      }

Should StyleCop include this as a rule for exmpty string comparisons?

Coordinator
Jun 24, 2011 at 8:11 AM

Thats a different check though right? 

string.IsNullOrEmpty is the same as:

if (stringVar == string.Empty || stringVar == null)

which is not the same as:

(stringVar == string.Empty)

Jun 24, 2011 at 7:54 PM

I agree that the check is different in that null value would now be included as well as a blank in the test, however this is the change that FXCOP instructs to be done.

Coordinator
Jun 24, 2011 at 7:57 PM
I think fxcop 'suggests' it but from a 'styling' point of view stylecop will not mind either of them.

A.



On 24 Jun 2011, at 20:54, "TATWORTH" <notifications@codeplex.com> wrote:

From: TATWORTH

I agree that the check is different in that null value would now be included as well as a blank in the test, however this is the change that FXCOP instructs to be done.