if ... else if ... else if ... else ... : an alternative(blog.activa.be)

submitted by activaactiva(2340) 4 years, 2 months ago

Did you know there's an alternative way of writing "if .. else if .. else if .. etc.." in C#? You probably do, but if you don't, read on...

5 comments |category: |Views: 270

tags: another

new Add a live kick counter to your blog >> liveImage

You can even customize the image by choosing your own colors, and then clicking the button below to update the preview and the html code:

  • "Kick It" text
  • "Kick It" background
  • kick count text
  • kick count background
  • border

Simply copy and paste this HTML into your blog post.


Users who kicked this story:
Comments:

posted by luciolucioluciolucio(0) 4 years, 2 months ago 0

404'd! :D

Reply

posted by vistadbvistadb(879) 4 years, 2 months ago 0

yuck, I would shoot anyone on my team who nested 5+ ternary operators. If you want to get rid of the IF ELSE lists replace it with another construct, not a shortcut to the exact same construct.

Reply

posted by wisemxwisemx(8074) 4 years, 2 months ago 0

read the comments...went over like walking into a classic cafe and asking where Starbucks is. :-)

Reply

posted by samdnpsamdnp(980) 4 years, 2 months ago 0

I am terrified now. I have seen bad code, but......

Reply

posted by activaactiva(2340) 4 years, 2 months ago 0

You people are hilarious.

What a fuss over a stupid article about a simple language feature that few people know about. The code sample I gave is not complicated, and cleaner than the if-then-else way of writing it, *IN THIS SPECIFIC CASE*. And in this case, even if someone doesn't have a clue about what the ternary operator does, he/she will still capture the meaning of that piece of code, because of the way it is written.

I would never recommend this pattern for performing complex conditional assignments. It's just a piece of code showing the "special" behavior of a right-associative operator such as the ternary operator.

Reply

information Login or create an account to comment on this story