string[] words1 = new string[] {
"How are you?",
"Where are you?"
};
string[] words2 = new string[] {
"I'm fine.",
"I'm outside."
};
// how do I perform an action by showing the indexes are same
if (words1[0] == words2[0]) // I'm stuck here
{
// an action will be executed.
}
Asked
Active
Viewed 99 times
0

Tobias Tengler
- 6,848
- 4
- 20
- 34

Tanjim
- 3
- 1
- 6
-
Kindly provide us what you are trying to achieve with little bit of explanation and some sample input and expected output – Prasad Telkikar May 03 '19 at 08:20
-
It's unclear what you're asking. Do you want to compare two strings from different arrays at a specific index? – Tobias Tengler May 03 '19 at 08:20
-
Please update your question not an **answer**. Are you looking for array length check... – Prasad Telkikar May 03 '19 at 08:22
-
No, I want to set a condition that the words1[0] is equal to words2[0] (the two index of two arrays) are same, and perform an action.@TobiasTengler – Tanjim May 03 '19 at 08:24
-
1@Tanjim Your if statement is just fine for this. It will (casesensitive) compare `"How are you?"` with `"I'm fine."`. – Tobias Tengler May 03 '19 at 08:25
-
Your if condition is correct but it will always fail because **"How are you?" is not equal to "I'm fine."** – Prasad Telkikar May 03 '19 at 08:26
-
@TobiasTengler I don't want to compare the values beneath the arrays, because the values will be different. But as the indexes are same, I want to perform an action by saying that the specific indexes are same. Similar to your advice. – Tanjim May 03 '19 at 08:31
-
@PrasadTelkikar I know they are not equal, So I want to set condition on their indexes. How can I do that. – Tanjim May 03 '19 at 08:32
-
@Tanjim the indexes are the same unless one array is larger than the other. running a for loop trough both arrays would be one way. – SomethingCool May 03 '19 at 08:34
-
@Tanjim since you input the indexes manually (`0`), why would you need to check if they're the same? Unless this is not the entire code. Please post all the relevant information, f.e. if your indexes are variables you could just compare those variables. – Tobias Tengler May 03 '19 at 08:36
-
@Tanjim check my answer pleas and let me know if that is what you looking for? – SomethingCool May 03 '19 at 09:25
-
It sounds like XY-problem. Can you describe the problem you're trying to solve ? – Dmytro Mukalov May 03 '19 at 11:02
-
@TobiasTengler I'm giving the link of the main problem that I want to solve, please check that. Looking forward to get the solution. https://stackoverflow.com/questions/55929193/is-there-any-way-to-disable-a-pair-of-matched-buttons-based-of-the-index-of-two – Tanjim May 03 '19 at 18:58
-
@whiterabbitj I was out for a while. And I didn't find you answer. Can you kindly repost it? – Tanjim May 03 '19 at 19:00
-
@DmytroMukalov I want assign different values (the meanings of those words will be same, as it will be a word matching game) inside the two arrays. But I want to show that the values same and perform an action (f.e. disable two pairs of buttons which will be carrying the two values of same meaning) based on showing the specific indexes (f.e. words1[0] == words2[0]) are same so execute a block of code then. Please let me know how to sort this out by creating such condition. – Tanjim May 03 '19 at 19:25
1 Answers
-1
When you want to check the length of two arrays you can use the length property
if (words1.Length == words2.Length)
To check if a word from words1 array exists also in words2 you can use linq. Iterate through all words from words1 and check if there is one that equals in words2 array. When not return false
foreach (var word1 in words1)
{
bool word1ExistsInWords2 = words2.FirstOrDefault(word2 => word2 == word1) == null ? false : true;
}

programmer444
- 156
- 1
- 5
-
Neither I want to check the length of two arrays nor I want to find if there is a similar value inside the two arrays. The length will be same but the values will be different. Actually it's like a words matching game between two different language. So I kept the values in two different arrays, and I want to match a pari of button based on similar meaning. Thus, I know I couldn't set up such condition (I don't know if there is any solution) based on their text property. So I thought of matching two buttons (As the values are assigned to buttons) based on setting an if condition on indexes. – Tanjim May 03 '19 at 19:09
-
(Continue) I thought as the indexes might be same so I can perform an action (disable the paired buttons and erase the texts from them) just by showing that as the indexes are same, perform such action. Sorry for longing the detail of the problem. I would be so glad if you understand what I want to do and help me out of the mess. Thanks in advance. @programmer444 – Tanjim May 03 '19 at 19:13
-
Oh okay now i understand. It's not possible this way. The computer doesn't know which words are similiar. You have to program it. When you want to check based on a similiar meaning you can use a Dictionary with the key of the meaning and the 2 language words as value (just for example). And then you can set up conditions with the button texts. – programmer444 May 04 '19 at 07:37