Pregunta

Is there a possible way that let me compare strings without calling the op_Equality function is mscorlib? For example:

string s1 = "hi";
if(s1 == "bye")
{
Console.Writeline("foo");
}

Compiles to:

IL_0003: call bool [mscorlib]System.String::op_Equality(string, string)

And looking at op_Equality at mscorlib from GAC it calls another method Equals(string, string) Code:

public static bool Equals(string a, string b)
{
   return a == b || (a != null && b != null && string.EqualsHelper(a, b));
}

it uses the op code bne.une.s to compare those strings.

Now back at my question, how can I compare 2 strings without calling any function from the mscorlib like the method Equals does.

¿Fue útil?

Solución

Now back at my question, how can I compare 2 strings without calling any function from the mscorlib like the method Equals does.

You can't - at least not without writing your own string comparison method from scratch.

At some point, you'd have to at least call String.Equals(a,b) (in order to have the private EqualsHelper method called, which does the actual comparison), which is also defined in mscorlib.

You could call Equals directly if you wish to avoid the operator call, though:

string s1 = "hi";
if (string.Equals(s1, "bye"))
{
    Console.WriteLine("foo");
}

This would avoid the call to op_Equality, bypassing it and calling Equals directly.

That being said, there is really no reason to avoid calling op_Equality on strings in the first place...

Otros consejos

It seems like you are asking for a string comparison function. Strings are basically arrays of characters that you can index in a similar fashion. Here is a simple string comparison algorithm.

public static bool Equals(string left, string right)
{
    if (ReferenceEquals(left, null)) return ReferenceEquals(right, null);
    if (left.Length != right.Length) return false;
    for (int i = 0; i < left.Length; i++)
    {
        if (left[i] != right[i])
            return false;
    }
    return true;
}

Having shown you that, there is no reason to avoid the mscorlib implementation, since my example makes several calls to mscorlib.

This is another way to compare:

        string s1 = "Hello";

        bool same = s1.CompareTo("Hello") == 0;
        bool different = s1.CompareTo("GoodBye") != 0;

        Console.WriteLine(same);
        Console.WriteLine(different);

In this case, both should report "True" to the console.

Another way:

    private static bool CheckEqual(string s1, string s2)
    {
        char[] c1 = (s1 == null) ? new char[0] : s1.ToCharArray();
        char[] c2 = (s2 == null) ? new char[0] : s2.ToCharArray();

        if (c1.Length != c2.Length) { return false; }

        for (int i = 0; i < c1.Length; i++)
        {
            if (!c1[i].Equals(c2[i])) { return false; }
        }

        return true;
    }

Given that the String class is provided by mscorlib, there is absolutely no way to compare two strings without using mscorlib. Even just using the indexer to get each character calls into mscorlib.

The only way to avoid mscorlib is to not use the .Net runtime, but that's pretty difficult if you want to use C#.

Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top