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

C# C中两个字符串值的数据类型的相等性比较#,c#,comparison,types,equality,C#,Comparison,Types,Equality,这是一个奇怪的要求,我有。我知道连我的问题都很让人困惑。这是我想知道的 我有两个字符串变量。我需要在字符串变量中对基础值的数据类型进行相等比较。例如 string firstVariable = "123"; // It contains integer value. i.e. I can convert it to integer value string secondVariable = "string" // It contains string value. 现在我需要比较这两个字符串

这是一个奇怪的要求,我有。我知道连我的问题都很让人困惑。这是我想知道的

我有两个字符串变量。我需要在字符串变量中对基础值的数据类型进行相等比较。例如

string firstVariable = "123"; // It contains integer value. i.e. I can convert it to integer value
string secondVariable = "string" // It contains string value.
现在我需要比较这两个字符串的基础值的数据类型是否相同。我该怎么做

更新:感谢大家的澄清和回答。如果我知道一个变量的类型呢? 例如:

这是否可以检查第一个变量的类型是否等于第二个变量的转换值。当我将firstVariable声明为
int
时,并不意味着它总是
int
类型。我这里的意思是,我知道一个变量和另一个变量的类型是字符串,我想比较第一个变量的数据类型和第二个变量的值数据类型的相等性

在上述场景中,
Convert.ChangeType
会有帮助吗


我知道这是一个愚蠢的问题,但出于对语言功能探索的好奇,我想知道这一点。

没有“底层数据类型”这样的东西


谁能说“123”不仅仅是一个包含数字1、2和3的字符串?将值转换为字符串后,有关转换后的值的任何信息(包括其类型)都将丢失。

没有“基础数据类型”这样的东西


谁能说“123”不仅仅是一个包含数字1、2和3的字符串?将值转换为字符串后,有关转换值的所有信息(包括其类型)都将丢失。

为什么需要按基础数据类型进行比较?。只需将它们作为字符串进行比较。或者,如果有另一个变量作为字符串,只需调用
ToString()
将其转换为字符串,并在字符串级别进行比较。

为什么需要按基础数据类型进行比较?。只需将它们作为字符串进行比较。或者,如果您有另一个变量作为字符串,只需通过调用
ToString()
将其转换为字符串,并在字符串级别进行比较。

为此,您必须有一个可能的数据类型的有限列表,并确保字符串内容的类型没有歧义。然后,对于每种类型,您都可以尝试将字符串转换为它,这样您就可以找到它实际上是什么类型。

要做到这一点,您必须有一个可能的数据类型的有限列表,并确保字符串内容的类型不含糊。然后,对于每种类型,您可以尝试将字符串转换为它,这样您就可以找到它实际上是什么类型。

是从我的头脑中写出来的,所以可能(将)有错误:

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?
    }
}
类StringTypeEqualityComparer:IEqualityComparer
{
公共布尔等于(字符串x、字符串y)
{
int-tempInt;
if(Int32.TryParse(x,out tempInt)和&(Int32.TryParse(y,out tempInt))
返回true;
布尔-坦普尔;
if(Boolean.TryParse(x,out tempBool)和&Boolean.TryParse(y,out tempBool))
返回true;
浮动时间浮动;
if(Single.TryParse(x,out tempFloat)和&Single.TryParse(y,out tempFloat))
返回true;
//还有你想比较的其他类型。。。
返回false;
//但如果两个正则字符串的计算结果也应该相等呢?
}
}

是从我的头脑中写出来的,因此可能(将)有错误:

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?
    }
}
类StringTypeEqualityComparer:IEqualityComparer
{
公共布尔等于(字符串x、字符串y)
{
int-tempInt;
if(Int32.TryParse(x,out tempInt)和&(Int32.TryParse(y,out tempInt))
返回true;
布尔-坦普尔;
if(Boolean.TryParse(x,out tempBool)和&Boolean.TryParse(y,out tempBool))
返回true;
浮动时间浮动;
if(Single.TryParse(x,out tempFloat)和&Single.TryParse(y,out tempFloat))
返回true;
//还有你想比较的其他类型。。。
返回false;
//但如果两个正则字符串的计算结果也应该相等呢?
}
}

首先,您必须定义数据类型。它们是什么?您如何知道您拥有哪种数据类型?如果您想验证它是否只包含一个数值,请使用:Int.TryPa。首先,您必须定义您的数据类型。它们是什么?您如何知道您拥有哪种数据类型?如果您想验证它是否只包含一个数值,请使用:Int.TryParse免责声明:虽然上述代码可能有效,但您可能应该在此处重新思考您的方法。我永远不会在企业、甚至个人上下文中使用此类代码。免责声明:虽然上述代码可能有效,但您可能应该在此处重新思考您的方法。我永远不会在企业、甚至个人上下文中使用此类代码。