Saturday, December 29, 2012

Switch Case Example in C#.NET


Below method accept a DataTime parameter and will print the astrological sign. It uses switch case statement along with if else condition to perform the logic.

private string GetastrologicalSign(DateTime dt) 

 { 
 int month = dt.Month;
 int day = dt.Day;
 switch (month)
 { 
 case 1:
 if (day <= 19)
 return "Capricorn"; 
 else return "Aquarius"; 

 case 2: 
 if (day <= 18) return "Aquarius"; 
 else return "Pisces"; 

case 3:
 if (day <= 20) return "Pisces";
else return "Aries";

 case 4: 
 if (day <= 19) return "Aries";
 else return "Taurus";

 case 5: 
 if (day <= 20) return "Taurus"; 
 else return "Gemini"; 

 case 6:
 if (day <= 20) return "Gemini";
 else return "Cancer"; 

 case 7: 
 if (day <= 22) return "Cancer"; 
 else return "Leo"; 

 case 8: 
 if (day <= 22) return "Leo";
 else return "Virgo"; 

 case 9: if (day <= 22) return "Virgo"; 
 else return "Libra"; 

 case 10: 
 if (day <= 22) return "Libra";
 else return "Scorpio"; 

 case 11: if (day <= 21) return "Scorpio"; 
 else return "Sagittarius"; 

 case 12: if (day <= 21) return "Sagittarius"; 
 else return "Capricorn"; 
 } 
 return ""; 
 }

No comments:
Write comments
Recommended Posts × +