C# Slide Teacher
C# Slide Teacher
C# Slide Teacher
A variable is a name of memory location. It is used to store data. Its value can be changed and
it can be reused many times.
It is a way to represent memory location through symbol so that it can be easily identified.
In this example, Student is the type and s1 is the reference variable that refers to the instance of
Student class. The new keyword allocates memory at runtime.
DESCRIPTION
// Addition
result = (x + y);
Console.WriteLine("Addition Operator: " + result);
// Subtraction
result = (x - y);
Console.WriteLine("Subtraction Operator: " + result);
// Multiplication
result = (x * y);
Console.WriteLine("Multiplication Operator: "+ result);
// Division
result = (x / y);
Console.WriteLine("Division Operator: " + result);
// Modulo
result = (x % y);
Console.WriteLine("Modulo Operator: " + result);
EXAMPLE
class Program
{
static void Main(string[] args)
{
Console.ReadKey();
}
}
Console.WriteLine("Value = {0}", val);
{0} is the placeholder for variable val which will be replaced by value of val. Since only one variable is used so there is only
one placeholder
EXAMPLE
class Program {
In C# programming, the if statement is used to test the condition. There are various types of if
statements in C#.
if statement
if-else statement
nested if statement
if-else-if ladder
C# IF STATEMENT
The C# if statement tests the condition. It is executed if condition is true
The if statement is single selection control structure.
Syntax:
if(condition){
//code to be executed
}
EXAMPLE
using System;
public class IfExample
{
public static void Main(string[] args)
{
int num = 10;
if (num % 2 == 0)
{
Console.WriteLine("It is even number");
}
} }
C# IF-ELSE STATEMENT
The C# if-else statement also tests the condition. It executes the if block if condition is true
if(condition){
using System;
{
public static void Main(string[] args) {
int num = 11;
if (num % 2 == 0)
{
Console.WriteLine("It is even number"); }
else
{ Console.WriteLine("It is odd number"); } } }
C# IF-ELSE EXAMPLE: WITH INPUT FROM
USER
using System;
public class IfExample
{
public static void Main(string[] args)
{
Console.WriteLine("Enter a number:");
int num = Convert.ToInt32(Console.ReadLine());
if (num % 2 == 0)
{
Console.WriteLine("It is even number");
}
else
{
Console.WriteLine("It is odd number");
} }
} Convert.ToInt32() converts a value to a 32-bit signed integer (int) data type.
C# IF-ELSE-IF STATEMENT
The C# if-else-if ladder statement executes one condition from multiple statements
Syntax:
if(condition1){
//code to be executed if condition1 is true
}else if(condition2){
//code to be executed if condition2 is true
}
else if(condition3){
//code to be executed if condition3 is true
}
...
else{
//code to be executed if all the conditions are false
C# IF ELSE-IF EXAMPLE
using System;
public class IfExample
{
public static void Main(string[] args)
{
Console.WriteLine("Enter a number to check grade:");
int num = Convert.ToInt32(Console.ReadLine());
if (num <0 || num >100)
{
Console.WriteLine("wrong number");
}
else if(num >= 0 && num < 50){
Console.WriteLine("Fail");
}
else if (num >= 50 && num < 60)
{
Console.WriteLine("D Grade");
else if (num >= 60 && num < 70)
{
Console.WriteLine("C Grade");
}
else if (num >= 70 && num < 80)
{
Console.WriteLine("B Grade");
}
else if (num >= 80 && num < 90)
{
Console.WriteLine("A Grade");
}
else if (num >= 90 && num <= 100)
{
Console.WriteLine("A+ Grade");
}
}
C# SWITCH
The C# switch statement executes one statement from multiple conditions. It is like if-else-if ladder statement in C#.
Syntax:
switch(expression){
case value1:
//code to be executed;
break;
case value2:
//code to be executed;
break;
......
default:
//code to be executed if all cases are not matched;
break;
}
C# SWITCH EXAMPLE
using System;
switch (num)
{
case 10: Console.WriteLine("It is 10"); break;
case 20: Console.WriteLine("It is 20"); break;
case 30: Console.WriteLine("It is 30"); break;
default: Console.WriteLine("Not 10, 20 or 30"); break;
}
}
C# SWITCH EXAMPLE
char ch;
Console.WriteLine("Enter an alphabet");
ch = Convert.ToChar(Console.ReadLine());
switch(Char.ToLower(ch))
{
case 'a':
case 'e':
case 'i':
case 'o':
case 'u':
Console.WriteLine("Vowel");
break;
default:
Console.WriteLine("Not a vowel");
break; }
LOOP IN C#
Looping in a programming language is a way to execute a
statement or a set of statements multiple numbers of times
depending on the result of a condition to be evaluated. The
resulting condition should be true to execute statements within
loops.
In C# We have Three Type Of Loop
For Loop(Foreach Loop is one of The Type For Loop)
While Loop
Do While Loop
FOR LOOP
The C# for loop is used to iterate a part of the program several times. If the number of iteration is
fixed, it is recommended to use for loop than while or do-while loops.
The C# for loop is same as C/C++. We can initialize variable, check condition and
increment/decrement value.
Syntax:
for(initialization; condition; incr/decr){
//code to be executed
}
C# FOR LOOP EXAMPLE
using System;
public class ForExample
{
public static void Main(string[] args)
{
for(int i=1;i<=10;i++){
Console.WriteLine(i);
}
}
}
SUM OF NUMBERS FROM 1-10
int sum = 0;
for(int i=0;i<100;i++)
{
Console.WriteLine("" + i);
sum = i + sum;
}
Console.WriteLine("the sum+" + sum);
C# NESTED FOR LOOP
In C#, we can use for loop inside another for loop, it is known as nested for loop. The inner loop is
executed fully when outer loop is executed one time. So if outer loop and inner loop are executed
3 times, inner loop will be executed 3 times for each outer loop i.e. total 9 times.
Example:
using System;
public class ForExample
{
public static void Main(string[] args)
{
for(int i=1;i<=3;i++){
for(int j=1;j<=3;j++){
Console.WriteLine(i+" "+j);
}
C# INFINITE FOR LOOP
If we use double semicolon in for loop, it will be executed infinite times. Let's see a simple example of
infinite for loop in C#.
using System;
public class ForExample
{
public static void Main(string[] args)
{
for (; ;)
{
Console.WriteLine("Infinitive For Loop");
}
}
}
FOR EACH LOOP IN C#
The foreach statement in C# iterates through a collection of items such as an array or list, The
foreach body must be enclosed in {} braces unless it consists of a single statement.
Foreach loop (or for each loop) is a control flow statement for traversing items in a
collection
foreach loop is used to iterate over the elements of the collection. The collection may be an
array or a list. It executes for each element present in the array.
Syntax:
•It is necessary to enclose the statements of foreach loop in curly braces {}.
•Instead of declaring and initializing a loop counter variable, you declare a
variable that is the same type as the base type of the array, followed by a
colon, which is then followed by the array name.
•In the loop body, you can use the loop variable you created rather than using
an indexed array element.
EXAMPLES
1- class Program
{
static void Main(string[] args)
{
Console.WriteLine("foreach loop Sample!");
int[] oddArray = new int[] { 1, 3, 5, 7, 9, 11, 13, 15, 17, 19, 21 };
foreach (int num in oddArray)
{
Console.WriteLine(num);
}
Console.ReadKey();
}
}
EXAMPLES
-Use foreach to find a char in a string
// Convert string into an array of chars
char[] chArray = name.ToCharArray();
// Loop through chars and display one char at a time
foreach (char ch in chArray)
{
if (ch.ToString() != " ")
Console.WriteLine(ch);
}
EXAMPLE
-Foreach in Collection
// Find number of occurrences of a char in a string
string str = "A monkey stole a banana";
char[] chars = str.ToCharArray();
int ncount = 0;
// Loop through chars and find all 'n' and count them
foreach (char ch in chars)
{
if (ch == 'n')
ncount++;
}
Console.WriteLine($"Total n found {ncount}");
EXAMPLE
int[] number_array = new int[5];
for (int i = 0; i < number_array.Length; i++)
{
Console.WriteLine("Enter value #" + (i + 1) + ": ");
number_array[i] = Convert.ToInt32(Console.ReadLine());
}
Console.WriteLine("Numbers entered:");
foreach (int number in number_array)
{
Console.WriteLine(number);
}
Console.ReadKey();
C# WHILE LOOP
In C#, while loop is used to iterate a part of the program several times. If the number of
iteration is not fixed, it is recommended to use while loop than for loop.
Syntax:
while(condition){
//code to be executed
}
C# WHILE LOOP EXAMPLE
using System;
public class WhileExample
{
public static void Main(string[] args)
{
int i=1;
while(i<=10)
{
Console.WriteLine(i);
i++;
}
}
}
C# NESTED WHILE LOOP
EXAMPLE:
In C#, we can use while loop inside another while loop, it is known as nested while loop. The nested
while loop is executed fully when outer loop is executed once.
using System;
Function is a block of code that has a signature. Function is used to execute statements
specified in the code block. A function consists of the following components:
C# Function Syntax
<access-specifier><return-type>FunctionName(<parameters>)
{
// function body
// return statement
}
Access-specifier, parameters and return statement are optional.
TYPE OF FUNCTION
User defined functions
The size of the arrays should be specified at the time of its declaration.
In an array, elements are stored sequentially, that is, in contiguous memory locations.
Only one element can be added or removed from the array at a time
ADVANTAGE OF ARRAY
Arrays can store a large number of values with single name.
A one-dimensional array has only one subscript and a multi-dimensional array has n
subscripts, n presents the dimensions of the array.
Multi-dimensional arrays can be further classified into two dimensional and three-dimensional
arrays.
The dimensionality of an array is determined by the number of pairs of square brackets placed
after the array name.
One dimensional array array1[ ], two dimensional array2[ ,]three dimensional array
array3[ ,,].
ONE-DIMENSIONAL ARRAY
A type of array in which all elements are arranged in the form of a list is known as
one dimensional array.
Its also called single dimensional array or linear list
In one-dimensional array the data is stored in a sequence of memory locations.
Each individual element in an array can be referred by subscripted variables and its
declared form is as
Where:
data-type is any valid data type i.e. int, float, char etc.
array-name is the name of array
size indicates the number of locations in which data elements are to be stored
EXAMPLE
For example, if the marks of ten students are to be processed in a program we might use
an array to store them.
The computer must be instructed to reserve a sequence of ten memory locations for
storing the marks, the array can be defined or declared for this purpose as :
string[] Books = new string[5];
//traversing array
for (int i = 0; i <size; i++)
{
Console.WriteLine(“The index number {0}:”,i);
arr[i]=convert.toInt32(Console.RedLine());
}
Console.ReadKey();
DISPLAY VLAUE
Foreach(int k in arr)
{
Console.writeLine(k);
sum+=k
}
Consol.writeLine(“The sum is =”+sum);
TWO-DIMENSIONAL ARRAY
Two dimensional array can be considered as a table that consists of rows and
columns .
Each element in 2-d array is referred with the help of two indexes.
One index is used to indicate the row and the second index indicates the column
of the element.
Data_type [Row,Cols] identifier;
Identifier indicate the name of array
Rows indicates the number of rows in the array.
Cols indicates the number of columns in the array.
Int [,]arr;
This example shows an array with 4 rows and 3 columns
DECLARING AND &
INITIALIZING 2D ARRAY
Similar to a 1D array, the elements 2D array can be initialized either one at a time or all at
once.
Syntax
int[,] arr=new int[3,3];//declaration of 2D array
int[,] arr=new int[3,3];//declaration of 2D array
arr[0,1]=10;//initialization
arr[1,2]=20;
arr[2,0]=30;
int[,] arr = { { 1, 2, 3 }, { 4, 5, 6 }, { 7, 8, 9 } };
EXAMPLE
int[,] arr = { { 1, 2, 3 }, { 4, 5, 6 }, { 7, 8, 9 } };//declaration and initialization
//traversal
for(int i=0;i<3;i++){
for(int j=0;j<3;j++){
Console.WriteLine(“ The value:”+arr[i,j]+" ");
}
EXAMPEL
Console.WriteLine("Enter The Array Row Size !");
int Rowsize = Convert.ToInt32(Console.ReadLine());
Console.WriteLine("Enter The Arrary Column Size !");
int Columnsize = Convert.ToInt32(Console.ReadLine());
int[,] numbers = new int[Rowsize,Columnsize];
int sum = 0;
for (int i = 0; i < Rowsize; i++)
{
for (int j = 0; j < Columnsize; j++)
{
numbers[i, j] = Convert.ToInt32(Console.ReadLine());
sum = sum + numbers[i,j];
}
}
CONT.…
Console.WriteLine("the sum is =" + sum);
Console.WriteLine(" The value is called !");
foreach (int value in numbers)
{
Console.WriteLine(value + "");
}
Console.ReadKey();
}
OBJECT
In C#, Object is a real world entity, for example, chair, car, pen, mobile, laptop etc.
In other words, object is an entity that has state and behavior. Here, state means data and
behavior means functionality.
Object is a runtime entity, it is created at runtime.
Object is an instance of a class. All the members of the class can be accessed through object.
Let's see an example to create object using new keyword.
Student s1 = new Student();//creating an object of Student
In this example, Student is the type and s1 is the reference variable that refers to the
instance of Student class. The new keyword allocates memory at runtime
C# CLASS
In C#, class is a group of similar objects. It is a template from which objects are created. It can
have fields, methods, constructors etc.
Let's see an example of C# class that has two fields only.
public class Student
{
int id;//field or data member
String name;//field or data member
}
C# OBJECT AND CLASS
EXAMPLE
Let's see an example of class that has two fields: id and name. It creates instance of the
class, initializes the object and prints the object value
public class Student
{
int id;//data member (also instance variable)
String name;//data member(also instance variable)
public static void Main(string[] args)
{
Student s1 = new Student();//creating an object of Student
s1.id = 101;
s1.name = "Sonoo Jaiswal";
Console.WriteLine(s1.id);
Console.WriteLine(s1.name);
}
}
HAVING MAIN() IN ANOTHER CLASS
using System;
public class Student
{
public int id;
public String name;
}
class TestStudent{
public static void Main(string[] args)
{
Student s1 = new Student();
s1.id = 101;
s1.name = "Sonoo Jaiswal";
Console.WriteLine(s1.id);
Console.WriteLine(s1.name);
}
}
INITIALIZE AND DISPLAY DATA THROUGH METHOD
A destructor works opposite to constructor, It destructs the objects of classes. It can be defined only once in a
class. Like constructors, it is invoked automatically
In c#, Destructor is a special method of a class, and it is used in a class to destroy the object or instances of classes
using System;
public class Employee
{
public Employee()
{
Console.WriteLine("Constructor Invoked");
}
~Employee()
{
Console.WriteLine("Destructor Invoked");
}
}
CONT..
class TestEmployee{
public static void Main(string[] args)
{
Employee e1 = new Employee();
Employee e2 = new Employee();
}
}
C# ACCESS MODIFIERS / SPECIFIERS
C# Access modifiers or specifiers are the keywords that are used to specify accessibility or scope
of variables and functions,Clasess in the C# application.
C# provides five types of access specifiers.
Public: It specifies that access is not restricted.
Protected: It specifies that access is limited to the containing class or in derived class.
Private: It specifies that access is limited to the containing type
C# THIS
In C# programming, this is a keyword that refers to the current instance of the class. There can main
usage of this keyword in C#.
The “this” keyword in C# is used to refer to the current instance of the class. It is also used to
differentiate between the method parameters and class fields if they both have the same name.
Another usage of “this” keyword is to call another constructor from a constructor in the same class
CONT..
using System;
public class Employee
{
public int id;
public String name;
public float salary;
public Employee(int id, String name,float salary)
{
this.id = id;
this.name = name;
this.salary = salary;
}
public void display()
{
Console.WriteLine(id + " " + name+" "+salary); }}
CONT.…
class TestEmployee{
public static void Main(string[] args)
{
Employee e1 = new Employee(101, “ahmad", 890000af);
Employee e2 = new Employee(102, “ajmal", 490000af);
e1.display();
e2.display();
}
}
C# STATIC
In C#, static means something which cannot be instantiated. You cannot create an object of a static
class and cannot access static members using an object.
In C#, static is a keyword or modifier that belongs to the type not instance. So instance is not
required to access the static members. In C#, static can be field, method, class .
Advantage of C# static keyword
Memory efficient: Now we don't need to create instance for accessing the static members, so it
saves memory. Moreover, it belongs to the type, so it will not get memory each time when instance
is created.
STATIC CLASS
Static Class can not be initialized , it means you can not use the new keyword to
Create a variable of Class type .
Static Class can not inherit from any Class except ,System.obj.
Example of Static Class is in C# System.Math.
All the member of static class must be static otherwise the compiler throw the error.
EXAMPLE
public static class Calculator
{ private static int _resultStorage = 0;//it is a class level Variable.
public static string Type = "Arithmetic";
public static int Sum(int num1, int num2)
{
return num1 + num2; }
public static void Store(int result)
{ _resultStorage = result;
}
}
CONT.…
Above, the Calculator class is a static. All the members of it are also static.
You cannot create an object of the static class; therefore the members of the static class can be
accessed directly using a class name like ClassName.MemberName, as shown below.
class Program
{
static void Main(string[] args)
{
var result = Calculator.Sum(10, 25); // calling static method
Calculator.Store(result);
var calcType = Calculator.Type; // accessing static variable
Calculator.Type = "Scientific"; // assign value to static variable
}
}
C# NAMESPACES
Namespaces in C# are used to organize too many classes so that it can be easy to handle the
application.
In a simple C# program, we use System. Console where System is the namespace and Console
is the class. To access the class of a namespace, we need to use namespacename.classname.
We can use using keyword that we don't have to use complete name all the time.
INTRODUCTION TO INHERITANCE IN C#
Inheritance in C# is the process of acquiring all the properties of one class into another class.
There are two classes referred to as base class and derived class. The base class is also
known as parent class and the properties or methods of this class we want to inherit to another
class.
The derived class is known as child class that is used to inherit the properties and methods of
the base class or parent class. It helps in reusing the same code again and no need to define the
same properties again and again.
Inheritance is one of the powerful concepts or fundamental attributes of
object-oriented programming language. It is widely used in all the OOPs based programming
language. Its main purpose is to use the same code again. The example of the Basic Structure
of Inheritance is given below:
CONT.…
Derived Class (child) - the class that inherits from another class
Base Class (parent) - the class being inherited from
To inherit from a class, use the : symbol.
class BaseClass { }
class ChildClass: BaseClass {}
Types of Inheritance in C#
The term "Polymorphism" is the combination of "poly" + "morphs" which means many
forms.
It is a greek word. In object-oriented programming, we use 3 main concepts: inheritance,
encapsulation and polymorphism.
There are two types of polymorphism in C#
{
We can use the base keyword to access the fields of the base class within derived class. It is
useful if base and derived classes have the same fields. If derived class doesn't define same field,
there is no need to use base keyword. Base class field can be directly accessed by the derived
class.
EXAMPLE
public string color = "white";
using System;
public class Animal{
}
public class Dog: Animal
{
string color = "black";
public void showColor()
{
Console.WriteLine(base.color);
Console.WriteLine(color);
} }
public class TestBase
{
public static void Main()
{ Dog d = new Dog();
d.showColor(); } }
CALLING BASE CLASS METHOD
using System;
{
{
1-Abstract class(0-100%)
2-Interface(100%)
Abstract class and interface both can have abstract methods which are necessary for
abstraction.
ABSTRACT METHOD
A method which is declared abstract and has no body is called abstract method. It
can be declared inside the abstract class only. Its implementation must be provided
by derived classes.
For example:
Syntax:
public abstract void draw();
An abstract method in C# is internally a virtual method so it can be overridden by
the derived class
You can't use static and virtual modifiers in abstract method declaration
C# ABSTRACT CLASS
In C#, abstract class is a class which is declared abstract. It can have abstract and
non-abstract methods. It cannot be instantiated.
Its implementation must be provided by derived classes. Here, derived class is forced
to provide the implementation of all the abstract methods.
Let's see an example of abstract class in C# which has one abstract method draw().
Its implementation is provided by derived classes: Rectangle and Circle. Both classes
have different implementation.
EXAMPLE
using System;
{ public abstract class Shape
public abstract void draw();
}
public class Rectangle : Shape
{
public override void draw()
{
Console.WriteLine("drawing rectangle...");
} }
public class Circle : Shape
{
public override void draw()
{
Console.WriteLine("drawing circle..."); } }
CONT.…
public class TestAbstract
{
public static void Main()
{
Shape s;
s = new Rectangle();
s.draw();
s = new Circle();
s.draw();
}
}
C# INTERFACE
Interface in C# is a blueprint of a class. It is like abstract class because all the methods
which are declared inside the interface are abstract methods. It cannot have method body
and cannot be instantiated.
It is used to achieve multiple inheritance which can't be achieved by class. It is used to
achieve fully abstraction because it cannot have method body.
Its implementation must be provided by class The class which implements the interface,
must provide the implementation of all the methods declared inside the interface.
Note: Interface methods are public and abstract by default. You cannot explicitly use
public and abstract keywords for an interface method
C# INTERFACE EXAMPLE
Let's see the example of interface in C# which has draw() method. Its implementation is provided by two classes: Rectangle and
Circle.
using System;
public interface Drawable
{
void draw();
}
public class Rectangle : Drawable
{
public void draw()
{
Console.WriteLine("drawing rectangle...");
}
}
CONT..
public class Circle : Drawable
{
public void draw()
{
Console.WriteLine("drawing circle...");
} }
public class TestInterface
{
public static void Main()
{
Drawable d;
d = new Rectangle();
d.draw();
d = new Circle();
d.draw(); } }
CONT.…
Note: Interface methods are public and abstract by default. You cannot explicitly use public
and abstract keywords for an interface method
using System;
public interface Drawable
{
public abstract void draw();//Compile Time Error
}
MULTIPLE INHERITANCE BY
INTERFACE
public interface Interface1
{
void Test();
void Show();
}
public interface Interface2
{
void Test();
void Show();
}
class ImplementInterafce : Interface1, Interface2
{
public void Test()
{
Console.WriteLine("Test method is implemented");
}
public void Show()
{
Console.WriteLine("Show mwthod is implemented");
}
class Program
{
{
obj.Test();
obj.Show();
obj1.Test();
obj1.Show();
obj2.Test();
obj2.Show();
Console.ReadKey();
}
}
}
C# STRINGS
In C#, string is an object of System.String class that represent sequence of characters. We can
perform many operations on strings such as concatenation, comparison, getting substring,
search, trim, replacement etc.
string vs String
In C#, string is keyword which is an alias for System.String class. That is why string and String
are equivalent. We are free to use any naming convention.
string s1 = "hello";//creating string using string keyword
String s2 = "welcome";//creating string using String class
EXAMPLE
using System;
public class StringExample
{
public static void Main(string[] args)
{
string s1 = "hello";
char[] ch = { 'c', 's', 'h', 'a', 'r', 'p' };
string s2 = new string(ch);
Console.WriteLine(s1);
Console.WriteLine(s2);
}
}
C# STRING METHODS
Split(char[])
ToLower()
ToUppwer()
ToString()
Replace()
Remove()
join(string,string[])
lastIndexof(char)
SPLIT()
public static void Main(string[] args)
{
string s1 = "Hello C Sharp";
string[] s2 = s1.Split(' ');
foreach (string s3 in s2)
{
Console.WriteLine(s3);
}
TO STRING()
public static void Main(string[] args)
{
string s1 = "Hello C#";
int a = 123;
string s2 = s1.ToString();
string s3 = a.ToString();
Console.WriteLine(s2);
Console.WriteLine(s3);
}
C# EXCEPTION HANDLING
It maintains the normal flow of the application. In such case, rest of the code is executed event
after exception.
C# EXCEPTION CLASSES
Exception Description
C# finally block is used to execute important code which is to be executed whether exception is handled or not. It must
be preceded by catch or try block.
C# finally example if exception is handled
public static void Main(string[] args)
{
try
{
int a = 10;
int b = 0;
int x = a / b;
}
catch (Exception e) { Console.WriteLine(e); }
finally { Console.WriteLine("Finally block is executed"); }
Console.WriteLine("Rest of the code");
System.DivideByZeroException: Attempted to divide by zero. Finally block is executed Rest of the code
EXAMPLE
try
{
int arr[]= {1,3,5,7};
Consol.WriteLine (arr[10]); //may throw exception
}
// handling the array exception
catch(System.IndexOutOfRangeException e)
{
Console.WrietLine(e);
}
Console.WrietLine ("rest of the code");
THROW KEYWORD
In c#, the throw is a keyword and it is useful to throw an exception manually
during the execution of the program and we can handle those thrown exceptions
using try-catch blocks based on our requirements. The throw keyword will raise
only the exceptions that are derived from the Exception base class.
throw exception;
public static void fn(Int32 age)
{
if (age < 0)
{
// throw an argument out of range exception if the age is
// less than zero.
throw new ArgumentOutOfRangeException("Age Cannot Be Negative ");
}
}
catch (Exception e)
{
Console.WriteLine(String.Concat(e.StackTrace, e.Message));
Console.ReadLine();
}
END