You are on page 1of 8

C#

using System;
using System.Collections.Generic;
using System.Text;

namespace InitializingVariables
{
class Value
{
static void Main(string[] args)
{
int myInt = 7;
System.Console.WriteLine("Initialized myInt = {0}", myInt);

myInt = 5;
System.Console.WriteLine("After assignment myInt = {0}", myInt);
System.Console.ReadLine();
}
}
}

C# requires definite assignment, i.e. variables must be initialized or assigned to before they are used. We
must assign a value to it before we attempt to use it.

Constant:
A constant is a variable, whose value cant be changed. Constants come in 3 flavours, as-

1. Literals
2. Symbolic constants
3. Enumeration

const int FreezingPoint = 32;

A constant must be initialized, where it is declared and once initialized it cant be altered.

using System;
using System.Collections.Generic;
using System.Text;

namespace SymbolicConstant
{
class Program
{
static void Main(string[] args)
{
const int FreezingPoint = 32;
const int BoilingPoint = 212;

System.Console.WriteLine("Freezing Point of water ={0}",


FreezingPoint);
System.Console.WriteLine("Boiling Point of water ={0}",
BoilingPoint);

System.Console.ReadLine();

//BoilingPoint = 21;
//this line will give an error because BoilingPoint is a constant
}
}
}

Enumeration:
an enumeration is a distinct value types consisting of a set of named vaslue constants. Every
enumeration has an underlying type, which can be any integral type (integer, short, long etc.) except
char.

using System;
using System.Collections.Generic;
using System.Text;

namespace EnumeratedConstants
{
class Program
{
enum Temp
{
WickedCold = 0,
FreezingPoint = 32,
LightJacketWeather = 60,
SwimmingWeather = 72,
BoilingPoint = 212
}

static void Main(string[] args)


{
System.Console.WriteLine("\n\n\tFreezing Point = {0}F",
(int)Temp.FreezingPoint);
System.Console.WriteLine("\tWickedCold = {0}F",
(int)Temp.WickedCold);
System.Console.WriteLine("\tLightJacketWeather = {0} F",
(int)Temp.LightJacketWeather);
System.Console.WriteLine("\tSwimmingWeather = {0} F",
(int)Temp.SwimmingWeather);
System.Console.WriteLine("\tBoilingPoint = {0} F",
(int)Temp.BoilingPoint);
System.Console.ReadLine();
}
}
}
By default, an enumeration value is displayed using its symbolic names (i.e. BoilingPoint,
WickedCold etc.). When we want to display the value of an enumerated constant, we must cast the
constant to its underlying type (as (int)Temp.BoilingPoint) .
Each constant in an enumeration corresponds to a numerical value. If we don’t specifically set it
otherwise, the enumeration begin at 0 and each subsequent value counts up from the value previous.

using System;
using System.Collections.Generic;
using System.Text;

namespace EnumerationTest
{
class Program
{
enum SomeValue
{
First, Second, Third = 20, Fourth
}
static void Main(string[] args)
{
Console.WriteLine("\n\n\t First = {0}",(int)SomeValue.First);
Console.WriteLine("\t Second = {0}", (int)SomeValue.Second);
Console.WriteLine("\t Third = {0}", (int)SomeValue.Third);
Console.WriteLine("\t Fourth = {0}", (int)SomeValue.Fourth);
Console.ReadLine();

}
}
}

The output is…


First = 0
Second = 1
Third = 20
Fourth = 21

Branching:
There are 2 types of branching in C#.
1. Conditional Branching
2. Un-conditional Branching

Unconditional Branching:
using System;
using System.Collections.Generic;
using System.Text;

namespace UnconditionalBranching
{
class Program
{
static void SomeMethod()
{
Console.WriteLine("Executing SomeMethod()...");
Console.ReadLine();
}
static void Main(string[] args)
{
Console.WriteLine("In main calling SomeMethod()...");
Console.ReadLine();
SomeMethod();
Console.WriteLine("Inside Main Method again.");
Console.ReadLine();
}
}
}

Another way to create unconditional branching is by using the keywords – goto, break, continue, return
or throw.

Statement:
The If Statement:

using System;
using System.Collections.Generic;
using System.Text;

