简体   繁体   English

C#中两个字符串值的数据类型的相等比较

[英]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. 是否可以检查第一个变量的类型是否等于secondVariable的转换值。 When I declared firstVariable as int it doesn't mean it is always int type. 当我将firstVariable声明为int时,并不意味着它始终是int类型。 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. 我的意思是,我知道一个变量的类型,另一个变量的类型是字符串,我想比较firstvariable的数据类型和secondVariable的值数据类型的相等性。

Is Convert.ChangeType will anyway help in the above scenario? 在上述情况下, Convert.ChangeType是否会有所帮助?

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? 谁说“ 123”不仅仅是包含数字1、2和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. 或者,如果您有另一个变量作为字符串,只需调用ToString()将其转换为字符串,然后在字符串级别进行比较。

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. 然后,对于每种类型,您都可以尝试将字符串转换为它,这样您就可以找到它实际是什么类型。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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