(Newbie question) if-else statement ignoring strings?

Now, I aknowledge this question may be a bit stupid but I couldn't find what was wrong with my code by myself so I thought I could ask for help on the forums. well, for context, I make simple codes in the code playground beetween courses to make sure that I can apply what I learned, but when I was testing if-else statements I was greeted with an error message that basically said that a string that I stored at the start of the code didn't exist. I attached my code with this post, so you can check it out to see what's wrong https://code.sololearn.com/clfkJiT8g5Lg/?ref=app

1/11/2019 3:47:20 PM


12 Answers

New Answer


To check for equality, use '==' instead of '='. And place string Manuel in double quotes like "Manuel".


To check for string equality use Equals methods: if(MyName.Equals("Manuel"))


I remember the first time i was searching why my code didnt work and i realised after many hours that you need == and not = to check equality




I think of ‘=‘ as ‘assign’ and == ( or === depending on language) as equals. This may help you too.


string input = Console.ReadLine(); if (input.Equals("hi")) { Console.WriteLine("Congratz"); } if (input == "hi") { Console.WriteLine("Congratz"); } You can use '==' and '.Equals', it's up to you :)


Guys. Of course my code works. BECAUSE IT'S FIXED. SOMEONE HELPED ME OUT.


And I do not get an error, everything works


I found the bug! For C#, you don’t need the MyName.Equals(“text”). You just need an equality sign like MyName==“text here”. You do need to do that in Java, but not in C#! Here is a link to the fixed code. Link: https://code.sololearn.com/c48J0Tcmtk69/?ref=app


I wrote a commercial C sharp program in Visual Studios 10 that uses Forms for inputs. It is Tutorial for individuals studying Electronics! I have always loved science and engineering!


I tested your code and did not have any errors! I used "Manuel" then "Sam" each time it branched to the correct output!


Oh how can someone help you unless you send code with errors?