namespace TheIfStatement
{
class Program
{
static void Main(string[] args)
{
int ValueOne = 10;
int ValueTwo = 20;

if (ValueOne > ValueTwo)


{
Console.WriteLine("ValueOne = {0} is larger than ValueTwo =
{1}", ValueOne, ValueTwo);
}
else{
Console.WriteLine("ValueTwo = {0} is larger than ValueOne =
{1}", ValueTwo, ValueOne);
}

ValueOne = 30;
if (ValueOne > ValueTwo)
{
ValueTwo = ValueOne++;
Console.Write("setting ValueTwo to ValueOne value");
Console.Write(" and increasing ValueOne\n");
Console.WriteLine("ValueOne = {0}, ValueTwo = {1}",
ValueOne,ValueTwo);
}
else{
ValueOne = ValueTwo;
Console.WriteLine("setting them equal");
Console.WriteLine("ValueOne = {0}, ValueTwo = {1}",
ValueOne,ValueTwo);
}
Console.ReadLine();
}
}
}

Nesting of if statement:
Issues:
 If the temparature is 32 degree or lower, the program should warn
you about the ice on the road.
 If the temparature is exactly 32 degree, the program should tell
you that there may be ice patches.
using System;
using System.Collections.Generic;
using System.Text;

namespace NestedIf
{
class NestedIf
{
int temp = 0;
public void GetInput()
{
Console.Write("What is the temparature? = ");
temp = Convert.ToInt32(Console.ReadLine());
}
public void Hold_A_Line()
{
Console.ReadLine();
}
public void WhetherChecker()
{
if (temp <= 32)
{
Console.WriteLine("Warning !! Ice on road...");
if (temp == 32)
{
Console.WriteLine("Warning !! Temparature Exactly
freezing... Beware of water.");
}
else
{
Console.WriteLine("Watch For Black Ice... Temp =
{0}",temp);
}
}
else
{
Console.WriteLine("Temparature is above 32 degree F, Temp =
{0}", temp);
}
}
static void Main(string[] args)
{
NestedIf N = new NestedIf();
N.GetInput();
N.WhetherChecker();
N.Hold_A_Line();
}
}
}

What is the difference of if(temp=32) in C, C++ and in C#?


In C, C++:
In if statement if C, C++ every nonzero value is treated as true so temp will
be assinged with the value 32 and if condition will return a ‘true’ for the
checking.

In C#:
If statement in C# only accepts boolean values, so it will not assign
32 to temp variable. Instead it will throw an error at compile time.
Thus it a=is a good feature of C# compiler over the C,C++ compiler.

Using Switch Case:

using System;
using System.Collections.Generic;
using System.Text;

namespace SwitchCase
{
class SwitchCase
{
int vote = 0;
int TryCount = 0;
int i = 2;

public void ShowMenu()


{
Console.Write("\t================================\n");
Console.Write("\t------------ V O T E -----------\n");
Console.Write("\t================================\n");
Console.Write("\t DEMOCRAT = 0\n");
Console.Write("\t LIBERAL REPUBLICAN = 1\n");
Console.Write("\t REPUBLICAN = 2\n");
Console.Write("\t LIBERTARIAN = 3\n");
Console.Write("\t NEW LEFT = 4\n");
Console.Write("\t PROGRESSIVE = 5\n");
Console.Write("\t================================\n");
Console.Write("\t Plz, Enter your Vote = ");
}
public void GetData()
{
vote = Convert.ToInt32(Console.ReadLine());
}
public void Have_A_Pause()
{
Console.ReadLine();
}
public void YourChoice()
{
switch (vote)
{
case 0:
Console.WriteLine("You Voted DEMOCRATIC...");
TryCount = 3;
break;
case 1:
Console.WriteLine("You Voted LIBERAL REPUBLICAN...");
TryCount = 3;
break;
case 2:
Console.WriteLine("You Voted REPUBLICAN...");
TryCount = 3;
break;
case 3:
Console.WriteLine("You Voted LIBERTARIAN...");
TryCount = 3;
break;
case 4:
Console.WriteLine("You Voted NEW LEFT...");
TryCount = 3;
break;
case 5:
Console.WriteLine("You Voted PROGRESSIVE...");
TryCount = 3;
break;
default:
if (TryCount == 3)
{
Console.WriteLine("SORRY.... 0 attempt left, Try
Next Time...");
}
else
{
Console.WriteLine("SORRY ! your choice is not
valid... {0} Attemp Left", i--);
Console.ReadLine();
}
break;
}
}
static void Main(string[] args)
{
SwitchCase NewVote = new SwitchCase();

while (NewVote.TryCount < 3)


{
NewVote.TryCount++;
Console.Clear();
NewVote.ShowMenu();
NewVote.GetData();
NewVote.YourChoice();
}
NewVote.Have_A_Pause();
}
}
}

You might also like