Congratulations to all the winners of April 2013, they have won INR 3400 cash and INR 20147 worth prizes !
DotNetFunda.Com Logo
Twitter TwitterLinkedIn
YouTubeGoogle
 Online : 4156 |  Welcome, Guest!   Register  Login
 Home > Blogs > C# > Alternate way of writing multiple conditions inside IF statement for conditional checking ...
Niladri.Biswas

Alternate way of writing multiple conditions inside IF statement for conditional checking

 Blog author: Niladri.Biswas | Posted on: 7/8/2012 | Category: C# Blogs | Views: 1196 | Status: [Member] | Points: 75 | Alert Moderator   


Suppose we have a requirement like we want to check if a particular input string matches or not

Generally we write as under

var input = "xyz";
if (input != "a")
{
  Console.WriteLine("Not matched");
}
else
{
  Console.WriteLine("matched");
}

output
---------
Not matched

Ok.That's fine. Now consider, we have two such condition to check, so generally we go ahead as under

var input = "xyz";
if (input != "a" && input != "b")
{
  Console.WriteLine("Not matched");
}
else
{
  Console.WriteLine("matched");
}

output
---------
Not matched

If there is a third condition to check , so it will be

if (input != "a" && input != "b" && input != "c")

And so on...So if there are many such conditions to check we need to write many such conditional statements inside "If" which is difficult to maintain and less readable.

An alternate and cleaner way is to put them inside an array and search if the string is present or not as under

var input = "c";
var recordCount = new string[] { "a", "b","c","d" }.Where(i => i == input).Count();           
var result = (recordCount == 0) ? "Not matched":"matched";
Console.WriteLine(result);

In this method, we find that only we need to extend the array if some more requirement comes.It is readable and maintainable too.

Thanks for reading




Best Regards,
Niladri Biswas
Found interesting? Add this to:


Experience:6 year(s)
Home page:http://www.dotnetfunda.com
Member since:Monday, October 25, 2010
Level:Diamond
Status: [Member]
Biography:Lead Engineer at HCL Technologies Ltd., having 6 years of experience in IT field.
I love to explore new technologies and love challenges and try to help others as much as possible not only by coding but also by all possible means.

 Responses

Premalatha
Posted by: Premalatha | Posted on: 6/28/2012 | Level: Starter | Status: [Member] | Points: 15 | Alert Moderator 

if(ConditionOne && ConditionTwo && ConditionThree)
{
Code to execute
}

Second example:-

if(ConditionOne)
{
if(ConditionTwo )
{
if(ConditionThree)
{
Code to execute
}
}
}


Premalatha
Software Engineer

Niladri.Biswas
Posted by: Niladri.Biswas | Posted on: 6/28/2012 | Level: Diamond | Status: [Member] | Points: 15 | Alert Moderator 

The code of yours get's into the same problem of maintenance.That is what I tried to address in the example given. (:

Best Regards,
Niladri Biswas

Sheonarayan
Posted by: Sheonarayan | Posted on: 6/29/2012 | Level: HonoraryPlatinum | Status: [Microsoft_MVP] [Administrator] | Points: 15 | Alert Moderator 

Indeed its a better way, less line of code too. However we need to check for its speed in comparison with the traditional if else. As internally this is also doing the same thing (using linq).

A finding of speed comparison would be nice thing to see.

Thanks

Regards,
Sheo Narayan, Microsoft MVP
230+ ASP.NET Tips and Tricks - http://www.itfunda.com/Howto

>> Write Response - Respond to this post and get points

More Blogs

About Us | Contact Us | The Team | Advertise | Software Development | Write for us | Testimonials | Privacy Policy | Terms of Use | Link Exchange | Members | Go Top
General Notice: If you find plagiarised (copied) contents on this page, please let us know the original source along with your correct email id (to communicate) for further action.
Copyright © DotNetFunda.Com. All Rights Reserved. Copying or mimicking the site design and layout is prohibited. Logos, company names used here if any are only for reference purposes and they may be respective owner's right or trademarks. | 5/19/2013 8:16:59 PM