0

I want to add a Custom rule for avoiding the '==' operator inside a method in a class. For example in the below method i need to avoid 'str1==str2' with string.Compare(str1, str2,StringComparison.Ordinal);.So I need to check this types of code are appear in any of the method

public void StringTest2()
    {
        string str1 = "Hello";
        string str2 = "HELLO";
        if (str1 == str2)
        {
        }
    }
Jameel
  • 81
  • 5
  • 2
    Out of interest, *why* would you want to avoid this, insisting on a far more verbose version which achieves the same result? – Jon Skeet Nov 02 '12 at 10:32
  • But I need to avoid the == operator. How to check this type of code is present inside the method? – Jameel Nov 02 '12 at 10:44
  • "I need to" is in no way an answer to my question of *why* you think you need to. – Jon Skeet Nov 02 '12 at 10:47
  • I think string.compare is the best method for comparing a string than == . – Jameel Nov 02 '12 at 10:52
  • is there is any globalization problem using '==' operator in the code for comparing a string? – Jameel Nov 02 '12 at 10:56
  • No. The `==` operator performs an ordinal comparison. See http://msdn.microsoft.com/en-us/library/system.string.op_equality.aspx – Jon Skeet Nov 02 '12 at 10:57

2 Answers2

1

Just say no.

The string == operator already performs an ordinal comparison, and is considerably more readable IMO than insisting on using string.Compare.

Even if you did want an ordinal string comparison explicitly, I'd suggest using string.Equals(string, string, StringComparison) instead of Compare.

Jon Skeet
  • 1,421,763
  • 867
  • 9,128
  • 9,194
  • Your answer is a solution.But How to add a custom rule for checking 'string == operator' in the method? – Jameel Nov 02 '12 at 11:45
  • @Jameel: I suspect it may not even be possible, but I don't know much about FxCop. I know enough to know I would violently protest against such a rule being enforced on me though :) – Jon Skeet Nov 02 '12 at 11:55
0
**The below code checks both the assignment and equal to operator in an assembly**


public override ProblemCollection Check(Member member)
            {
                var method = member as Method;
                if (method == null)
                    return null;
                if (method.Instructions.Count > 0)
                {
                    foreach (var instruction in method.Instructions)
                    {
                        if (instruction != null)
                            if ( instruction.OpCode == OpCode.Ceq)
                            {
                                var resolution = GetResolution(member.Name.Name);
                                var problem = new Problem(resolution, member)
                                                  {
                                                      Certainty = 95,
                                                      FixCategory = FixCategories.Breaking,
                                                      MessageLevel = MessageLevel.Warning
                                                  };
                                Problems.Add(problem);
                            }
                    }
                }
                return base.Problems;
            }
Jameel
  • 81
  • 5