mardi 1 mai 2018

C# Shorten if statement

I know this may be a very silly question but as a learner, I always like to learn new things.Anyway, here's a quick If statement :

string test = "It's a test";
string test2;
if (test != "")
{
    test2 = test;
}
else
{
    test2 = "It's null";
}

Note: I'm aware that Regex can also be used to check if the string is empty: Regex.IsMatch( "[A-TVWZ]", test) . Also, I'm aware that I can use SELECT CASE

In the code above, I am checking if test string is empty or not.If it is, then test2 gets one value, if it's not,test2 get's a different value.

My question is, is there a shorter/more elegant/quicker way to do it ? I mean one line which would check if string is empty or not and supply required value to test2 ?

Aucun commentaire:

Enregistrer un commentaire