A different if else in .net

by Marre 30. November 2009 09:41

As a programmer I always try to find ways to cleaner code. This is an example:

The code can be a mess when using if else if else if else if (well, I think you know what I mean :) )

Instead of writing my code like this:

if (countryCode == "SE")
    country= "Sweden";
else if (countryCode== "NO")
    country= "Norway";
else if (countryCode== "DK")
    country= "Denmark";
else
    country = "";

 

I can write it like this:

 

country =(countryCode == "SE") ? "Sweden" : (countryCode == "NO") ? "Norway" : (countryCode == "DK") ? "Denmark": "";

 

 

Nice? Well, I think so :)

Be the first to rate this post

  • Currently 0/5 Stars.
  • 1
  • 2
  • 3
  • 4
  • 5

Tags:

C#

Powered by BlogEngine.NET 1.4.5.0
Theme by Supremelink Development

Martin Andersson

I´m currently working as senior consultant for Capgemini Norway AS.

Curriculum vitae

I use this blog when I find something useful that I want to be able to get hold of wherever I need. But who knows...Maybe someone else will find it useful as well.


About

Supremelink is the name of where I collect my spare time projects. The idea is to learn more about new techniques and areas that I´m not usually is working with or that can be nice to know before future projects at work.

To see the benefits with new technologies and to achieve as much knowledge about those areas, I usually have a goal/project to work with.

This site run´s on BlogEngine.NET. It´s a full featured blog that is using XML as data source. No database is required.

Supreme Software

When I find software that I like and of whitch I can see the benefits to use, I write about it under the category "Supreme Software".

Beware of the soon coming updates in this category!

Tag cloud