AWP Unit 1
AWP Unit 1
com
Acuity Educare
ADVANCED WEB
PROGRAMMING
SEM : V
SEM V: UNIT 1
Page 1 of 38
YouTube - Abhay More | Telegram - abhay_more
607A, 6th floor, Ecstasy business park, city of joy, JSD road, mulund (W) | 8591065589/022-25600622
TRAINING -> CERTIFICATION -> PLACEMENT BSC IT : SEM – V AWP:UNIT1
MSIL
It is language independent code. When you compile code that uses the .NET
Framework library, we don't immediately create operating system - specific native
code.
Instead, we compile our code into Microsoft Intermediate Language (MSIL) code.
The MSIL code is not specific to any operating system or to any language.
Advantages -
MSIL provide language interoperability as code in any .net language is compiled on
MSIL
Same performance in all .net languages
support for different runtime environments
JIT compiler in CLR converts MSIL code into native machine code which is executed
by OS
Functions of the CLR
o Garbage Collector
o Exception handling
o Type safety
o Memory management (using the Garbage Collector)
o Security
o Improved performance
A namespace is designed for providing a way to keep one set of names separate from
another. The class names declared in one namespace does not conflict with the same
class names declared in another.
Defining a Namespace
A namespace definition begins with the keyword namespace followed by the namespace
name as follows:
namespace namespace_name
{
// code declarations
}
Page 3 of 38
YouTube - Abhay More | Telegram - abhay_more
607A, 6th floor, Ecstasy business park, city of joy, JSD road, mulund (W) | 8591065589/022-25600622
TRAINING -> CERTIFICATION -> PLACEMENT BSC IT : SEM – V AWP:UNIT1
Example:
using System;
namespace first_space{
class namespace_cl{
public void func(){
Console.WriteLine("Inside first_space");
}
}
}
namespace second_space{
class namespace_cl{
public void func(){
Console.WriteLine("Inside second_space");
}
}
}
class TestClass{
static void Main(string[] args){
first_space.namespace_cl fc = new first_space.namespace_cl();
second_space.namespace_cl sc = new second_space.namespace_cl();
fc.func();
sc.func();
Console.ReadKey();
}
}
The using keyword states that the program is using the names in the given namespace.
For example, we are using the System namespace in our programs. The class Console is
defined there. We just write:
Console.WriteLine ("Hello there");
We could have written the fully qualified name as: System.Console.WriteLine("Hello
there");
Alias of Namespace:
using A=System.Console;
class Test{
static void Main(){
A.Write("Craetion of Alias");
A.ReadKey();
}
}
Page 4 of 38
YouTube - Abhay More | Telegram - abhay_more
607A, 6th floor, Ecstasy business park, city of joy, JSD road, mulund (W) | 8591065589/022-25600622
TRAINING -> CERTIFICATION -> PLACEMENT BSC IT : SEM – V AWP:UNIT1
ValueType.
The value types directly contain data. Some examples are int, char, and float, which
stores numbers, alphabets, and floating-point numbers, respectively. When you declare
an int type, the system allocates memory to store the value.
Reference Type
The reference types do not contain the actual data stored in a variable, but they
contain a reference to the variables.
In other words, they refer to a memory location. Using multiple variables, the
reference types can refer to a memory location. If the data in the memory location is
changed by one of the variables, the other variable automatically reflects this change in
value. Example of built-in reference types are: object, dynamic, and string.
Page 5 of 38
YouTube - Abhay More | Telegram - abhay_more
607A, 6th floor, Ecstasy business park, city of joy, JSD road, mulund (W) | 8591065589/022-25600622
TRAINING -> CERTIFICATION -> PLACEMENT BSC IT : SEM – V AWP:UNIT1
Q. Explain the implicit and explicit conversion of data types with examples.
The process of converting from one data type to another data type is called conversion.
When one type of data is assigned to another type of variable, an implicit type
conversion will take place automatically if
o The destination type has a range that is greater than the source type.
When these two conditions are met, a widening conversion takes place. For
example, the int type is always large enough to hold all valid byte values, and both
int and byte are compatible integer types, so an implicit conversion can be applied.
For widening conversions, the numeric types, including integer and floating-point
types, are compatible with each other.
For example, the following program is perfectly valid since long to double is a
widening conversion that is automatically performed.
using System;
class LtoD {
static void Main(){
long L; double D;
L = 100123285L; D = L;
Console.WriteLine("L and D: " + L + " " + D);
}
}
Although the implicit type conversions are helpful, they will not fulfil all programming
needs because they apply only to widening conversions between compatible types. For
all other cases we must employ a cast. A cast is an instruction to the compiler to
convert the outcome of an expression into a specified type. Thus, it requests an explicit
type conversion.
Page 6 of 38
YouTube - Abhay More | Telegram - abhay_more
607A, 6th floor, Ecstasy business park, city of joy, JSD road, mulund (W) | 8591065589/022-25600622
TRAINING -> CERTIFICATION -> PLACEMENT BSC IT : SEM – V AWP:UNIT1
(target-type) expression
Here, target-type specifies the desired type to convert the specified expression to. For
Example
double x, y;
if you want the type of the expression x/y to be int, you can write
(int) (x / y)
Here, even though x and y are of type double, the cast converts the outcome of the
expression to int. The parentheses surrounding x / y are necessary. Otherwise, the
cast to Int would apply only to the x and not to the outcome of the division.
The cast is necessary here because there is no implicit conversion from double to int.
When a cast involves a narrowing conversion, information might be lost.
Q. Explain Boxing and Unboxing with reference to value type and reference
type.
Boxing:
Any type, value or reference can be assigned to an object without explicit conversion.
When a compiler fined a value where it needs a reference type, it creates an object
‘box’ into which it places the value of the value type.
Example: -
When executed, this code creates a temporary reference _type ‘box’ for the object on
heap. We can also use a C-style cast for boxing.
int m = 10;
object om = (object) m;
Note that the boxing operation creates a copy of the value of the m integer to the
object om. Both the variables exist but the value of om resides on the heap. This means
that the values are independent of each other.
Example
int m =10;
object om = m;
m = 20;
Console.WriteLine(m); // m= 20 Console .WriteLine(om); //om=10
Unboxing:
Unboxing is the process of converting the object type back to the value type.
Page 7 of 38
YouTube - Abhay More | Telegram - abhay_more
607A, 6th floor, Ecstasy business park, city of joy, JSD road, mulund (W) | 8591065589/022-25600622
TRAINING -> CERTIFICATION -> PLACEMENT BSC IT : SEM – V AWP:UNIT1
Remember that a variable can be unboxed only if it has been previously boxed. In
contrast to boxing, unboxing is an explicit operation.
Example:-
int m = 100;
object om = m; //boxing int n = (int) om; //unboxing
When performing unboxing, C# checks the value type we request is actually stored in
the object under conversion. Only if it is, the value is unboxed.
Comparison operators:
It is also called as relational operator. They are used to compare at least two operands.
Below is the list of relational operators as follows:
10
Logical operators
Logical operators are used to combine two or more condition. Below is list of logical
operators used in C#.
The statements inside your source files are generally executed from top to bottom, in
the order that they appear. Control flow statements, however, break up the flow of
execution by employing decision making, looping, and branching, enabling your
program to conditionally execute particular blocks of code.
Break Statement
The break statement is used to exit from loops and switch statements. The break
statement terminates the execution control from a loop or a switch statement to the
next statement after the loop or switch statement.
The general format is: Example:-Write a program to generate the following triangle
Page 8 of 38
YouTube - Abhay More | Telegram - abhay_more
607A, 6th floor, Ecstasy business park, city of joy, JSD road, mulund (W) | 8591065589/022-25600622
TRAINING -> CERTIFICATION -> PLACEMENT BSC IT : SEM – V AWP:UNIT1
1
12
123
using System;
class demo{
public static void Main(){
for(int i=1;i<=5;i++){
for(int j=1;j<=5;j++){
Console.Write(" "+j);
if(j==i){
break;
}
}
Console.WriteLine(" ");
}
}
}
Continue Statement
The continue statement causes early iteration of a loop. It skips the remaining
statements after the continue statement and moves the execution control to the next
iteration of the loop.
The general format is:
using System;
class demo{
public static void Main(){
for(int i=1;i<=25;i++){
if(i%2!=0){
continue;
}
else{
Console.Write(" "+i);
}
}
}
}
Use the break keyword to stop the loop and the continue keyword to skip one loop cycle
and continue to the next.
using System;
public class Program{
public static void Main(){
Console.WriteLine("Demonstrating the use of break.\n");
for (int x = 1; x < 10; x++){
if (x == 5) break;
Console.WriteLine("Number " + x);
}
Console.WriteLine("\nDemonstrating the use of continue.\n");
for (int x = 1; x < 7; x++){
if (x == 5) continue;
Console.WriteLine("Number " + x);
}
}
}
Page 9 of 38
YouTube - Abhay More | Telegram - abhay_more
607A, 6th floor, Ecstasy business park, city of joy, JSD road, mulund (W) | 8591065589/022-25600622
TRAINING -> CERTIFICATION -> PLACEMENT BSC IT : SEM – V AWP:UNIT1
case<comparisonValN>:
<code to execute if <testVar> == <comparisonValN>> break;
default:
<code to execute if <testVar> != comparisonVals> break;
}
Example:
class SwitchEg{
static void Main(string[] args){
const string myName = "karli";
const string brotherName = "john";
const string sisterName = "ploppy";
string name;
Console.WriteLine("What is your name?");
name = Console.ReadLine();
switch (name.ToLower()){
case myName:
Console.WriteLine("You have the same name as me!"); break;
case brotherName:
Console.WriteLine("My, what a sexy name you have!"); break;
case sisterName:
Console.WriteLine("That’s a very silly name."); break;
}
Console.WriteLine("Hello {0}!", name);
Console.ReadKey();
}
}
Page 10 of 38
YouTube - Abhay More | Telegram - abhay_more
607A, 6th floor, Ecstasy business park, city of joy, JSD road, mulund (W) | 8591065589/022-25600622
TRAINING -> CERTIFICATION -> PLACEMENT BSC IT : SEM – V AWP:UNIT1
A foreach loop mostly used with array and collection such ArrayList. A foreach loop
enables us to address each element in an array using this simple syntax:
This loop will cycle through each element, placing it in the variable <name> in turn,
without danger of accessing illegal elements. We don’t have to worry about how many
elements are in the array, and we can be sure that we get to use each one in the loop.
The main difference between using this method and a standard for loop is that foreach
gives us read- only access to the array contents, so we can’t change the values of any
of the elements.
Example
using System;
class Abc{
static void Main(string[] args){
string[] friendNames = {"Robert Barwell", "Mike Parry", "Jeremy Beacock"};
Console.WriteLine("Here are {0} of my friends:", friendNames.Length);
foreach (string friendName in friendNames){
Console.WriteLine(friendName);
}
Console.ReadKey();
}
j= j + i ;
The foreach statement repeats a group of embedded statements for each element in an
array or an object collection. You do not need to specify the loop bounds minimum or
maximum.
Page 11 of 38
YouTube - Abhay More | Telegram - abhay_more
607A, 6th floor, Ecstasy business park, city of joy, JSD road, mulund (W) | 8591065589/022-25600622
TRAINING -> CERTIFICATION -> PLACEMENT BSC IT : SEM – V AWP:UNIT1
Example:
int j = 0;
int[] tempArr = new int[] { 0, 1, 2, 3, 5, 8, 13 };
foreach (int i in tempArr ){
j=j+i;
}
Jagged array is also called Array of Array or variable sized array. In a jagged array,
each row may have different number of columns.
It is equivalent to an array of variable sized one-dimensional arrays. The Length
property is used to get the length of each one-dimensional array.
class numadd{
public static void Main(){
int[][] x=new int[4][];
x[0]=new int[2]{5,13};
x[1]=new int[3]{7,8,11};
x[2]=new int[4]{2,3,4,5};
x[3]=new int[1]{9};
for(int i=0;i<4;i++){
for(int j=0;j<x[i].Length;j++){
Console.Write(x[i][j]+"\t");
}
Console.Write("\n");
}
}
}
Page 12 of 38
YouTube - Abhay More | Telegram - abhay_more
607A, 6th floor, Ecstasy business park, city of joy, JSD road, mulund (W) | 8591065589/022-25600622
TRAINING -> CERTIFICATION -> PLACEMENT BSC IT : SEM – V AWP:UNIT1
Reference Parameter:
It is used as a call by reference in C#. It is used in both places; when we declare a
method and when we call the method. In this example we create a function (cube) and
pass a ref parameter in it, now we look at the value before we call the function and
after we call the function.
Class TryRef{
public void cube(ref int x){
x= x * x * x;
}
}
class Program{
static void Main(string[] args){
TryRef tr = new TryRef();
int y = 5;
Console.WriteLine("The value of y before the function call: " + y);
tr.cube(ref y);
Console.WriteLine("The value of y after the function call: " + y);
Console.ReadLine();
}
}
Output Parameter:
Sometimes we do not want to give an initial value to the parameter; in this case we
use the out parameter. The declaration of the out parameter is the same as the ref
parameter. But it is used to
pass the value out of a method. Now we look at the
class TryOut{
public int mul(int a, out int b){
b = a * a;
return b;
}
}
class Program{
static void Main(string[] args){
TryOut to = new TryOut();
int x,y;
x = to.mul(10, out y);
Console.WriteLine("The output is: "+x);
Console.ReadLine();
}
}
Page 13 of 38
YouTube - Abhay More | Telegram - abhay_more
607A, 6th floor, Ecstasy business park, city of joy, JSD road, mulund (W) | 8591065589/022-25600622
TRAINING -> CERTIFICATION -> PLACEMENT BSC IT : SEM – V AWP:UNIT1
We can use more than on Main Method in C# program, But there will only one Main
Method which will act as entry point for the program.
using System;
class A{
static void Main(string[] args){
Console.WriteLine("I am from Class A");
Console.ReadLine();
}
}
class B{
static void Main(string[] args){
Console.WriteLine("I am from Class B");
Console.ReadLine();
}
}
Try to execute Program as given below:
As in given program there are two classes A and B in which each containing A Main
Method. We may write as.
csc filename.cs /main:A [ for Class A Main Execution ] or, csc filename.cs /main:B [ for
Class B Main Execution ]
Structure Class
It is value type It is reference type.
Structure stored in stack memory. Class stored in heap memory.
Does not consist of parametrized Consist of parametrized constructor.
constructor.
Does not allow inheritance. Allow inheritance.
Does not consist of destructor. Consist of destructor.
Structure created with help of struct Classes created with the help of class
keyword. keyword.
Example:- struct Book Example:- class Abc
{ {
} }
Page 14 of 38
YouTube - Abhay More | Telegram - abhay_more
607A, 6th floor, Ecstasy business park, city of joy, JSD road, mulund (W) | 8591065589/022-25600622
TRAINING -> CERTIFICATION -> PLACEMENT BSC IT : SEM – V AWP:UNIT1
An enum is a value type with a set of related named constants often referred to
as an enumerator list.
The enum keyword is used to declare an enumeration. It is a primitive data
type, which is user defined.
Enums type can be integer (float, int, byte, double etc.). But if you used beside
int it has to be cast.
The keyword enum is used to create numeric constants in .NET framework.
There must be a numeric value for each enum type.
The default underlying type of the enumeration elements is int. By default, the
first enumerator has the value 0, and the value of each successive enumerator is
increased by 1.
Example:-
using System;
enumDays {Sat, Sun, Mon, Tue, Wed, Thu, Fri};
class Abc{
public static void main(){
Console.WriteLine((int)Days.Wed); Console.WriteLine((Days)4);
}
}
Inheritance
Inheritance is the concept we use to build new classes using the existing class
definitions. Through inheritance we can modify a class the way we want to create new
objects. The original class is known as base or parent class & the modified class is
known as derived or subclass or child class. The
concept of inheritance facilitates the reusability of existing code & thus improves the
integrity of programs & productivity of programmers.
Polymorphism
Polymorphism is the ability to take more than one form. For example, an operation may
exhibit different behavior in different situations. The behavior depends upon the types
of data used on the operation. Polymorphism is extensively used while implementing
inheritance.
Two types
Compile time polymorphism Runtime time polymorphism
Page 15 of 38
YouTube - Abhay More | Telegram - abhay_more
607A, 6th floor, Ecstasy business park, city of joy, JSD road, mulund (W) | 8591065589/022-25600622
TRAINING -> CERTIFICATION -> PLACEMENT BSC IT : SEM – V AWP:UNIT1
Q. What are sealed classes and sealed methods? Why are they used?
The methods declared with a sealed keyword in its header are known as sealed
methods. Such method provides a complete implementation to its base class virtual
method using the override keyword.
Characteristics
A method cannot be defined as sealed unless that method is an override of a
method in its base class.
A sealed method cannot be overridden further.
Sealed methods are useful to avoid problems caused by overriding the existing
functionality.
It prevents the user from changing the internal functionality of a class.
Example:
using System;
class A{
public virtual void F(){
Console.WriteLine("A.F");
}
public virtual void G(){
Console.WriteLine("A.G");
}
}
class B: A{
sealed override public void F(){
Console.WriteLine("B.F");
}
override public void G(){
Console.WriteLine("B.G");
}
}
class C: B{
override public void G(){
Console.WriteLine("C.G");
}
}
The class B provides two override methods: an F method that has the sealed modifier
and a G method that does not. B's use of the sealed modifier prevents C from further
overriding F.
Sealed Classes:
Generally if we create classes we can inherit the properties of that created class
in any class without having any restrictions.
In some situation we will get requirement like we don’t want to give permission
for the users to derive the classes from it or don’t allow users to inherit the
properties from particular class.
For that purpose we have keyword called “sealed” in OOPS.
When we defined class with keyword “Sealed” then we don’t have a chance to
derive that particular class and we don’t have permission to inherit the
properties from that particular class.
A sealed class cannot be inherited.
It is an error to use a sealed class as a base class.
Use the sealed modifier in a class declaration to prevent inheritance of the class.
It is not permitted to use the abstract modifier with a sealed class.
Page 16 of 38
YouTube - Abhay More | Telegram - abhay_more
607A, 6th floor, Ecstasy business park, city of joy, JSD road, mulund (W) | 8591065589/022-25600622
TRAINING -> CERTIFICATION -> PLACEMENT BSC IT : SEM – V AWP:UNIT1
Example:
using System;
sealed class MyClass{
public int x; public int y;
}
class MainClass{
public static void Main(){
MyClass mC = new MyClass();
mC.x = 110;
mC.y = 150;
Console.WriteLine("x = {0}, y = {1}", mC.x, mC.y);
}
}
Q. What are the rules in defining a constructor? Explain static constructor with
example.
Also note that a static constructor is parameter less and no access modifiers
are allowed with its declaration. The exact timing of static constructor
execution is implementation-dependent, but is subject to the following rules:
The static constructor for a class executes before any instance of the class is
created.
The static constructor for a class executes before any of the static members for
the class are referenced.
The static constructor for a class executes after the static fields initializes for
the class.
The static constructor for a class executes at most one time during a single
program execution.
Example:
class A{
static A(){
Console.WriteLine("static constructor A is invoked");
}
public static void Main(){
}
}
Page 17 of 38
YouTube - Abhay More | Telegram - abhay_more
607A, 6th floor, Ecstasy business park, city of joy, JSD road, mulund (W) | 8591065589/022-25600622
TRAINING -> CERTIFICATION -> PLACEMENT BSC IT : SEM – V AWP:UNIT1
When constructor does not accept any parameter then it is referred as default
constructor or zero parametrized.
When constructor accepts the parameter then it is referred as parametrized
constructor.
Example:
using System;
class test{
double length, breadth;
public test(){
Console.WriteLine("default cons");
}
public test(double l, double b){
//Console.WriteLine("paramaterized cons");
length = l;
breadth = b;
}
public double area(){
return (length * breadth);
}
}
class testmain{
public static void Main(){
test defaultcon =new test();//calls default cons
test t1 = new test(10.05, 100.45);
double result = t1.area();
Console.WriteLine("area of rectangle-->" + result);
Console.ReadLine();
}
}
Page 18 of 38
YouTube - Abhay More | Telegram - abhay_more
607A, 6th floor, Ecstasy business park, city of joy, JSD road, mulund (W) | 8591065589/022-25600622
TRAINING -> CERTIFICATION -> PLACEMENT BSC IT : SEM – V AWP:UNIT1
Private constructor
Private constructors are used to restrict the instantiation of object using ‘new’ operator.
A private constructor is a special instance constructor. It is commonly used in classes
that contain static members only. This type of constructors is mainly used for creating
singleton object. If you don't want the class to be inherited we declare its constructor
private.
Example:
using System;
class test{
private test(){
Console.WriteLine("private cons ");
}
public test(int x){
Console.WriteLine("non private cons--->"+ x);
}
}
class testmain{
public static void Main(){
test t1 = new test();//Error'test.test()' is inaccessible due to its protection level
test t2 = new test(10);
Console.ReadLine();
}
}
Static Constructor
There can be only one static constructor in the class. The static constructor should be
without parameters. It can only access the static members of the class. There should be
no access modifier in static constructor definition.
Example:
using System;
class test{
static int x;
static test(){
Console.WriteLine("static cons ");
x = 10;//set the values for static member here
}
public static void show(){
Console.WriteLine("show of x--->" + x);
}
}
class testmain{
public static void Main(){
test.show();
Console.ReadLine();
}
}
Page 19 of 38
YouTube - Abhay More | Telegram - abhay_more
607A, 6th floor, Ecstasy business park, city of joy, JSD road, mulund (W) | 8591065589/022-25600622
TRAINING -> CERTIFICATION -> PLACEMENT BSC IT : SEM – V AWP:UNIT1
Copy constructor
When constructor accepts the object as parameter then it is referred as copy
constructor.
Example:
using System;
class test{
double length, breadth;
public test(){
Console.WriteLine("default cons");
}
public test(double l, double b){
//Console.WriteLine("paramaterized cons");
length = l;
breadth = b;
}
public test(test t1){
length = t1.length;
breadth = t1.breadth;
}
public double area() {
return (length * breadth);
}
}
class testmain
{
public static void Main(){
test defaultcon = new test();//calls default cons
test t1 = new test(10,10);
double result = t1.area();
Console.WriteLine("area of rectangle t1-->" + result);
test t2 = new test(20, 20);
double result1 = t2.area();
Console.WriteLine("area of rectangle t2-->" + result1);
test t3 = new test(t2);
double result_copy = t3.area();
Console.WriteLine("area of rectangle t3 is copy_con of t2-->" +
result_copy);
Console.ReadLine();
}
}
Page 20 of 38
YouTube - Abhay More | Telegram - abhay_more
607A, 6th floor, Ecstasy business park, city of joy, JSD road, mulund (W) | 8591065589/022-25600622
TRAINING -> CERTIFICATION -> PLACEMENT BSC IT : SEM – V AWP:UNIT1
C# does not support multiple inheritance among classes. However, you can use
interfaces to implement multiple inheritance. The following program demonstrates this:
Example:
using System;
class Shape{
protected int width; protected int height;
public void setWidth(int w){
width = w;
}
public void setHeight(int h){
height = h;
}
}
public interface PaintCost{
int getCost(int area);
}
class Rectangle : Shape, PaintCost{
public int getArea(){
return (width * height);
}
public int getCost(int area){
return area * 70;
}
}
class RectangleTester{
static void Main(string[] args){
Rectangle Rect = new Rectangle();
int area;
Rect.setWidth(5);
Rect.setHeight(7);
area = Rect.getArea();
Console.WriteLine("Total area: {0}", Rect.getArea());
Console.WriteLine("Total paint cost: ${0}" , Rect.getCost(area));
Console.ReadKey();
}
}
Page 21 of 38
YouTube - Abhay More | Telegram - abhay_more
607A, 6th floor, Ecstasy business park, city of joy, JSD road, mulund (W) | 8591065589/022-25600622
TRAINING -> CERTIFICATION -> PLACEMENT BSC IT : SEM – V AWP:UNIT1
C# does not support multiple inheritances among classes, but a class has the option of
implementing one or more interfaces.
One challenge with interfaces is that they may include methods that have the
same signatures existing class members or members of other interfaces.
Explicit interface implementations can be used to disambiguate class and interface
methods that would otherwise conflict.
Explicit interfaces can also be used to hide the details of an interface that the
class developer considers private.
To explicitly implement an interface member, just use its fully qualified name in
the declaration.
A fully qualified interface name takes the form InterfaceName.MemberName
Example:
interface I1{
void A();
}
interface I2{
void A();
}
class C : I1, I2{
public void I1.A(){
Console.WriteLine("A() from I");
}
void I2.A(){
Console.WriteLine("A() from I2");
}
}
Similarities:
Both abstract classes and interfaces may contain members that can be inherited
by a derived class.
Neither interfaces nor abstract classes may be directly instantiated, but we can
declare variables of these types. If we do, we can use polymorphism to assign
objects that inherit from these types to variables of these types.
In both cases, we can then use the members of these types through these
variables, although we don’t have direct access to the other members of the
derived object.
Page 22 of 38
YouTube - Abhay More | Telegram - abhay_more
607A, 6th floor, Ecstasy business park, city of joy, JSD road, mulund (W) | 8591065589/022-25600622
TRAINING -> CERTIFICATION -> PLACEMENT BSC IT : SEM – V AWP:UNIT1
Differences:
Derived classes may only inherit from a single base class, which means that only
a single abstract class can be inherited directly. Conversely, classes can use as
many interfaces as they want
Abstract classes may possess both abstract members and non-abstract members.
Interface members conversely, must be implemented on the class that uses the
interface they do not possess code bodies.
Moreover, interface members are by definition public but members of abstract
classes may also be private (as long as they aren’t abstract), protected, internal,
or protected internal. In addition, interfaces can’t contain fields, constructors,
destructors, static members, or constants.
Overloading
Method Overloading means having two or more methods with the same name but with
different signature (different parameters list and different type of parameters) in same
class or in different classes. Method Overloading forms compile-time polymorphism.
Overriding
Method overriding means having two methods with same name and same signature,
one method in base class and other method in derived class.
A subclass inherits methods from a base class. Sometimes, it is necessary for the
subclass to modify the methods defined in the base class. This is referred to as method
overriding.
This can be achieved by using the virtual and override keywords. We have to use the
virtual keyword for the method which in base class and override keyword for the
method in subclass.
By default functions are not virtual in C# and so you need to write “virtual” explicitly.
Page 24 of 38
YouTube - Abhay More | Telegram - abhay_more
607A, 6th floor, Ecstasy business park, city of joy, JSD road, mulund (W) | 8591065589/022-25600622
TRAINING -> CERTIFICATION -> PLACEMENT BSC IT : SEM – V AWP:UNIT1
In a C# program a derived class can have methods with the same signature as that of
its base class methods. This hides the base class methods by its derived class
counterparts and gives a warning. This warning can be suppressed by using the new
keyword with the derive class methods. This means that the members of the base class
are made intentionally hidden by the members having the same signature in the
derived class. The new function will never be invoked by a base class pointer.
Example:-
class demo{
public void disp(){
System.Console.WriteLine("From disp method of base class");
}
}
class test:demo{
new public void disp(){
System.Console.WriteLine("From disp method of derived class");
}
public static void Main(){
test t=new test(); t.disp();
}
}
Page 25 of 38
YouTube - Abhay More | Telegram - abhay_more
607A, 6th floor, Ecstasy business park, city of joy, JSD road, mulund (W) | 8591065589/022-25600622
TRAINING -> CERTIFICATION -> PLACEMENT BSC IT : SEM – V AWP:UNIT1
Overriding methods:
In method overriding we can override a method in base class by creating similar
method in derived class this can be achieved by using inheritance principle and using
“virtual & override” keywords.
If we want to override base class method then we need to declare base class method
with “virtual” keyword and the method which we created in derived class to override
base class need to declare with “override” keyword like as shown below
Example:-
class SampleA{
public virtual void Show(){
Console.WriteLine("Sample A Test Method");
}
}
class SampleB:SampleA{
public override void Show(){
Console.WriteLine("Sample B Test Method");
}
}
class Program{
static void Main(string[] args){
SampleA a=new SampleA();
SampleB b=new SampleB();
a.Show();
b.Show();
a = new SampleB();
a.Show();
Console.ReadLine();
}
}
Hiding methods:
To hide base class methods in derived classes we can declare derived class methods
with new keyword. To use new keyword, we need to write the code like as shown
below
Example:-
class SampleA{
public void Show(){
Console.WriteLine("Sample A Test Method");
}
}
class SampleB:SampleA{
public new void Show(){
Console.WriteLine("Sample B Test Method");
}
}
class Program{
static void Main(string[] args){
Page 26 of 38
YouTube - Abhay More | Telegram - abhay_more
607A, 6th floor, Ecstasy business park, city of joy, JSD road, mulund (W) | 8591065589/022-25600622
TRAINING -> CERTIFICATION -> PLACEMENT BSC IT : SEM – V AWP:UNIT1
Derived class
The functionality of an existing class can be extended by creating a new class from it.
The new class is then called a child class or derived class. The derived class inherits the
properties of the base class.
Abstract class
Static class
A static class is class whose objects cannot be created and must contain only static
members.
Sealed class
A sealed class is a class which cannot be inherited. In C#, the sealed modifier is used
to define a class as sealed.
Partial class
It is possible to split the definition of a class over two or more source files. Each source
file contains a section of the type or meth
Page 27 of 38
YouTube - Abhay More | Telegram - abhay_more
607A, 6th floor, Ecstasy business park, city of joy, JSD road, mulund (W) | 8591065589/022-25600622
TRAINING -> CERTIFICATION -> PLACEMENT BSC IT : SEM – V AWP:UNIT1
This is in contrast to DLL in the past. Earlier developers used to share libraries of code
through DLL. To use the DLL that is developed by another developer for another
application, we must register that DLL in our machine. In ASP.NET, the assembly is
created by default whenever we build a DLL. We can check the details of the manifest
of the assembly by using classes located in the System.Reflection namespace.
Thus, we can create two types of ASP.NET Assemblies in ASP.NET: private ASP.NET
Assemblies and shared assemblies. Private ASP.NET Assemblies are created when you
build component files like DLLs that can be applied to one application.
Shared ASP.NET Assemblies are created when you want to share the component files
across multiple applications. Shared ASP.NET Assemblies must have a unique name
and must be placed in Global Assembly Cache (GAC). The GAC is located in the
Assembly directory in WinNT. You can view both the manifest and the IL using
ILDisassembler (ildasm.exe).
Page 28 of 38
YouTube - Abhay More | Telegram - abhay_more
607A, 6th floor, Ecstasy business park, city of joy, JSD road, mulund (W) | 8591065589/022-25600622
TRAINING -> CERTIFICATION -> PLACEMENT BSC IT : SEM – V AWP:UNIT1
The manifest file contains all the metadata needed to specify the assembly's version
requirements, security identity, and all metadata needed to define the scope of the
assembly and resolve references to resources and classes.
Components of Assembly Manifest
It describes the assembly. The manifest file contains all the metadata needed to
specify the assembly's version requirements, security identity, and all metadata
needed to define the scope of the assembly and resolve references to resources and
classes.
Type Metadata
It contains metadata, which describes each type (class, structure, enumeration, and so
forth)
MSIL
It contains Intermediate language code.
Resources
It contains bitmaps, icons, audios and other types of resources.
Page 29 of 38
YouTube - Abhay More | Telegram - abhay_more
607A, 6th floor, Ecstasy business park, city of joy, JSD road, mulund (W) | 8591065589/022-25600622
TRAINING -> CERTIFICATION -> PLACEMENT BSC IT : SEM – V AWP:UNIT1
Every time our application instantiates a reference-type object, the CLR allocates space
on the managed heap for that object. However, we never need to clear this memory
manually. As soon as our reference to an object goes out of scope (or our application
ends), the object becomes available for garbage collection. The garbage collector runs
periodically inside the CLR, automatically reclaiming unused memory for inaccessible
objects.
Page 30 of 38
YouTube - Abhay More | Telegram - abhay_more
607A, 6th floor, Ecstasy business park, city of joy, JSD road, mulund (W) | 8591065589/022-25600622
TRAINING -> CERTIFICATION -> PLACEMENT BSC IT : SEM – V AWP:UNIT1
Namespaces are C# program elements designed to help you organize our programs.
They also provide assistance in avoiding name clashes between two sets of code.
Implementing Namespaces in our own code is a good habit because it is likely to save
us from problems later when we want to reuse some of our code.
For example, if we created a class named Console, we would need to put it in our own
namespace to ensure that there wasn't any confusion about when the System.Console
class should be used or when our class should be used. Generally, it would be a bad
idea to create a class named Console, but in many cases your classes will be named
the same as classes in either the .NET Framework Class Library or namespaces help us
to avoid the problems that identical class names would cause. System is fundamental
namespace for C# application. It contains all the fundamental classes and base classes
which are required in simple C# application. These classes and sub classes defines
reference data type, method and interfaces. Some classes provide some other feature
like data type conversion, mathematical function. Some functionality provided by
System namespace
Commonly-used value
Mathematics
Remote and local program invocation
Application environment management
Reference data types
Events and event handlers
Interfaces Attributes Processing exceptions
Data type conversion
Method parameter manipulation
Alias of namespace
Below example demonstrate use of alias where A is alias for System.Console
namespaces.
using A=System.Console;
class Abc{
public static void Main(){
A.Write(“Welcome to C# !!!!”);
}
}
Page 31 of 38
YouTube - Abhay More | Telegram - abhay_more
607A, 6th floor, Ecstasy business park, city of joy, JSD road, mulund (W) | 8591065589/022-25600622
TRAINING -> CERTIFICATION -> PLACEMENT BSC IT : SEM – V AWP:UNIT1
The System namespace contains fundamental classes and base classes that define
commonly-used value and reference data types, events and event handlers, interfaces,
attributes, and processing exceptions.
The System.Collections namespace contains interfaces and classes that define
various collections of objects, such as lists, queues, bit arrays, hash tables and
dictionaries.
The System.Text.RegularExpressions namespace contains classes
like Regex, Match and MatchCollection Class that provide access to the
.NET Framework regular expression engine.
The System.Data namespace provides access to classes that represent the
ADO.NET architecture. ADO.NET lets you build components that efficiently
manage data from multiple data sources.
The System.Drawing parent namespace contains types that support basic GDI+
graphics functionality.
Collection: Collections are basically group of records which can be treated as a one
logical unit.
.NET Collections are divided in to four important categories as follows.
Indexed based.
Key Value Pair.
Prioritized Collection.
Specialized Collection.
Indexed based:
It helps us to access the value by using generated index number by the collection.
ArrayList: A simple resizable, index-based collection of objects.
Key Value Pair: It helps you to access value by the user defined key.
SortedList: A sorted collection of name/value pairs of objects.
Hashtable: A collection of name/value pairs of objects that allows retrieval by name or
index
Prioritized Collection: It helps us to get the element in a particular sequence.
Queue: A first-in, first-out collection of objects
Stack: A last-in, first-out collection of objects
Specialized Collection:
It is very specific collections which are meant for very specific purpose like hybrid
dictionary that start as list and become Hashtable.
StringCollection: A simple resizable collection of strings
StringDictionary: A collection of name/values pairs of strings that allows retrieval by
name or index
ListDictionary: An efficient collection to store small lists of objects
HybridDictionary: A collection that uses a ListDictionary for storage when the number
of items in the collection is small, and then migrate the items to a Hashtable for large
collections
Page 32 of 38
YouTube - Abhay More | Telegram - abhay_more
607A, 6th floor, Ecstasy business park, city of joy, JSD road, mulund (W) | 8591065589/022-25600622
TRAINING -> CERTIFICATION -> PLACEMENT BSC IT : SEM – V AWP:UNIT1
ArrayList is a non-generic type of collection in C#. It can contain elements of any data
types. It is similar to an array, except that it grows automatically as you add items in it.
Unlike an array, we don't need to specify the size of ArrayList.
Example: Insert()
ArrayList myArryList = new ArrayList();
myArryList.Add(1);
myArryList.Add("Two");
myArryList.Add(3);
Page 33 of 38
YouTube - Abhay More | Telegram - abhay_more
607A, 6th floor, Ecstasy business park, city of joy, JSD road, mulund (W) | 8591065589/022-25600622
TRAINING -> CERTIFICATION -> PLACEMENT BSC IT : SEM – V AWP:UNIT1
myArryList.Add(4.5);
myArryList.Insert(1, "Second Item");
myArryList.Insert(2, 100);
Example: Remove()
ArrayList arryList1 = new ArrayList();
arryList1.Add(100);
arryList1.Add(200);
arryList1.Add(300);
arryList1.Remove(100); //Removes 1 from ArrayList
Example: RemoveAt()
ArrayList arryList1 = new ArrayList();
arryList1.Add(100);
arryList1.Add(200);
arryList1.Add(300);
arryList1.RemoveAt(1); //Removes the first element from an ArrayList
Example: Contains()
ArrayList myArryList = new ArrayList();
myArryList.Add(100);
myArryList.Add("Hello World");
myArryList.Add(300);
Console.WriteLine(myArryList.Contains(100));
Page 34 of 38
YouTube - Abhay More | Telegram - abhay_more
607A, 6th floor, Ecstasy business park, city of joy, JSD road, mulund (W) | 8591065589/022-25600622
TRAINING -> CERTIFICATION -> PLACEMENT BSC IT : SEM – V AWP:UNIT1
A generic class can be defined using angle brackets <>. For example, the following is a
simple generic class with a generic member variable, generic method and property.
Example:
class MyGenericClass<T>{
private T genericMemberVariable;
public MyGenericClass(T value){
genericMemberVariable = value;
}
public T genericMethod(T genericParameter){
Console.WriteLine("Parameter type: {0}, value: {1}",typeof(T).ToString(),genericParameter);
Console.WriteLine("Return type: {0}, value: {1}", typeof(T).ToString(), genericMemberVariable);
return genericMemberVariable;
}
In C#, properties are nothing but natural extension of data fields. They are usually
known as ' smart fields' in C# community. We know that data encapsulation and hiding
are the two fundamental characteristics of any object-oriented programming language.
Page 35 of 38
YouTube - Abhay More | Telegram - abhay_more
607A, 6th floor, Ecstasy business park, city of joy, JSD road, mulund (W) | 8591065589/022-25600622
TRAINING -> CERTIFICATION -> PLACEMENT BSC IT : SEM – V AWP:UNIT1
class MyClient{
public static void Main(){
MyClass mc = new MyClass();
mc.X = 10;
int xVal = mc.X;
Console.WriteLine(xVal);//Displays 10
}
}
Delegate:
The dictionary meaning of ‘Delegates’ is “A person acting for another person”. A
delegate in C# is a class type object & is used to invoke a method that has been
encapsulated into it at the time of its creation. Following steps are used to create
delegate:
Delegate declaration
modifier delegate return_type delegate_name(parameters)
Delegate instantiation
new delegate_type(expression);
Delegate invocation
Delegate_object (parameters);
Note: signature of delegate and function must be same
Page 36 of 38
YouTube - Abhay More | Telegram - abhay_more
607A, 6th floor, Ecstasy business park, city of joy, JSD road, mulund (W) | 8591065589/022-25600622
TRAINING -> CERTIFICATION -> PLACEMENT BSC IT : SEM – V AWP:UNIT1
Example:
public delegate void MyDelegate(int , int );
class Abc{
public void Addition(int a, int b){
Console.WriteLine (“Addition :”+(a+b));
}
public void Subtraction(int a, int b) {
Console.WriteLine (“Subtraction :”+(a-b));
}
}
class Xyz{
public static void Main(){
Abc a1=new Abc();
MyDelegate m1=new MyDelegate(a1.Addition);
MyDelegate m2=new MyDelegate(a1.Subtraction);
m1(10,20);
m2(40,5);
Console.Read();
}
}
Q. What is delegate? Explain multiple delegate with example.
Delegate:
A delegate in C# is similar to a function pointer in C or C++. It is a reference type
object that allows the programmer to encapsulate a reference to a method in it. It
defines the return type and signature for a method and can refer to any methods that
are of the same format in the signature.
When same delegate is used to call method multiple time then it is referred as Multicast
delegate or Multiple delegate.
Following are condition for multicast delegate:
void is used as return type
Out parameter is not allowed as arguments
Example
public delegate void MyDelegate();
class Abc{
public void Show(){
Console.WriteLine (“New Delhi”);
}
public void Display(){
Console.WriteLine (“New York”);
}
}
class Xyz{
public static void Main(){
Abc a1=new Abc();
MyDelegate m1=new MyDelegate(a1.Show);
MyDelegate m2=new MyDelegate(a1.Display);
m1=m1+m2+m1+m2-m1;
m1();
Console.Read();
}
}
Page 37 of 38
YouTube - Abhay More | Telegram - abhay_more
607A, 6th floor, Ecstasy business park, city of joy, JSD road, mulund (W) | 8591065589/022-25600622
TRAINING -> CERTIFICATION -> PLACEMENT BSC IT : SEM – V AWP:UNIT1
Q. Delegates in C# are used for Event Handling. Justify this statement with a
relevant example program
Events:
An important C# feature is built upon the foundation of delegates: the event. An event
is, essentially, an automatic notification that some action has occurred. Events work like
this: An object that has an interest in an event registers an event handler for that
event. When the event occurs, all registered handlers are called. Event handlers are
represented by delegates. Events are members of a class and are declared using the
event keyword.
Here, event-delegate is the name of the delegate used to support the event, and event-
name is the name of the specific event object being declared.
Example:
using System;
delegate void MyEventHandler();
class MyEvent{
public event MyEventHandler SomeEvent;
public void OnSomeEvent() {
if(SomeEvent != null)
SomeEvent();
}
}
class EventDemo{
static void Handler() {
Console.WriteLine("Event occurred");
}
static void Main(){
MyEvent evt = new MyEvent();
evt.SomeEvent += Handler; evt.OnSomeEvent();
}
}
Page 38 of 38
YouTube - Abhay More | Telegram - abhay_more
607A, 6th floor, Ecstasy business park, city of joy, JSD road, mulund (W) | 8591065589/022-25600622