简体   繁体   中英

Equality comparison of the datatype of two strings value in C#

This is a weird requirement I have. I know even my question is quite confusing. Here is what I want to know.

I've two string variables. I need to do equality comparison of the datatype of the underlying value in the string variables. For ex.

string firstVariable = "123"; // It contains integer value. i.e. I can convert it to integer value
string secondVariable = "string" // It contains string value.

Now I need to compare whether datatype of the underlying values of these two strings are same. How can I do this?

Update: Thanks to all for the clarifications and answers. How about if I know the type of one variable? For ex:

int firstVariable;
string secondVariable = "123". 

Is this possible to check whether the type of the first variable equals to converted value of the secondVariable. When I declared firstVariable as int it doesn't mean it is always int type. What I mean here is, I know the type of one variable and other variable is string and I want compare equality of the datatypes of firstvariable and value datatype of the secondVariable.

Is Convert.ChangeType will anyway help in the above scenario?

I know this is silly question, but out of curiosity in the language feature exploring, I wanted to know this.

There's no such thing as the "underlying data type".

Who's to say that "123" isn't just a string containing the digits 1, 2 and 3? Once you've converted a value to a string, any information about the value you converted from - including its type - is lost.

Written from my head so there may (will) be errors:

class StringTypeEqualityComparer : IEqualityComparer<string, string>
{
    public bool Equals(string x, string y)
    {
        int tempInt;
        if (Int32.TryParse(x, out tempInt) && (Int32.TryParse(y, out tempInt))
            return true;

        bool tempBool;
        if (Boolean.TryParse(x, out tempBool) && Boolean.TryParse(y, out tempBool))
            return true;

        float tempFloat;
        if (Single.TryParse(x, out tempFloat) && Single.TryParse(y, out tempFloat))
            return true;

        // And whatever other types you want to compare...

        return false;

        // But what if two regular strings should also evaluate to equal?
    }
}

Why do you need to compare them by the underlying data type ?. Just compare them as string. Or if you have another variable as string, just convert it to a string by calling ToString() and make the comparison on the string level.

To do it you have to have a limited list of possible data types and be sure that type of your string content is not ambiguous. Then, for each type, you can try to convert string to it, so you will be able to find what type it actually was.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM