I am having a
List<Tuple<string,string>> tr = new List<Tuple<string,string>>();
tr.Add(new Tuple<string, string>("Test","Add");
tr.Add(new Tuple<string, string>("Welcome","Update");
foreach (var lst in tr)
{
if(lst.Contains("Test"))
MessageBox.Show("Value Avail");
}
I failed while doing this ,....
A tuple elements can be accessed with Item<elementNumber> properties, e.g., Item1, Item2, Item3, and so on up to Item7 property. The Item1 property returns the first element, Item2 returns the second element, and so on. The last element (the 8th element) will be returned using the Rest property.
A tuple is a data structure that contains a sequence of elements of different data types. Tuple<int, string, string> person = new Tuple <int, string, string>(1, "Test", "Test1"); A tuple can only include a maximum of eight elements. It gives a compiler error when you try to include more than eight elements.
C# tuple is a data structure that provides an easy way to represent a single set of data. The System. Tuple class provides static methods to create tuple objects.
Probably this should work:
foreach (var lst in tr)
{
if (lst.Item1.Equals("Test"))
MessageBox.Show("Value Avail");
}
or this
if (lst.Item1.Equals("Test") || lst.Item2.Equals("Test"))
Read Tuple Class; you'll need to access values of the tuple through Item1
and/or Item2
properties.
And why use Tuple at all? Maybe this is easier:
Dictionary<string, string> dict = new Dictionary<string, string>
{
{"Test", "Add"},
{"Welcome", "Update"}
};
if (dict.ContainsKey("Test"))
{
MessageBox.Show("Value Avail:\t"+dict["Test"]);
}
If you'd like to use LINQ:
if(tr.Any(t => t.Item1 == "Test" || t.Item2 == "Test"))
MessageBox.Show("Value Avail");
This will also have the benefit of only showing the message box once if the text is found multiple times (if that is what is desired).
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With