Doug.C
(Doug Crabtree)
June 16, 2024, 1:38pm
101
Edit: Conditionals: Single Line IF-ELSE Statements
klincecum:
if(name == "Chris")
Don’t forget the following:
string name = "Chris"; //Assign a name
if(name == "Chris") Console.WriteLine("Yup"); else Console.WriteLine("Nope");
if(name == "chris") Console.WriteLine("Yup"); else Console.WriteLine("Nope");
if(name == "CHRIS") Console.WriteLine("Yup"); else Console.WriteLine("Nope");
Yields:
Yup
Nope
Nope
4 Likes
klincecum
(Kevin Lincecum)
June 16, 2024, 3:42pm
102
Doug.C:
Don’t forget
I did! Nice catch.
I showed a mixed one, but not a double single line one.
Added.
1 Like
JasonMcD
(Jason McDermott)
June 17, 2024, 8:15pm
103
There might be a “better” way, but I’ve done
if(MyString1.ToUpper() == MyString2.ToUpper())
In order to avoid upper/lower/CaMeL case.
3 Likes
klincecum
(Kevin Lincecum)
June 17, 2024, 8:18pm
104
There are “better” ways, but almost all of us do that all the time.
It’s when you start dealing with multiple languages and cultures when you have to pay attention.
2 Likes
klincecum
(Kevin Lincecum)
June 17, 2024, 9:40pm
105
And I’m a “ToLower()” man.
I’ve threatened to remove the Caps Lock Keys at work.
I could keep them in a jar by my desk, next to the eyeballs of my enemies.
6 Likes
JasonMcD
(Jason McDermott)
June 18, 2024, 11:44am
106
Explain?
You mean like diacritical marks on letters, where résumé (a CV) is different than resume (to start again)?
1 Like
klincecum
(Kevin Lincecum)
June 18, 2024, 12:16pm
107
That’s one example, yes.
I can see if I can drag you up an article later that can explain it better than I can.
1 Like
jwphillips
(Jonathan Phillips)
June 18, 2024, 1:04pm
109
Having only worked on explicitly English-only, USA-only applications … this is surprising to me!
2 Likes
klincecum
(Kevin Lincecum)
June 18, 2024, 1:11pm
110
You should have seen the crap we had to do with unicode back in the old days in vb6 lol
3 Likes
Hally
(Simon Hall)
June 20, 2024, 11:59pm
111
Ok I think I might be able to come with a simple how to…At least payment processing
3 Likes
jtownsend
(John Townsend)
July 1, 2024, 8:36pm
113
Not to spark a religious war, but I am extremely divided on that one. On one hand, it’s pretty clean looking. On the other hand, I really don’t like seeing (never mind hunting for) semicolons in the middle of lines. One statement per line is a pretty good rule.
Note that I full abuse single line ifs, mostly for exiting methods early. The second there is additional steps in the logic tree, else’s or else if’s, I’m either using braces or some other method.
//example
if (dataViewA.Count == 0 || dataViewB.Count == 0) return true;
if (headerRow["Closed"]) return true;
2 Likes
klincecum
(Kevin Lincecum)
July 2, 2024, 2:10am
114
I’ve got an awful idea…
I’ll find a code example, and we’ll all clean it up and post our results.
People can vote and crown a king or queen…
Or maybe not. That sounds like a lot of work.
2 Likes
TDray
(Todd Dray)
July 2, 2024, 10:43am
115
I don’t even clean my own code up…
1 Like
jtownsend
(John Townsend)
July 2, 2024, 2:16pm
116
Not sure if this is brilliant, or it’s one of those take-home interview questions where they’re basically asking you to build a feature for them for free.
1 Like
Doug.C
(Doug Crabtree)
July 3, 2024, 6:48pm
118
To be honest, I don’t use this method either. I was primarily trying to show, in C#, case matters. But also, that you can do single line if…then…else statements…
1 Like
kve
(Kevin Veldman)
July 11, 2024, 10:50pm
119
As a guy who overuses delegates, I’d use:
string name = "Doug";
Action<string> MatchName = s => Console.WriteLine( name == s? "Yup": "Nope" );
MatchName("Doug"); // Writes "Yup"
MatchName("DOUG"); // Writes "Nope"
MatchName("Dug"); // Writes "Nope"
1 Like
kve
(Kevin Veldman)
July 11, 2024, 10:53pm
120
…or even better:
string name = "Doug";
Action<string> MatchName = s => Console.WriteLine( name == s? "Yup": "Nope" );
var NamesToCheck = new List<string>{ "Doug", "DOUG", "doug" };
NamesToCheck.ForEach( MatchName );
1 Like