Home > Net >  How to compare string, object with string?
How to compare string, object with string?

Time:02-10

I am new to c#. I have a few (string, object) that I need to compare to a string.

string, object looks like: [password, helloworld] and my string looks like 'helloworld'

When I try to do if(A == B) I get the following error:

Operator '==' cannot be applied to operands of type '(string, object)' and 'string'

[password, helloworld] is a System.Collections.Generic.Dictionary`2[System.String,System.Object]

CodePudding user response:

Just going off of the error you're getting it looks like you are using a C# Tuple type. Like the comments on your post are saying, you need to reference the item inside of the tuple, rather than compare the tuple itself.

public static bool TestDefaultName()
{
    (string, object) tuple = ("password", "helloworld");
    return tuple.Item2.ToString() == "helloworld";
}

You can also name the items inside the tuple:

public static bool TestCustomName()
{
    (string myString, object myObject) tuple = ("password", "helloworld");
    return tuple.myObject.ToString() == "helloworld";
}
  •  Tags:  
  • Related