Here, we demonstrate the use of Enumerations because, in the .NET Framework Class Library, properties can often be set only to a predetermined subset of possible values. To illustrate this point, we create our own custom enumeration and then utilize it in a simple application that demonstrates a third Decision statement, the switch.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace UsingSwitch
{
class Program
{
static void Main(string[] args)
{
Console.WriteLine("Type in a super hero's name to see his nickname:");
string userValue = Console.ReadLine();
switch(userValue.ToUpper())
{
case "BATMAN":
Console.WriteLine("Caped Crusader");
break;
case "SUPERMAN":
Console.WriteLine("Man of Steel");
break;
case "GREENLANTERN":
Console.WriteLine("Emerald Knight");
break;
default:
Console.WriteLine("Does not compute");
break;
}
Console.ReadLine();
}
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace UnderstandingEnumerations
{
class Program
{
static void Main(string[] args)
{
Console.ForegroundColor = ConsoleColor.DarkRed;
//Console.WriteLine("Hello world!");
Console.WriteLine("Type in a superhero's name to see his nickname: ");
string userValue = Console.ReadLine();
SuperHero myValue;
if (Enum.TryParse<SuperHero>(userValue, true, out myValue))
{
switch (myValue)
{
case SuperHero.Batman:
Console.WriteLine("Caped crusader");
break;
case SuperHero.Superman:
Console.WriteLine("Man of Steel");
break;
case SuperHero.GreenLantern:
Console.WriteLine("Emerald Knight");
break;
default:
break;
}
}
else
{
Console.WriteLine("Does not compute");
}
Console.ReadLine();
}
}
enum SuperHero
{
Batman,
Superman,
GreenLantern
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ComplexIfStatement
{
class Program
{
static void Main(string[] args)
{
Console.WriteLine("Pick a number between 1 and 100:");
string userValue = Console.ReadLine();
int compareValue = int.Parse(userValue);
if ((compareValue < 1) || (compareValue > 100))
Console.WriteLine("The number you chose was out of bounds.");
else if ((compareValue == 42) || (compareValue > 90))
Console.WriteLine("You found one of the special numbers!");
else
Console.WriteLine("You didn't find one of the special numbers");
Console.ReadLine();
}
}
}
Source : MS Virtual Academy
No comments:
Post a Comment