C# Simplified
C# Simplified
NET-Simplified
Contents
ABOUT THE AUTHOR ................................................................................................................................................................. 4
CHAPTER 1: ARITHMETIC OPERATIONS ................................................................................................................................... 5
CHAPTER 2: TYPE CASTING ..................................................................................................................................................... 15
CHAPTER 3: CONTROL STRUCTURES ...................................................................................................................................... 23
1. CONDITIONAL: ....................................................................................................................................................................................... 24
2
www.manzoorthetrainer.com
C#.NET-Simplified
CHAPTER 14 : UNDERSTANDING THE CONCEPT OF INHERITANCE AND PROTECTED VARIABLES IN C#. ....................... 148
CHAPTER 15 : CONSTRUCTOR CHAINING IN C#. .................................................................................................................. 155
CHAPTER 16 : METHOD OVERRIDING, VARIOUS TYPES OF INHERITANCE AND CONSTRUCTOR CHAINING IN C#. ....... 168
CHAPTER 17 : ABSTRACT METHOD AND ABSTRACT CLASS IN C#. .................................................................................... 184
CHAPTER 18 : REAL-TIME RUNTIME POLYMORPHISM IMPLEMENTATION IN C#. ............................................................. 191
CHAPTER 19 : SEALED METHOD AND SEALED CLASS IN C#. .............................................................................................. 207
CHAPTER 20 : INTERFACES AND MULTIPLE INHERITANCE IN C#. ...................................................................................... 213
CHAPTER 21 : COLLECTION CLASSES AND ITS LIMITATION IN C#. .................................................................................... 222
CHAPTER 22 : GENERIC COLLECTION CLASSES IN C#. ......................................................................................................... 232
CHAPTER 23 : CONCEPT OF DELEGATES, UNICAST DELEGATES AND MULTICAST DELEGATES IN C#. ........................... 239
CHAPTER 24 : DELEGATES, CALLBACK AND METHOD CONCEPT IN C#. ............................................................................ 247
3
www.manzoorthetrainer.com
C#.NET-Simplified
4
www.manzoorthetrainer.com
C#.NET-Simplified
Now it opens new window with Program.cs file name (i.e. Default page
whenever we start console application).
That program contain namespace as your file name (MathOperationsEg).
In this page we need to start writing the program from main.
5
www.manzoorthetrainer.com
C#.NET-Simplified
6
www.manzoorthetrainer.com
C#.NET-Simplified
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace MathOperationsEg
{
class Program
{
static void Main(string[] args)
{
int n1, n2, n3;
n1=67;
n2=56;
n3 = n1 + n2;
Console.WriteLine(n3);
Console.ReadLine();
}
}
}
7
www.manzoorthetrainer.com
C#.NET-Simplified
8
www.manzoorthetrainer.com
C#.NET-Simplified
Press F5.
Both of the methods display same result but using two different techniques.
One is passing parameters kind of things like giving indexes for n number of
variables, and another is using + operator for string concatenation.
9
www.manzoorthetrainer.com
C#.NET-Simplified
Console.ReadLine();
}
Press F5.
If we want to add different values we need not to go and edit the program
again and again.
We can give the option to end user to enter the values from keyboard at
runtime.
10
www.manzoorthetrainer.com
C#.NET-Simplified
11
www.manzoorthetrainer.com
C#.NET-Simplified
If we have double values we need to change int type to double and instead
of int.Parse we need to use double.Parse.
Double n1,n2,n3;
Console.WriteLine(Enter the value for n1);
n1= double.Parse(Console.ReadLine());
Console.WriteLine(Enter the value for n2);
n2= double.Parse(Console.ReadLine());
Complete code of addition of two integer numbers given below.
12
www.manzoorthetrainer.com
C#.NET-Simplified
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace MathOperationsEg
{
class Program
{
static void Main(string[] args)
{
int n1, n2, n3;
Console.WriteLine("Enter the value for n1");
n1 =int.Parse(Console.ReadLine());
n3 = n1 + n2;
Console.WriteLine("Sum of {0} and {1} is {2}",n1,n2,n3);
Console.WriteLine("Sum of " + n1 + " and " + n2 + " is " + n3);
Console.ReadLine();
}
}
}
Press F5.
13
www.manzoorthetrainer.com
C#.NET-Simplified
In this chapter we have seen addition of two integers and double numbers.
Display methods using various techniques.
Reading values from keyboard.
Int.Parse and double.Parse for conversion.
Thank you..!
14
www.manzoorthetrainer.com
C#.NET-Simplified
15
www.manzoorthetrainer.com
C#.NET-Simplified
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace TypeCastEg
{
class Program
{
static void Main(string[] args)
{
int a = 5;
int b = 2;
Console.WriteLine(a/b);
Console.ReadLine();
}
}
}
16
www.manzoorthetrainer.com
C#.NET-Simplified
5+2=7.
If we say 5.0+2=7.0.
See that there is not much difference in mathematical concept 7 and 7.0 both
are same.
Whereas in our computer world 7 and 7.0 has lot of difference in their data
structure.
If we say 5+2.0 definitely our result would be 7.0.
If we say 5.0+2.0 our result would be again same as 7.0.
One thing we need to observe here is 5 is our first operand 2 is our second
operand and + called as operator.
If our both the operands are integer our result is integer.
One of the two operands is double then our result is double.
If anyone operand is double our result will be double.
In the same way we are performing 5/2 the result will be 2.
Why because 5 is an integer 2 is an integer, so integer/integer will gives
rise to integer?
We want the result to be double what is that we need to do, we need to
make either 5 as double or 2 as double or both of them as double.
Well make 5 as double.
5.0/2 this will give the result as 2.5.
17
www.manzoorthetrainer.com
C#.NET-Simplified
We got 2.5.
Now what we want weve a/b, now how do we make this as in points.
If we make int as double.
This could be one of the solution but this is not correct way.
Why because only once we want to perform division but maybe we know n
number of times we want to perform addition, subtraction and multiplication,
so in that cases if we want to add two integer it takes very very less amount
of time when we compare it with adding an integer and a double.
Adding an integer with double takes huge amount of time whereas it takes
less amount of time to add two integers.
So may be in our program we need only once for division may be n number
of times we may be adding it.
So addition of two integers takes very very less amount of time when
compare to an addition of integer and a double.
We dont want to change the data type we want it to be an integer.
But at the time of performing division operation we want to make this as
double.
18
www.manzoorthetrainer.com
C#.NET-Simplified
19
www.manzoorthetrainer.com
C#.NET-Simplified
Our program execution will come till this point and it will stop.
From there we want to see the execution we can just press F11 to see the
execution one step after the other.
Or we can just press F5 to see the complete result or to ignore this break
point.
Now well just press F5.
That means weve converted or weve type casted the value of a from
integer to double.
For example if weve 5 billion dollars of business.
We want to divide the profit in two equal shares to share holder A and
shareholder B.
20
www.manzoorthetrainer.com
C#.NET-Simplified
If we do not type cast of this things then well be simply losing 0.5 million
dollars, so that is not a small amount.
So what is that we need to do we need to go for type casting.
Concept is very small but very important.
See the code given below.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace TypeCastEg
{
class Program
{
static void Main(string[] args)
{
int a = 5;
int b = 2;
Console.WriteLine((double)a/b);
Console.ReadLine();
}
}
}
21
www.manzoorthetrainer.com
C#.NET-Simplified
22
www.manzoorthetrainer.com
C#.NET-Simplified
23
www.manzoorthetrainer.com
C#.NET-Simplified
1. Conditional:
If and If-else Condition:
Start visual studio choose new project.
And give it a name.
Click on ok.
In this program well check greater number between two variables.
We have declared two variable with some values.
And well check greater number, for this we need to write if (a>b) condition.
24
www.manzoorthetrainer.com
C#.NET-Simplified
In above snap we can see that value of a is 78, and value of b is 45 then a
greater than b condition become true.
If we press F11 it will going to execute Cosole.WriteLine() statement.
Now statement Greater is 78 will be displayed.
25
www.manzoorthetrainer.com
C#.NET-Simplified
26
www.manzoorthetrainer.com
C#.NET-Simplified
First it will check first condition a>b if its true then print the result.
And skip the else part and then comes out of the block
27
www.manzoorthetrainer.com
C#.NET-Simplified
28
www.manzoorthetrainer.com
C#.NET-Simplified
Else-if ladder:
In this chapter well see greatest of three numbers using else-if ladder.
Choose another console application.
To find greatest of three numbers we need three variables.
29
www.manzoorthetrainer.com
C#.NET-Simplified
Press F5.
30
www.manzoorthetrainer.com
C#.NET-Simplified
First it will check if condition a>b and a>c, here both are false so its skip the
if block and enter into else block.
In else block weve again if condition, it checks the condition for if (b>a &&
b>c) here both parts are true so whole condition becomes true, so control
enters into if block and executes the statement and comes out of the block.
It need not to go for the else part.
31
www.manzoorthetrainer.com
C#.NET-Simplified
Its working fine but logic become more complex and it gives tough
readability.
In above program weve used many nested if-else.
As many nested if-else becomes more complex to understand.
To overcome this issue we have one solution i.e. else-if ladder.
Its very simple.
32
www.manzoorthetrainer.com
C#.NET-Simplified
Above code works same as earlier code but it gets easier than earlier to
write as well as to understand.
In the above situation it checks first if block, if all conditions in this block
become true then it skips remaining block.
Put a break point check the execution for above code.
33
www.manzoorthetrainer.com
C#.NET-Simplified
If a=934 then first condition block become true and skip all remaining elseif ladder block.
Now print the result as Greatest is 934.
Complete code is given below.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace ElseIfLadderEg
{
class Program
{
static void Main(string[] args)
{
int a = 934;
int b = 78;
int c = 67;
int d = 678;
else
{
if (b > a && b > c && b > d)
Console.WriteLine("Greatest is " + b);
34
www.manzoorthetrainer.com
C#.NET-Simplified
else
{
if (c > a && c > b && c > d)
Console.WriteLine("Greatest is " + c);
else
Console.WriteLine("Greatest is " + d);
}
}
Console.ReadLine();
}
}
}
35
www.manzoorthetrainer.com
C#.NET-Simplified
Switch case:
In this chapter we are going to write a program which accepts a value from
keyboard from 1-4 and depending upon the input value it is going to perform
some mathematical operations.
If input value is 1-add two integers.
If input is 2- subtract two integers.
If input value is 3-for multiply.
If input value is 4-divide two integers.
These options should be enabled for the end-user to enter the choice.
This can be achieve by using switch case.
When should we go for switch case, well go for switch case whenever weve
some multiple options we need to select one then we should go with switch
cases.
Same thing we might have observe when we have multiple options once we
go for swiping our ATM card in ATM machine.
It asks for saving account or current account.
If we say saving account.
Then it asks for the operations do you want to perform.
Do you want to deposit amount or withdraw amount or balance enquiry.
We have multiple things and we need to select one thing.
So that kind of functionality whenever we want to implement we should go
with switch cases.
Here we need to display the options first.
And read the choice.
36
www.manzoorthetrainer.com
C#.NET-Simplified
Based on switch (ch) value we are entering into the cases. (ch is user
choice).
We are giving user option in ch.
If it is 1 then it will jump into the case 1 and execute addition operation.
And it will break.
As soon as it encounters the break statement i.e. unconditional statement,
without looking for any kind of condition it will directly jump out of the block.
37
www.manzoorthetrainer.com
C#.NET-Simplified
We have one default case, if choice of the user is other than these cases
then it enters into default case.
Let us see the execution, put a break point and press F5.
User choice is 3.
38
www.manzoorthetrainer.com
C#.NET-Simplified
If user enters the value other than these choices then it jumps into default
case and prints Invalid Input.
One more thing this cases need not be in order we can change the order of
cases also.
We can start with case 3 and we can end it with case 2.
We can give special characters as options in switch but we have to give
same characters in double quotation as cases.
And complete code is given below.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace SwitchCaseEg
{
class Program
{
static void Main(string[] args)
{
int a = 45;
int b = 34;
Console.WriteLine("+.Add -.Sub *.Mul /.Div");
string ch =Console.ReadLine();
39
www.manzoorthetrainer.com
C#.NET-Simplified
switch (ch)
{
case "*":
Console.WriteLine(a * b);
break;
case "/":
Console.WriteLine((double)a / b);
break;
case "+":
Console.WriteLine(a + b);
break;
case "-":
Console.WriteLine(a - b);
break;
default:
Console.WriteLine("Invalid Input");
break;
}
Console.ReadLine();
}
}
}
Press F5.
40
www.manzoorthetrainer.com
C#.NET-Simplified
For loop:
Let us start new project where we will write sample program to explain
for loop.
Program Visual studioFileNew projectConsole application.
Name it as ForEg.
41
www.manzoorthetrainer.com
C#.NET-Simplified
In this section well write the program to display the name for 10 times
using for loop.
For loop is used for definite iteration, which means we know the number
of iterations prior to execution.
For example
for(int i=0;i<10;i++)
{
}
It will start from 0 and executes whatever we write in this block for 10
times.
From 0 till 9, i value will vary from 0 till 9.
Now well display message for 10 times.
Press F5.
42
www.manzoorthetrainer.com
C#.NET-Simplified
43
www.manzoorthetrainer.com
C#.NET-Simplified
In earlier code weve displayed name, whereas here we are trying to display
the value of i.
it will display all integers from 1 to 10.
Press F5.
44
www.manzoorthetrainer.com
C#.NET-Simplified
Here i value is initialized to 10, check the condition for i >=1 means till 1, and
use decrement operator.
45
www.manzoorthetrainer.com
C#.NET-Simplified
46
www.manzoorthetrainer.com
C#.NET-Simplified
namespace ForEg
{
class Program
{
static void Main(string[] args)
{
for (int i = 0; i < 10; i++)
{
Console.WriteLine("Manzoor");
}
47
www.manzoorthetrainer.com
C#.NET-Simplified
if (i % 2 == 1)
Console.WriteLine(i);
}
int n = 6;
Console.ReadLine();
}
}
}
While loop:
Program Visual studioFileNew projectConsole application.
Name it as WhileEg.
While loop is used whenever we have the situation of indefinite iterations.
Means number of iterations are unknown prior to execution we go for while
loop
Here is the simple code for understand while loop.
48
www.manzoorthetrainer.com
C#.NET-Simplified
It looks similar to our for loop but to understand we are using while loop to
implement this task.
Because if number of iterations are known prior to execution we should go
with for loop.
While loop is specially designed for those iterations which are unknown prior
to execution.
Well put a break point it very simple and straight forward.
First I value will be 0 then it checks the condition i.e. 0<10 condition becomes
true.
It will executes the statement and I value gets incremented.
Again it jumps to the condition.
49
www.manzoorthetrainer.com
C#.NET-Simplified
50
www.manzoorthetrainer.com
C#.NET-Simplified
In above case while loop is used because number of iterations are unknown
as a number can have n number of digits.
Complete code is given for sum of the digits is given below.
51
www.manzoorthetrainer.com
C#.NET-Simplified
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace WhileEg
{
class Program
{
static void Main(string[] args)
{
int i = 0;
while (i < 10)
{
Console.WriteLine("ManzoorTheTrainer");
i++;
}
int n = int.Parse(Console.ReadLine());
int sum = 0;
int r;
while (n != 0)
{
r = n % 10;
sum = sum + r;
n = n / 10;
}
52
www.manzoorthetrainer.com
C#.NET-Simplified
Console.ReadLine();
}
}
}
Press F5.
Do-While loop:
Do-while loop is used again, whenever we have indefinite number of
iterations.
But difference between while and do-while is, while checks the condition first
then execute the statements whereas do-while it executes the statements
first and then it goes for checking the condition.
We can use do-while loop when iterations are unknown and need to
executes the task at least once.
Weve unknown number of iterations and we need to execute the task at
least once in that kind of situations we should go for do-while loop.
53
www.manzoorthetrainer.com
C#.NET-Simplified
54
www.manzoorthetrainer.com
C#.NET-Simplified
namespace DoWhileEg
{
class Program
{
static void Main(string[] args)
{
int n;
do{
n=int.Parse(Console.ReadLine());
}
while(n!=0);
55
www.manzoorthetrainer.com
C#.NET-Simplified
Press F5.
56
www.manzoorthetrainer.com
C#.NET-Simplified
Chapter 4: Arrays
In this chapter we are going to deal with arrays.
What is an array?
Array is nothing but collection of similar type of elements or collection of
homogeneous data type elements.
For example if we have a collection of elements.
We want to store ids of 100 student, so it is very tough or difficult to us to
declare 100 variables.
So instead of that well go for the concept of Arrays.
Well try to understand the concept of arrays in which we are planning to
store all integer type elements.
Start new project.
Program Visual C# Express EditionFileNew projectConsole
application.
Name it as ArraysEg.
57
www.manzoorthetrainer.com
C#.NET-Simplified
58
www.manzoorthetrainer.com
C#.NET-Simplified
Press F11.
Memory gets allocated see that int [10].
If we observe the index we say that index value zero, at 0th location the
default value is null.
So we have 10 locations index ranging from 0-9.
If we say element at 0th location means we are referring to the first element.
If we say element at 5th index means we are referring to the sixth element.
This is how weve declared an array of 10 integers.
To access the elements of an array well be using A[index].
A [0] =67 that means we are trying to store 67 in 0th location.
A[1]=A[2]=A[3]=56 this means we are trying to store 56 in 3rd location and
that value trying to store in 2nd location and the same value trying to store
at 1st index.
That means our 2nd, 3rd, and 4th element will be 56.
A[4]=A[1]+A[2]+A[3];
That means the value of A [1], A [2], A [3] gets added and the result will be
stored in A [4].
That means at index 4 the location is 5th.
59
www.manzoorthetrainer.com
C#.NET-Simplified
But last 9th and 10th elements are accepting from keyboard.
Put a break point and press F5.
60
www.manzoorthetrainer.com
C#.NET-Simplified
Press enter.
If we observe all the locations got filled with all the elements.
This is how we can access the elements or we can store the values in an
array.
Now we want to display all the values.
So we can simply write Cosole.WriteLine (A[0]) for first element.
Cosole.WriteLine (A[1]) for second element.
Cosole.WriteLine (A[2]) for third element.
Like that we need to write till 9th location.
If we observe everything is same only index value varying from 0 to 9.
So we can re-write this and we can use the concept of for loop.
For display all the elements we have code below.
61
www.manzoorthetrainer.com
C#.NET-Simplified
Press F5.
namespace ArraysEg
{
class Program
{
static void Main(string[] args)
{
int[] A;
A = new int[10];
62
www.manzoorthetrainer.com
C#.NET-Simplified
A[0] = 67;
A[1] = A[2] = A[3] = 56;
A[4] = A[1] + A[2] + A[3];
A[5] = A[6] = A[7] = A[4] - 10;
A[8] = A[9] = int.Parse(Console.ReadLine());
}
Console.ReadLine();
}
}
}
Press F5.
63
www.manzoorthetrainer.com
C#.NET-Simplified
64
www.manzoorthetrainer.com
C#.NET-Simplified
65
www.manzoorthetrainer.com
C#.NET-Simplified
Now we can use same for loop for displaying all the elements.
Press F5.
This is how we displays all the elements that means we are iterating through
all the elements one after the other from location 0 to till last location.
To iterate through all the element of an array or to iterate through list of
elements or list of objects we can use foreach loop.
It will first extract the top element of the array and it will store it in k.
66
www.manzoorthetrainer.com
C#.NET-Simplified
Press Enter.
67
www.manzoorthetrainer.com
C#.NET-Simplified
68
www.manzoorthetrainer.com
C#.NET-Simplified
69
www.manzoorthetrainer.com
C#.NET-Simplified
70
www.manzoorthetrainer.com
C#.NET-Simplified
namespace ArraysEg2
{
71
www.manzoorthetrainer.com
C#.NET-Simplified
class Program
{
static void Main(string[] args)
{
//int[] A;
//A = new int[5];
Console.WriteLine("Enter 5 Element");
for (int i = 0; i < A.Length; i++)
{
A[i] = int.Parse(Console.ReadLine());
}
Array.Sort(A);
Array.Reverse(A);
72
www.manzoorthetrainer.com
C#.NET-Simplified
}
int sum = 0;
for (int i = 0; i < A.Length; i++)
{
sum = sum + A[i];
}
Console.WriteLine("Sum is " + sum);
//foreach (int k in A)
//{
//
if(k%2==0)
//
Console.WriteLine(k);
//}
Console.ReadLine();
}
}
}
73
www.manzoorthetrainer.com
C#.NET-Simplified
74
www.manzoorthetrainer.com
C#.NET-Simplified
75
www.manzoorthetrainer.com
C#.NET-Simplified
Press Ok.
We can also say structure is a user defined data types.
Like we have integer it is a predefined data type.
We can create the variables of integer type.
In the same way we can define the structure and we can create the variables
of structures.
We need to define structure of the student.
Structure of the student includes student id, student name, and student
marks.
Well be declaring structure above the class.
Till now weve learnt written the code inside the class.
Now well writing some code outside the class.
Now well defining the structure.
76
www.manzoorthetrainer.com
C#.NET-Simplified
77
www.manzoorthetrainer.com
C#.NET-Simplified
See the difference for i it allocates single memory allocation of type integer.
But for structure variable s it allocates 3 memory allocation with different
data types.
S is a structure variable and we have three fields in it Sid, SName, SAvg.
We can access these Sid, SName, SAvg with the help of Period operator or
dot operator.
Like I was accessing the elements of an array using indexes A[0] where 0
is an index.
In the same way we can access fields of structure using Period operator.
78
www.manzoorthetrainer.com
C#.NET-Simplified
Press F5.
79
www.manzoorthetrainer.com
C#.NET-Simplified
Structure variable has 5 indexes S[0]-S[4] and each index has three
members.
We are accessing the elements with the help of index.
Now each element is of type structure.
So S[0].Sid, S[0].SName in this way we can access.
Now well try to store records of two students below.
80
www.manzoorthetrainer.com
C#.NET-Simplified
Instead of writing same statement again and again we can give index value
in for loop and accessing the values from keyboard.
Well try to accept the values or information about 5 students.
And we are trying to store in an array of structure s.
So we can proceed with for loop.
81
www.manzoorthetrainer.com
C#.NET-Simplified
82
www.manzoorthetrainer.com
C#.NET-Simplified
In this scenario foreach works as, it takes the each record from collection S
and stores in k and displays it.
Put a break point and see the execution.
Press F5.
It asks for enter 3 student records.
83
www.manzoorthetrainer.com
C#.NET-Simplified
84
www.manzoorthetrainer.com
C#.NET-Simplified
Now well display the student records who got average more than 80.
Simply we need to filter.
85
www.manzoorthetrainer.com
C#.NET-Simplified
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace StructEg
{
struct Student
{
public int Sid;
public string SName;
public double SAvg;
}
class Program
{
static void Main(string[] args)
{
//Student s;
//s.Sid = 1221;
//s.SName = "Manzoor";
//s.SAvg = 88.9;
86
www.manzoorthetrainer.com
C#.NET-Simplified
Console.WriteLine("Enter SName");
S[i].SName = Console.ReadLine();
Console.WriteLine("Enter Avg");
S[i].SAvg = double.Parse(Console.ReadLine());
}
87
www.manzoorthetrainer.com
C#.NET-Simplified
Console.WriteLine("Record");
Console.ReadLine();
}
}
}
88
www.manzoorthetrainer.com
C#.NET-Simplified
See the details weve entered 3 records but as per average it displays 2
records.
Record 1 and 3 who secured more than 80%.
In this chapter we have seen structures and various methods to access it.
89
www.manzoorthetrainer.com
C#.NET-Simplified
Chapter 7 : Class
Class, Object, Public, Private, Method, Constructor, and constructor
overloading in C#.
In this chapter well see what a class is, public and private members, and
methods.
90
www.manzoorthetrainer.com
C#.NET-Simplified
If we do not write any things or any access specifiers (i.e. public, private) by
default the fields of the class are private.
Private means cannot be accessible from outside the class.
And Public means can access it from outside of the class.
Now in this class we need to declare few methods.
Methods are nothing but behaviours.
So whenever a new customer comes to the bank or new customer wants to
create an account so we need to assign account number, name and balance
to that particular customer.
91
www.manzoorthetrainer.com
C#.NET-Simplified
If we observe we write void because our method is not returning any value.
Whereas it is taking three values, we call it as parameters.
So it is taking three parameters one is for account number, another is name
and one more is balance.
So this is the method which we used to create an object.
If we want to display the details of that particular customer or we can say
balance enquiry.
92
www.manzoorthetrainer.com
C#.NET-Simplified
93
www.manzoorthetrainer.com
C#.NET-Simplified
If we observe we will not show fields in their members because they are
private by default.
If we make them as public then we can access it.
But as per the rules of a class objects should not have access the fields
directly.
Thats way we making the fields as private.
So as we might have observed in structures we declared all the fields of
structure as public so that we should access them.
So this is our class and we creates the customer and we pass three
parameters.
With this we create the customer.
To display the details we called BalEnq() method.
Press F5.
94
www.manzoorthetrainer.com
C#.NET-Simplified
95
www.manzoorthetrainer.com
C#.NET-Simplified
The next point is we can re initializing the object by calling that method
again and again that problem is solved with this feature of constructor that
we cannot invoke the constructor explicitly.
Now we are declaring two constructors in class customer one with no
parameter called as Default constructor.
And another is with 3 parameters.
96
www.manzoorthetrainer.com
C#.NET-Simplified
Press F5.
97
www.manzoorthetrainer.com
C#.NET-Simplified
98
www.manzoorthetrainer.com
C#.NET-Simplified
99
www.manzoorthetrainer.com
C#.NET-Simplified
Execute this.
100
www.manzoorthetrainer.com
C#.NET-Simplified
So weve taken this example to explain that we can have more than one
method with same name in a class but different parameters as we have more
than one constructor with different parameters.
This concept is called as method overloading.
Method overloading is also called as static polymorphism.
101
www.manzoorthetrainer.com
C#.NET-Simplified
Static means compile time poly means many morphism means forms
So in our program we have many forms.
One balance enquiry with no parameters and another with two parameters.
So we can understand that it is polymorphism.
We call it as polymorphism because without executing the program at
compile time itself we knew that balance enquiry with two parameters going
to invoke balance enquiry method with two parameters.
That means code for this method is generated at compile time itself that
means it need not to wait for runtime.
Our code gets generated at compile time itself.
So at compile time we knew that what method is going to be invoked so we
call it as static.
Polymorphism means there are many forms of single balance enquiry.
So we call it as static polymorphism.
Complete code is given below.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace ClassEg1
{
class Customer
{
int AcNo;
string Name;
double Bal;
102
www.manzoorthetrainer.com
C#.NET-Simplified
public Customer()
{
Console.WriteLine("Hello World");
}
AcNo = a;
//
Name = n;
//
Bal = b;
//}
103
www.manzoorthetrainer.com
C#.NET-Simplified
}
class Program
{
static void Main(string[] args)
104
www.manzoorthetrainer.com
C#.NET-Simplified
{
Customer c = new Customer(1234, "Jack", 78000);
//c.BalEnq();
c.BalEnq();
c.Deposit(3000);
c.BalEnq();
c.WithDraw(5000);
c.BalEnq();
c.BalEnq(4500, "D");
Console.ReadLine();
}
}
}
105
www.manzoorthetrainer.com
C#.NET-Simplified
106
www.manzoorthetrainer.com
C#.NET-Simplified
107
www.manzoorthetrainer.com
C#.NET-Simplified
Press F5.
108
www.manzoorthetrainer.com
C#.NET-Simplified
109
www.manzoorthetrainer.com
C#.NET-Simplified
From here at the occurrence of this keyword with three parameters control
jumps to three parameterized constructor to initialize that parameters.
110
www.manzoorthetrainer.com
C#.NET-Simplified
111
www.manzoorthetrainer.com
C#.NET-Simplified
See it executes the default constructor first then it executes the three
parameterized constructor then finally it executes four parameterized
constructor.
So this is all about this keyword.
Complete program is given below.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace thisKeyWordEg
112
www.manzoorthetrainer.com
C#.NET-Simplified
{
class Customer
{
int AcNo;
string Name;
double Bal;
string Contact;
public Customer()
{
Console.WriteLine("Hello World");
}
113
www.manzoorthetrainer.com
C#.NET-Simplified
114
www.manzoorthetrainer.com
C#.NET-Simplified
class Program
{
static void Main(string[] args)
{
Customer c = new Customer(123, "Jack", 78000, "9676010101");
c.BalEnq();
c1.BalEnq();
Console.ReadLine();
}
}
}
115
www.manzoorthetrainer.com
C#.NET-Simplified
In this class weve account number of the person who has taken the housing
loan and the name, loan amount and the rate of interest that we are
applying on the loan amount.
If we observe if there are n number of customers who has taken housing
loan all of them may have different account numbers, names and loan
amounts whereas rate of interest would be same for each and every
customer.
So if we create 10 objects or 10 customers then we need to allocate 10
memory locations for ROI.
And all the 10 location have same value.
So which results in wastage of memory and another drawback is that if we
wants to update ROI we need to update it in all the objects.
We need to allocate memory for ROI only once and allow all the objects to
access it.
So we just make ROI as static and also make it as public so that we can
access it from outside the class to set the value of ROI.
116
www.manzoorthetrainer.com
C#.NET-Simplified
Now this three things we need to initialize whenever we need to create the
object.
So for that well use constructor following constructor.
117
www.manzoorthetrainer.com
C#.NET-Simplified
Press F5.
118
www.manzoorthetrainer.com
C#.NET-Simplified
Press F5.
119
www.manzoorthetrainer.com
C#.NET-Simplified
Now if weve taken loan amount of 56000 at the ROI 12.8 so we need to
repay them 63168.
If ROI gets updated to 13.8 then we need to repay them 63728.
So this our static variable works.
Now as per the standards and rules we need to declare the variable as
private.
If we make static variable as private it throws an error in main method it says
that we cannot access the private members outside the class.
What is that we can do?
We can write a separate constructor where we can initialize static members.
So well write constructor using which we can initialize static variable.
So that is nothing but our static constructor.
120
www.manzoorthetrainer.com
C#.NET-Simplified
121
www.manzoorthetrainer.com
C#.NET-Simplified
122
www.manzoorthetrainer.com
C#.NET-Simplified
Here weve take 68000 loan and we are going to repay it 12 months.
Above code has two same static methods but with different parameters.
So this is the concept of static method overloading.
123
www.manzoorthetrainer.com
C#.NET-Simplified
At this time well call the method with single parameter i.e. loan amount.
Here weve called two same methods but with different parameters.
Press F5.
124
www.manzoorthetrainer.com
C#.NET-Simplified
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace StaticEg
{
class HousingLoan
{
int AcNo;
string Name;
double LoanAmount;
static double ROI;
static HousingLoan()
{
ROI = 12.8;
}
125
www.manzoorthetrainer.com
C#.NET-Simplified
double LoanAmount)
{
this.AcNo = AcNo;
this.Name = Name;
this.LoanAmount = LoanAmount;
}
}
class Program
{
126
www.manzoorthetrainer.com
C#.NET-Simplified
//HousingLoan.ROI = 13.8;
h1.Display();
h.Display();
HousingLoan.Enq(68000, 12);
HousingLoan.Enq(45000);
Console.ReadLine();
}
}
}
127
www.manzoorthetrainer.com
C#.NET-Simplified
Press F5.
128
www.manzoorthetrainer.com
C#.NET-Simplified
Class student contain some fields, constructor for initializing the student id
and student name but we are not initializing the marks because we need to
re initialize the marks for each and every exam so we kept it in separate
method for assigning the marks.
And weve two methods.
One is for set marks and another is for display average details of student.
Let us create the object of student and well call two methods.
Press F5.
129
www.manzoorthetrainer.com
C#.NET-Simplified
130
www.manzoorthetrainer.com
C#.NET-Simplified
Press F5.
131
www.manzoorthetrainer.com
C#.NET-Simplified
Whenever we want to set only marks m1 well just call s.setm1 () method.
Here we have three set methods separately and three get methods.
And each and every time we need to call the method to get the value.
132
www.manzoorthetrainer.com
C#.NET-Simplified
In set method weve value is a default variable which will store the value
whatever we going to assign.
And get method returns m1.
This is our simple m1 property works like setm1 and getm1 methods.
In this property weve one setter method and a getter method.
Whenever we want to assign the value of m1 we can simple write s.M1=67 to
set 67 for marks m1.
Like that well create properties for m2 and m3.
133
www.manzoorthetrainer.com
C#.NET-Simplified
So this are the best practices whenever we go for fields it is good to define
the properties of each and every fields.
Now we have many situations where we need to access student id.
So for student id we can also define the property.
134
www.manzoorthetrainer.com
C#.NET-Simplified
135
www.manzoorthetrainer.com
C#.NET-Simplified
namespace PropertiesEg
{
class Student
{
int Sid;
string SName;
double m1, m2, m3;
public double M1
{
set { m1 = value; }
get { return m1; }
}
public double M2
{
set { m2 = value; }
get { return m2; }
}
public double M3
{
set { m3 = value; }
get { return m3; }
136
www.manzoorthetrainer.com
C#.NET-Simplified
return m1;
//}
return m2;
//}
return m3;
//}
137
www.manzoorthetrainer.com
C#.NET-Simplified
this.m1 = m1;
//}
//public void Setm2(double m2)
//{
//
this.m2 = m2;
//}
//public void Setm3(double m3)
//{
//
this.m3 = m3;
//}
class Program
{
static void Main(string[] args)
{
Student S = new Student(123, "Peter");
S.DisplayAvg();
138
www.manzoorthetrainer.com
C#.NET-Simplified
S.M1 = 67;
S.M2 = 78;
S.M3 = 58;
Console.WriteLine("M1:" + S.M1);
Console.WriteLine("M2:" + S.M2);
Console.WriteLine("M3:" + S.M3);
S.DisplayAvg();
//S.SetMarks(34, 56, 78);
//S.DisplayAvg();
//Console.WriteLine("M1:" + S.Getm1());
//Console.WriteLine("M2:" + S.Getm2());
//Console.WriteLine("M3:" + S.Getm3());
//S.Setm1(67);
//S.DisplayAvg();
Console.ReadLine();
}
}
}
139
www.manzoorthetrainer.com
C#.NET-Simplified
Both of the team drop their codes and they try to integrate but at the time of
creating an object of class student it throws an error.
140
www.manzoorthetrainer.com
C#.NET-Simplified
Here we have TeamA and TeamB namespaces for two different classes of
student.
Now at the time of creation of object we need to use TeamA.Student
instead of Student.
Means at the time of creation of object weve to use namespace name
before class name.
141
www.manzoorthetrainer.com
C#.NET-Simplified
Like that we can create the object of team B but we need to use
TeamB.Student.
Using this object we can call the method of student.
142
www.manzoorthetrainer.com
C#.NET-Simplified
Press F5.
143
www.manzoorthetrainer.com
C#.NET-Simplified
Then instead of writing complete path of the class we can simply write Test
to create an object as below.
Press F5.
144
www.manzoorthetrainer.com
C#.NET-Simplified
And it is use to avoid naming conflicts for the classes and to have good
understanding of codes developed by teams of particular organization.
So this is all about namespaces.
Complete code is given below.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using TeamB.GroupA;
//namespace TeamA
//{
//
class Student
//
//
//
//
//
//}
namespace TeamB
{
class Student
{
public void Show()
{
}
145
www.manzoorthetrainer.com
C#.NET-Simplified
class Teacher
{
public void GetSalary()
{
}
}
namespace GroupA
{
class Test
{
public void Go()
{
System.Console.WriteLine("This is a Go method of class Test which is in Group A
namespace of TeamB Namespace");
}
}
}
}
namespace NameSpaceEg
{
class Program
{
static void Main(string[] args)
{
//TeamA.Student S = new TeamA.Student();
146
www.manzoorthetrainer.com
C#.NET-Simplified
t.Go();
System.Console.ReadLine();
}
}
}
147
www.manzoorthetrainer.com
C#.NET-Simplified
Well see how to implement inheritance and one more access specifier i.e.
protected and various types of inheritance.
Now well simulate this with the help of an example class A.
In this class weve two variables.
One is int x, and another is public int y.
As usual if we create the object of class A then definitely we can access the
public variables whereas we cannot access the private variables from
outside the class.
148
www.manzoorthetrainer.com
C#.NET-Simplified
149
www.manzoorthetrainer.com
C#.NET-Simplified
150
www.manzoorthetrainer.com
C#.NET-Simplified
151
www.manzoorthetrainer.com
C#.NET-Simplified
From this example we can say that if a class is standalone class protected
and private variables are one and the same things.
We cannot access them from outside the class.
If we going for inheritance protected variables get inherited that means well
have even protected variable z in class B.
Protected variables get inherited and public variables get inherited whereas
private variables will never get inherited into the derived class.
And we cannot access protected variables from outside the class means full
protection.
Complete code is given below.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace InheritanceEg
{
//Base Class
class A
{
152
www.manzoorthetrainer.com
C#.NET-Simplified
private int x;
public int y;
protected int z;
}
//Drived class
class B:A
{
//protected int z;
//public int y;
private int l;
public int m;
class Program
{
static void Main(string[] args)
{
A a = new A();
//Can Access a.y;
153
www.manzoorthetrainer.com
C#.NET-Simplified
B b = new B();
// Can Access b.m;
// Cannot Access b.l;
// Can Access b.y;
// Cannot Access b.x
// Cannot Access b.z
}
}
}
If we observe the code private variables provides the security but not gets
inherited.
Public variables get inherited but not provides the security.
Solution to this problem is protected variables.
Protected variables get inherited as well as provides the full security.
154
www.manzoorthetrainer.com
C#.NET-Simplified
155
www.manzoorthetrainer.com
C#.NET-Simplified
156
www.manzoorthetrainer.com
C#.NET-Simplified
Press F11.
Now whenever we are trying to create the object of class A it is going to
invoke its constructor its fair enough.
157
www.manzoorthetrainer.com
C#.NET-Simplified
158
www.manzoorthetrainer.com
C#.NET-Simplified
Now what well do instead of creating the object of class B well simply
create the object of class c.
159
www.manzoorthetrainer.com
C#.NET-Simplified
160
www.manzoorthetrainer.com
C#.NET-Simplified
161
www.manzoorthetrainer.com
C#.NET-Simplified
If we execute this it shows same output as earlier because our derived class
constructor going to invoke its immediate base class default constructor.
162
www.manzoorthetrainer.com
C#.NET-Simplified
As per the normal rules our class C object will automatically going to invoke
Cs default constructor.
From here in our earlier example our control was jumping to default
constructor of base class.
But now we are saying that base with two parameters that means we are
explicitly calling the base class constructor with two parameters.
So our control should jumps from here to two parameter constructor of base
class.
Now here we are not calling any constructors of base class explicitly.
So by default it is going to jump to the default constructor of base class.
163
www.manzoorthetrainer.com
C#.NET-Simplified
164
www.manzoorthetrainer.com
C#.NET-Simplified
Now the job of derived class to invoke its immediate base class constructor
default constructor.
But from here we are explicitly calling base class constructor with single
parameter.
In this way we can change the constructor chaining as per our requirements.
This is all about constructor chaining, base keyword and multi-level
inheritance.
Complete code is given below.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace ConstructorChainingEg
{
165
www.manzoorthetrainer.com
C#.NET-Simplified
class A
{
public A()
{
Console.WriteLine("A's Constructor with 0 Params");
}
public A(int x)
{
Console.WriteLine("A's Constructor with 1 Params");
}
}
class B:A
{
public B():base(5)
{
Console.WriteLine("B's Constructor with 0 Params");
}
class C : B
{
public C():base(2,4)
166
www.manzoorthetrainer.com
C#.NET-Simplified
{
Console.WriteLine("C's Constructor with 0 Params");
}
public C(int x)
{
Console.WriteLine("C's Constructor with 1 Params");
}
}
class Program
{
static void Main(string[] args)
{
//A a = new A();
C c = new C(5);
Console.ReadLine();
}
}
}
167
www.manzoorthetrainer.com
C#.NET-Simplified
168
www.manzoorthetrainer.com
C#.NET-Simplified
So this is our simple class with two methods and three properties.
169
www.manzoorthetrainer.com
C#.NET-Simplified
Press F5.
170
www.manzoorthetrainer.com
C#.NET-Simplified
Whenever we dont have any constructor in the class then we need not
to write default constructor it will take automatically.
Whenever we have constructor with parameter then we must need to
create default constructor.
One requirement got fulfilled.
Now next requirement is they need one more subject.
171
www.manzoorthetrainer.com
C#.NET-Simplified
172
www.manzoorthetrainer.com
C#.NET-Simplified
If we observe our base class and derived class has same GetDetails
method with no parameters and same return type.
This complete thing is called as signature i.e. public void GetDetails().
Signature of the method in base class is same as signature of the method
in derived class.
But the implementation is different.
Whenever weve a method with same signature in the derived class as
same as in base class with different implementation then we say the
method of derived class overrides the method of base class.
It means whenever we call GetDetails it executes derived class method
it is not at all executes base class method.
This concept is called as method overriding.
Same thing we need to implement for GetAvg method because previous
method was calculating average for 3 subjects.
But now weve 4 subjects.
173
www.manzoorthetrainer.com
C#.NET-Simplified
174
www.manzoorthetrainer.com
C#.NET-Simplified
Press F5.
175
www.manzoorthetrainer.com
C#.NET-Simplified
So that these three variables will get initialized at base class constructor
of three parameters.
And remaining one parameter get initialized here.
So in this way we can use the concept of constructor chaining.
So this is the use of base keyword.
One more thing if we observe GetDetails method in the derived class
there is three lines of code is same as that we have in base class.
So instead of writing this three lines of code again and again we can
simply call base class method from here.
We can call base class method as below.
So this three things it is executing from there and left out part is executing
from here.
176
www.manzoorthetrainer.com
C#.NET-Simplified
From here our control will jumps to the GetDetails method of NewStudent
class.
177
www.manzoorthetrainer.com
C#.NET-Simplified
So here weve seen two uses of base keyword in the same way weve
saw two uses of this keyword.
One use is to invoke the constructor of base class from the derived class.
And another use is to refer the members of base class as we were using
this keyword to refer to the member of current class.
So here we are utilizing the base keyword and the inheritance concept.
And the constructor chaining concept and all this things weve
implemented in this example.
Complete code is given below.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace MethodOverridingEg
{
class Student
{
protected int Sid;
protected string Name;
protected string Standard;
178
www.manzoorthetrainer.com
C#.NET-Simplified
public Student()
{
}
179
www.manzoorthetrainer.com
C#.NET-Simplified
public
NewStudent(int
Contact):base(Sid,Name,Standard)
Sid,
string
Name,
{
//this.Sid = Sid;
//this.Name = Name;
//this.Standard = Standard;
this.Contact = Contact;
}
180
www.manzoorthetrainer.com
string
Standard,string
C#.NET-Simplified
181
www.manzoorthetrainer.com
C#.NET-Simplified
else if(avg<70)
Console.WriteLine("Passed in first Division");
else if(avg<=100)
Console.WriteLine("Passed in Dist");
}
}
class Program
{
static void Main(string[] args)
{
Student s = new Student(124, "Peter", "X");
s._Maths = 67;
s._Science = 78;
s._Social = 66;
s.GetDetails();
s.GetAvg();
ns.GetDetails();
ns.GetAvg();
ns.GetDivision();
182
www.manzoorthetrainer.com
C#.NET-Simplified
Console.ReadLine();
}
}
}
183
www.manzoorthetrainer.com
C#.NET-Simplified
184
www.manzoorthetrainer.com
C#.NET-Simplified
185
www.manzoorthetrainer.com
C#.NET-Simplified
186
www.manzoorthetrainer.com
C#.NET-Simplified
187
www.manzoorthetrainer.com
C#.NET-Simplified
188
www.manzoorthetrainer.com
C#.NET-Simplified
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace AbstractEg
{
abstract class A
{
public abstract void show();
class B : A
{
public override void show()
{
Console.WriteLine("This is Show Method");
}
public void View()
{
Console.WriteLine("This is View Method");
}
}
189
www.manzoorthetrainer.com
C#.NET-Simplified
class Program
{
static void Main(string[] args)
{
A a;
B b = new B();
b.Display();
b.View();
b.show();
Console.ReadLine();
}
}
}
190
www.manzoorthetrainer.com
C#.NET-Simplified
191
www.manzoorthetrainer.com
C#.NET-Simplified
192
www.manzoorthetrainer.com
C#.NET-Simplified
First of all well try to implement with two classes employee and lab
assistant then well go for lecturer and admin.
Initially well make the reference of an employee as an object of lab
assistant.
First of all well implement employee class.
See the implementation.
193
www.manzoorthetrainer.com
C#.NET-Simplified
Here weve created a new class Lab Assistant which inherits employee
class.
Weve used base keyword to initialize some fields at base class only.
And remaining field i.e. lab number is initializing here itself.
And weve Display() as override method.
In that weve used base keyword for displaying some fields in base method
only and one lab number is displaying here.
And weve implemented abstract method.
Now well create a reference of an employee class.
And we make it an object of lab assistant.
And well call Display and calculate salary methods as below.
194
www.manzoorthetrainer.com
C#.NET-Simplified
195
www.manzoorthetrainer.com
C#.NET-Simplified
Depending upon the choices well make the reference the object of that
particular class.
By default it will be an object of lab assistant.
So well use switch to that choice ch.
In switch well give cases for Lab assistant, lecturer and admin.
196
www.manzoorthetrainer.com
C#.NET-Simplified
If we observe this program can we say that what is the method it is going to
display or whose salary it is going to calculate at compile time?
When we execute the end user may give option 1 so it will display methods
from lab assistant and calculate the salary of lab assistant.
If the end user give option 2 so it will displays methods from lecturer and
calculate the salary of lecturer.
If the end user give option 3 so it will displays methods from admin and
calculate the salary of admin.
197
www.manzoorthetrainer.com
C#.NET-Simplified
Our option is 2.
So its going to calculate the salary for lecturer.
Well execute this again.
198
www.manzoorthetrainer.com
C#.NET-Simplified
If we click drag and drop it is going to select the area because our reference
become select area.
Now well make it an object of circle and well execute same method click
drag and drop and leave it will draw circle.
199
www.manzoorthetrainer.com
C#.NET-Simplified
200
www.manzoorthetrainer.com
C#.NET-Simplified
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace RuntimePolymorphismEg
{
abstract class Employee
201
www.manzoorthetrainer.com
C#.NET-Simplified
{
protected int EmpId;
protected string EmpName;
protected double SalPerDay;
string
EmpName,
{
this.LabNo = LabNo;
202
www.manzoorthetrainer.com
double
SalPerDay,int
C#.NET-Simplified
203
www.manzoorthetrainer.com
C#.NET-Simplified
class Program
{
static void Main(string[] args)
{
Employee E;
204
www.manzoorthetrainer.com
C#.NET-Simplified
switch (ch)
{
case 1:
E = new LabAssistant(123, "Peter", 67.7, 34);
break;
case 2:
E = new Lecturer(124, "Tom", 89.6, "MS.Net");
break;
case 3:
E = new Admin(126, "Lilly", 45.8);
break;
case 4:
System.Threading.Thread.CurrentThread.Abort();
break;
default:
break;
}
E.Display();
205
www.manzoorthetrainer.com
C#.NET-Simplified
E.CalculateSalary(25);
Console.ReadLine();
}
}
}
206
www.manzoorthetrainer.com
C#.NET-Simplified
207
www.manzoorthetrainer.com
C#.NET-Simplified
Press F5.
208
www.manzoorthetrainer.com
C#.NET-Simplified
209
www.manzoorthetrainer.com
C#.NET-Simplified
So its shows an error we cannot derived new class from sealed class.
But we can create an object of sealed class.
It is very simple.
And its not like abstract if method is abstract class must be abstract.
Here if method is sealed there may be class is sealed or may not be sealed.
One more difference is we cannot create an object of abstract class.
Whereas in sealed we can create an object of sealed class.
Complete code is given below.
210
www.manzoorthetrainer.com
C#.NET-Simplified
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace SealedEg
{
abstract class A
{
public abstract void show();
sealed class B : A
{
public override sealed void show()
{
Console.WriteLine("This is Show Method");
}
public void View()
{
Console.WriteLine("This is View Method");
}
}
211
www.manzoorthetrainer.com
C#.NET-Simplified
//class C : B
//{
//
//
//{
//
//
//
//}
//}
class Program
{
static void Main(string[] args)
{
B b = new B();
Console.ReadLine();
}
}
}
212
www.manzoorthetrainer.com
C#.NET-Simplified
213
www.manzoorthetrainer.com
C#.NET-Simplified
214
www.manzoorthetrainer.com
C#.NET-Simplified
215
www.manzoorthetrainer.com
C#.NET-Simplified
216
www.manzoorthetrainer.com
C#.NET-Simplified
Press F5.
Press F5.
217
www.manzoorthetrainer.com
C#.NET-Simplified
Thats way we cannot invoke view method using the reference of interface
A.
If we making reference of interface A then we can invoke those methods
which are available in interface A.
So we need to create the reference of interface B to invoke view method.
And we cannot invoke neither display nor show methods using reference of
B.
Because interface B has only view method definition.
If we try to access then it shows an error.
Now if we execute.
218
www.manzoorthetrainer.com
C#.NET-Simplified
namespace InterfaceEg
{
interface A
{
void Show();
void Display(string msg);
}
interface B
{
219
www.manzoorthetrainer.com
C#.NET-Simplified
void View();
}
class C:A,B
{
public void Show()
{
Console.WriteLine("This is Show Method");
}
class Program
{
static void Main(string[] args)
{
//C c = new C();
//c.Display("ManzoorTheTrainer");
//c.Show();
//c.View();
A a;
220
www.manzoorthetrainer.com
C#.NET-Simplified
a = new C();
a.Display("ManzoorTheTrainer");
a.Show();
//a.View();
B b;
b = new C();
b.View();
Console.ReadLine();
}
}
}
221
www.manzoorthetrainer.com
C#.NET-Simplified
222
www.manzoorthetrainer.com
C#.NET-Simplified
223
www.manzoorthetrainer.com
C#.NET-Simplified
224
www.manzoorthetrainer.com
C#.NET-Simplified
Now well perform Dequeue operation and display elements after removing.
225
www.manzoorthetrainer.com
C#.NET-Simplified
Press F5.
226
www.manzoorthetrainer.com
C#.NET-Simplified
Weve inserted 4 integers and one string but our compiler will not stop us to
doing so because we are passing objects.
We can Enqueue few strings few double and few integers.
But the problem will arise at the time of addition.
227
www.manzoorthetrainer.com
C#.NET-Simplified
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Collections;
namespace CollectionClassesEg
{
class Program
{
static void Main(string[] args)
{
Stack S = new Stack();
S.Push(12);
S.Push(14);
S.Push(56);
S.Push(34);
//Console.WriteLine("Elements are");
//foreach (int item in S)
228
www.manzoorthetrainer.com
C#.NET-Simplified
//{
//
Console.WriteLine(item);
//}
//Console.WriteLine("Pop An Element");
//Console.WriteLine(S.Pop().ToString());
Console.WriteLine(item);
//}
//Console.WriteLine("Pop An Element");
//Console.WriteLine(S.Pop().ToString());
Console.WriteLine(item);
//}
//int sum = 0;
//foreach (int item in S)
//{
//
//}
//Console.WriteLine("Sum is "+sum);
229
www.manzoorthetrainer.com
C#.NET-Simplified
Q.Enqueue(23);
Q.Enqueue(45);
Q.Enqueue(56);
Q.Enqueue(458);
Q.Enqueue("Manzoor");
//Console.WriteLine("Elements are");
//foreach (int item in Q)
//{
//
Console.WriteLine(item);
//}
//Console.WriteLine("Dequeue Element");
//Console.WriteLine(Q.Dequeue().ToString());
//Console.WriteLine("After Dequeue");
//foreach (int item in Q)
//{
//
Console.WriteLine(item);
//}
int sum = 0;
foreach (int item in Q)
{
sum = sum + item;
}
230
www.manzoorthetrainer.com
C#.NET-Simplified
Console.ReadLine();
}
}
}
231
www.manzoorthetrainer.com
C#.NET-Simplified
232
www.manzoorthetrainer.com
C#.NET-Simplified
233
www.manzoorthetrainer.com
C#.NET-Simplified
234
www.manzoorthetrainer.com
C#.NET-Simplified
235
www.manzoorthetrainer.com
C#.NET-Simplified
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace GenericCollectionClassesEg
{
236
www.manzoorthetrainer.com
C#.NET-Simplified
class Student
{
int Sid;
string Name;
class Program
{
static void Main(string[] args)
{
//S.Push(23);
//S.Push(89);
//S.Push(34);
237
www.manzoorthetrainer.com
C#.NET-Simplified
//Console.WriteLine(S.Pop());
S.Add(new Student(126,"Tom"));
S.Add(new Student(127, "Lilly"));
S.Add(new Student(128, "Bob"));
Console.ReadLine();
}
}
}
238
www.manzoorthetrainer.com
C#.NET-Simplified
239
www.manzoorthetrainer.com
C#.NET-Simplified
We are declaring delegate nothing but function prototype which does not
return anything.
And its takes single parameter.
Using this delegate we can work with all the methods whose return type is
void and the parameter is string.
Like weve show() method whose return type is void and parameter is
string.
Well write another method display() which does not return anything and
parameter is string type.
240
www.manzoorthetrainer.com
C#.NET-Simplified
And weve invoked that method using reference t and given parameter as
Manzoor.
If we see that same thing weve done in earlier is statically but now weve
done dynamically.
241
www.manzoorthetrainer.com
C#.NET-Simplified
This is runtime binding why because we are using new keyword to allocate
memory.
Press F5.
Now its going to invoke show() method.
242
www.manzoorthetrainer.com
C#.NET-Simplified
Thats fine.
Now what we can do here instead of changing the reference from show to
display we can add it.
That means it was referring to show method now weve added one more
reference of display to the same.
243
www.manzoorthetrainer.com
C#.NET-Simplified
Once we call t of message its going to execute show method first then
display method next.
244
www.manzoorthetrainer.com
C#.NET-Simplified
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace DelegatesEg
{
class Program
{
delegate void Test(string name);
245
www.manzoorthetrainer.com
C#.NET-Simplified
//Show("Manzoor");
t += new Test(Display);
t += new Test(Display);
t -= new Test(Display);
t("Manzoor");
Console.ReadLine();
}
246
www.manzoorthetrainer.com
C#.NET-Simplified
247
www.manzoorthetrainer.com
C#.NET-Simplified
Now button has got an event say we click the button, so click is an event.
That means onClick is a method weve defined inside the class button.
Whenever we click the button that method gets invoke and that method
intern will be calling a method which we havent defined yet.
So whenever weve this kind of scenario where we need to invoke a method
first later on in future anybody can define that method then we can go for
delegates.
This concept would be more clear when we go for events.
248
www.manzoorthetrainer.com
C#.NET-Simplified
This is simple class in which weve one method CallBack which takes
parameter as type delegate Test.
And it is going to invoke the method.
But we dont know which method its going to invoke.
Now let us create the object of class sample in main method.
And with that object well call the method CallBack with parameter as
object of delegate test i.e. t.
249
www.manzoorthetrainer.com
C#.NET-Simplified
Why we named as callBack, because from this class of main method we are
calling callBack method of sample class.
But this CallBack method intern calls the method of same class.
Execute it.
250
www.manzoorthetrainer.com
C#.NET-Simplified
T1 is a method and its calling the method whose return type is void and
parameter is string.
If we observe were calling the method whose return type is void and
parameter is string, its definition it has not defined yet.
That means were invoking the method prior to its definition this is the
reverse way.
After that were defining that method and then calling it back.
Weve one more way of calling back.
The way is instead of creating the reference to display method and then
passing that in CallBack.
We can directly pass the method name which intern will pass its reference.
251
www.manzoorthetrainer.com
C#.NET-Simplified
In this concept we are invoking a method whose definition is not defined yet.
So single line of code is invoking n number of methods whether it is show
method or display method of same signature.
Complete code is given below.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
252
www.manzoorthetrainer.com
C#.NET-Simplified
namespace DelegateEg2
{
delegate void Test(string name);
class Sample
{
Test t1;
class Program
{
Console.ReadLine();
}
static void Show(string name)
253
www.manzoorthetrainer.com
C#.NET-Simplified
{
Console.WriteLine("Show Hello " + name);
}
254
www.manzoorthetrainer.com
C#.NET-Simplified
255
www.manzoorthetrainer.com
C#.NET-Simplified
Name it as Calculations.
Press ok.
In calculation class weve some code for arithmetic operations.
256
www.manzoorthetrainer.com
C#.NET-Simplified
257
www.manzoorthetrainer.com
C#.NET-Simplified
258
www.manzoorthetrainer.com
C#.NET-Simplified
Press ok.
259
www.manzoorthetrainer.com
C#.NET-Simplified
260
www.manzoorthetrainer.com
C#.NET-Simplified
Press F5.
261
www.manzoorthetrainer.com
C#.NET-Simplified
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace DelegateEg3
{
delegate void Test (int a,int b);
class Program
{
static void Main(string[] args)
{
Calculations c = new Calculations();
Test t = new Test(c.Add);
t = GetChoice(c, t);
t(230, 56);
Console.ReadLine();
}
262
www.manzoorthetrainer.com
C#.NET-Simplified
t = new Test(c.Add);
break;
case 2:
t = new Test(c.Sub);
break;
case 3:
t = new Test(c.Product);
break;
case 4:
t = new Test(c.Division);
break;
default:
break;
}
return t;
}
}
}
Thank you..!
263
www.manzoorthetrainer.com