Professional Documents
Culture Documents
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
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.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
}
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();
}
}
}
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;
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();
}
}
}
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 System;
using System.Collections.Generic;
using System.Text;
namespace SwitchCase
{
class SwitchCase
{
int vote = 0;
int TryCount = 0;
int i = 2;