OOPS And Modifiers in C# .Net
OOPS in C#
Listed of Object oriented programming
system (OOPS)
It is
one of the approch that followed by the industries in 1970’s to overcome the
drawback in procedural programming languages.
A language is called object oriented need to satisfied a
set of principal those are—
·
Encapsulation (Ex1:A Class rapping(Covering) the data
under it. Ex2: The real world example-
the car is rapped by the cover)
·
Abstraction (Ex1:Methods— This is all about hiding the
data --Here we know how to call the method but we don’t know the
functionality(or logic behind the method. Ex2: The car driver know that how to
drive the car but driver don’t know, how is working the engine)
·
Inheritance (Ex1: Re-usability of the code. It means-The member
that is defining in one class can be consume from other classes by followed
parent/child relationship.)
One type of inheritance-
*Single inheritance.
·
Polymorphism (Ex1: It means: One name but many forms.. The
polymorphism is the capability of one object can behave in multiple ways.) Ex2:
Behaving in different ways depending upon input receive is called polymorphism.
Three approaches
of polymorphism—
*Method overloading
*Method overriding
*Hiding or Shadowing.
1) Method OverLoading :
Ans: It is an approach to defining
multiple methods under a class with the
same name but different signatures(or arguments type or parameters type).
Ex1:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace M_OverLoading
{
class MLoad
{
public void show()
{
Console.WriteLine("No
parameter");
}
public void show(int i)
{
Console.WriteLine("one
parameter of integer type");
}
public void show(string s)
{
Console.WriteLine("one
parameter with string type");
}
public void show(int i,string s)
{
Console.WriteLine("Two
parameters with integer and string type");
}
public void show(string s,int i)
{
Console.WriteLine("Two
parameters with string and integer type");
}
static void Main(string[] args)
{
MLoad ob = new MLoad();
ob.show();
ob.show(10);
ob.show("Paul");
ob.show(3,"ujjwal");
ob.show("Indian",9);
Console.ReadLine();
}
}
}
Output:
//No
parameter
//one
parameter of integer type
//one
parameter with string type
//Two
parameters with integer and string type
//Two
parameters with string and integer type
Ex2:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace M_OverLoading
{
class MLoad
{
public void show()
{
Console.WriteLine("No
parameter");
}
public void show(int i)
{
Console.WriteLine("one parameter
of integer type");
}
public void show(string s)
{
Console.WriteLine("one
parameter with string type");
}
public void show(int i,string s)
{
Console.WriteLine("Two
parameters with integer and string type");
}
public void show(string s,int i)
{
Console.WriteLine("Two
parameters with string and integer type");
}
static void Main(string[] args)
{
MLoad ob = new MLoad();
ob.show();
ob.show(10);
ob.show("Paul");
ob.show(3,"ujjwal");
ob.show("Indian",9);
Console.ReadLine();
}
}
}
Output:
//No
parameter
//one
parameter of integer type
//one
parameter with string type
//Two
parameters with integer and string type
//Two
parameters with string and integer type
Ex2:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace test2
{
class MLoad2
{
public int sum(int x)
{
return(x);
}
public int sum(int x,int y)
{
return(x+y);
}
public int sum(int x, int y, int z)
{
return(x+y+z);
}
public int sum(int x, int y, int z , int k)
{
return(x+y+z+k);
}
static void Main(string[] args)
{
MLoad2 ob = new MLoad2();
int s1= ob.sum(10);
int s2= ob.sum(20,21);
int s3= ob.sum(30,31,32);
int s4= ob.sum(40,41,42,43);
Console.WriteLine("Parameter 1
method Sum1={0}",s1);
Console.WriteLine("Parameter 2
methos Sum2={0}", s2);
Console.WriteLine("Parameter 3
method Sum3={0}", s3);
Console.WriteLine("Parameter
method 4 Sum4={0}", s4);
Console.ReadLine();
}
}
}
Output:
Parameter
1 method Sum1=10
Parameter
1 method Sum1=41
Parameter
1 method Sum1=93
Parameter
1 method Sum1=166
Constructor OverLoading :
As we can overload methods of a class, we
can also overload the constructor of a class also by defining more than one
constructors with the different parameters types.
Ex:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ConsOverLoad
{
class CLoad
{
int x; bool b;
public CLoad()
{
// default Constructor.
}
public CLoad(int x)
{
this.x = x;
}
public CLoad(bool b)
{
this.b = b;
}
public CLoad(int x, bool b)
{
this.x = x; this.b = b;
}
public void Display()
{
Console.WriteLine("The value of
x={0} and b={1}" , x,b);
}
static void Main(string[] args)
{
CLoad ob1 = new CLoad();
CLoad ob2 = new CLoad(10);
CLoad ob3 = new CLoad(true);
CLoad ob4 = new CLoad(20,false);
ob1.Display();
ob2.Display();
ob3.Display();
ob4.Display();
Console.ReadLine();
}
}
}
//output:
//The
value of x=10 and b=false
//The
value of x=0 and b=false
//The
value of x=0 and b=true
//The
value of x=20 and b=false
1) Method OverRiding :
Ans: It is an approach to defining
multiple methods under the different classes (Parent /child classes) with the
same name and same signatures(signatute= arguments type or parameters type).
Or,,
If a parent classes method is
re-implemented under a child class exactly with the same name and same
signature then we called as method overriding (This can never be perform within
a class, it can perform only between parent / child classes)
Ex1:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace OverRideEX1
{
class LoadParent
{
public void show()
{
Console.WriteLine("parent
method called");
}
}
class LoadChild:LoadParent
{
public void show()
{
Console.WriteLine("child method
called");
}
}
class main
{
static void Main(string[] args)
{
LoadChild ob1 = new LoadChild();
ob1.show();
Console.ReadLine();
}
}
}
//Output--Child
method called.
Ex2:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace OverRideEX1
{
class LoadParent
{
public virtual void show()
{
Console.WriteLine("parent
method called");
}
}
class LoadChild:LoadParent
{
public void test()
{
Console.WriteLine("child method
called");
}
}
class main
{
static void Main(string[] args)
{
LoadChild ob1 = new LoadChild();
ob1.show();
Console.ReadLine();
}
}
}
//Output--Parent
method called method called.
Ex
3:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace OverRideEX1
{
class LoadParent
{
public virtual void show()
{
Console.WriteLine("parent method
called");
}
}
class LoadChild:LoadParent
{
public override void show()
{
Console.WriteLine("child method
called");
}
}
class main
{
static void Main(string[] args)
{
LoadChild ob1 = new LoadChild();
ob1.show();
Console.ReadLine();
}
}
}
//Output--Child
method called.
Method Hiding or shadowing:
This is also re-implementing a parent
class method under the child classes exectly with the same signature like
overriding but different between override and hiding is—
In overriding child class re-implement,
parent classes methods with the permission of parent class where as in hiding
child class re-implements its parent classes method without permission of
parent.
__ we can re-implement
the parent classes method under child class by using two diffrents methods—
Case 1: overriding case 2: hiding or shadowing
In the first case virtual method are parent class or re-implemented
under child class by using the ‘override’ modifier and in the 2 nd case any
method of the parent class can be re-implemented under child.
EX-1:
cclass1
Publid void display()
class2:class1
public new void display()
// hiding .
è Using ‘new’ keyword while
re-implementing the method is only optional to the child class , which is to
inform the compiler that we are intentionally re-implementing the method.
EX-2 :
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace OverRideEX1
{
class LoadParent
{
public void display()
{
Console.WriteLine("parent method
called");
}
}
class LoadChild:LoadParent
{
public new void display()
{
Console.WriteLine("child method
called");
}
}
class main
{
static void Main(string[] args)
{
LoadChild ob1 = new LoadChild();
ob1.display();
Console.ReadLine();
}
}
}
//Output--child method called.
EX-3 : base keyword use
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace OverRideEX1
{
class LoadParent
{
public void display()
{
Console.WriteLine("parent
method called");
}
}
class LoadChild:LoadParent
{
public void test()
{
base.display();
Console.WriteLine("child method
called");
}
}
class main
{
static void Main(string[] args)
{
LoadChild ob1 = new LoadChild();
ob1.test();
Console.ReadLine();
}
}
}
//Output--parent method called.
// child
method called.
--------------------------------------------------------------------------------------------------------------------------------------------
INHERITANCE
When one class consuming the members of
another class by stablishing parent child relationship between the classes is
called inheritance. This provide reusability of the code
Inheriting syntax:
[modifiers] class [child class
name]:[parent class name].
Here: colon for inheriting symbol to
inherit parent class.
Inheritance follows 4 rules—
Rule-1)In inheritance if parent classes
constructor is accessible to the child class ,then only inheritance possible
otherwise no question of inheritance.
EX:
class class1 // parent class
{
(Define sets of members)
}
Class class2:class1 //child class
{
(consume parent members here)
}
EX1:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ConsOverLoad
{
class LoadParent
{
public LoadParent() // Constructor
{
Console.WriteLine("consttructor
called");
}
public void test1()
{
Console.WriteLine("test1 method
called");
}
public void test2()
{
Console.WriteLine("test2 method
called");
}
}
class LoadChild : LoadParent // inherit parent class by child class.
{
public void test3()
{
Console.WriteLine("test3 method
called");
}
}
class entrypoint
{
static void Main(string[] args)
{
LoadChild ob = new LoadChild();
ob.test1();
ob.test2();
ob.test3();
Console.ReadLine();
}
}
}
//output:
//Constructor
called
//test1
method called
//test2
method called
//test3
method called
Rule-2)
In inheritance ,the child classes can consume the members of parents
class but a parent class will never consume the members of its child class that
are purely define under the child class.
EX2:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ConsOverLoad
{
class LoadParent
{
public LoadParent() // Constructor
{
Console.WriteLine("consttructor
called");
}
public void test1()
{
Console.WriteLine("test1 method
called");
}
public void test2()
{
Console.WriteLine("test2 method
called");
}
}
class LoadChild : LoadParent // inherit parent class by child class.
{
public void test3()
{
Console.WriteLine("test3 method
called");
}
}
class entrypoint
{
static void Main(string[] args)
{
LoadParent ob = new LoadParent();
ob.test1(); // Valid
ob.test2(); // Valid
ob.test3(); //
Invalid
Console.ReadLine();
}
}
}
//output:
//Error
Because parent class object can't call the child class members(or, methods).
Rule-3 :
Just like object of a class can be
assigned to the variable of the same class to make it a reference, it can also
be assigned to a variable of its parent to make it a reference and that
reference will start consuming the memory of its child class object.
EX:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ConsOverLoad
{
class LoadParent
{
public LoadParent() // Constructor
{
Console.WriteLine("consttructor
called");
}
public void test1()
{
Console.WriteLine("test1 method
called");
}
public void test2()
{
Console.WriteLine("test2 method
called");
}
}
class LoadChild : LoadParent // inherit parent class by child class.
{
public void test3()
{
Console.WriteLine("test3 method
called");
}
}
class entrypoint
{
static void Main(string[] args)
{
LoadChild ob = new LoadChild();
LoadParent p = ob;
//or,
LoadParent p = new LoadChild();
p.test1(); //
Valid.
p.test2(); //
Valid.
p.test3(); // Still
Invalid.
Console.ReadLine();
}
}
}
//output:
//Error
It can not possible to access child class member by parent. But child class
object //can access both members .
Note:
Rule-4 :
class constructor goes and called its
parent class constructor implicitly ok…But its implicitly calling will not
possible when parent class constructor have parameterized so , to resolve the
problem
Rule-5
Type of inheritance :
One type of inheritance in oops that is single inheritance only.
·
Single inheritance
·
Multiple inheritance. (.Net does not
support multiple inheritance , it suffer from ambiguity in class, so it uses in
interface)
Single inheritance means when a class
has just one class to it , we called as single inheritance.
Oops End Here.
______________________________________________________________________________________________________________________________________
Constructor
*It is a special type of method that
is present under a class responsible for initializing the variable of a class .
Each and every class require a constructor if we want to create a object of the
class.
*The name of the constructor method
is same as the name of the class and constructor can not return any value and
define as public.
Syntax:
[modifier] cons_name
(parameters)
{statements}
EX1:
using System;
using System.Collections;
namespace ConsOverLoad
{
class mycons
{
public mycons()
{
Console.WriteLine("Constructor
called when object is created ");
}
}
class entrypoint
{
static void Main(string[] args)
{
mycons obj = new mycons(); // no need to call, just created object here.
Console.ReadLine();
}
}
}
//output:
//Constructor
called when object is created
Type of constructor :
Constructor are of 2 types—
*parameter less constructor
*parameterized constructor.
A constructor that is define without
any parameter is a parameter less constructor. And if it is define with the
parameters then it is called as parameterized constructor.
Note: If a constructor is
parameterized , then we need to send the values those parameters while creating
object of a class.
EX2:
using System;
using System.Collections;
namespace ConsOverLoad
{
class mycons
{
public mycons(int x)
{
Console.WriteLine("Constructor
called when object is created ");
}
}
class entrypoint
{
static void Main(string[] args)
{
mycons obj = new mycons(10); // Here pass the
value only not called.
mycons obj1 = new mycons(20);
Console.ReadLine();
}
}
}
//output:
//Constructor
called when object is created
//Constructor
called when object is created
EX3:
NEED OF CONSTRUCTOR –
using System;
using System.Collections;
namespace ConsOverLoad
{
class math
{
int x, y;
public math(int x, int y)
{
this.x = x;
this.y = y;
}
public void add()
// no need to give any parameter.
{
Console.WriteLine(x+y);
}
public void sum()
// no need to give any parameter.
{
Console.WriteLine(x -y);
}
public void mul()
// no need to give any parameter.
{
Console.WriteLine(x * y);
}
public void div()
// no need to give any parameter.
{
Console.WriteLine(x / y);
}
}
class entrypoint
{
static void Main(string[] args)
{
math obj = new math(20,10); // Here pass the
value only, not called.
obj.add(); // here becz of constructor we need not put value in each
arithmetic operations.
obj.sum();
obj.mul();
obj.div();
Console.ReadLine();
}
}
}
//output:
//30
//10
//200
//2
……………………………………………
Abstract Class and Abstract Methos—
*A method without any method body is know as a abstract
method , it is contain only signature of the method without any implementation.
The class where we defined the abstract methods is called
as abstract class.
EX:
abstract class math // abstract class
{
Public void
abstract void add(int x, int y)
//abstract method
}
Rule1-
If a parent contains abstract method(s) then that abstract
method must be implementing in the child class by using ‘override’modifier
only.
Rule2-
An abstract class can contain both the
concrete(Non-abstract) methods and abstract methods in it. And if any child
class want to consume the concrete methods of its parent then first we have to
implements all the abstract methods of its parent otherwise child class can not
consume any concrete method of parent class.
EX-2: An Error program-
using System;
using System.Collections;
namespace ConsOverLoad
{
public abstract class AbsParent
// abstract class
{
public abstract void add(int x, int y); // abstract method1 .semicolon must
public abstract void sub(int x, int y); // abstract method2
.semicolon must
public void mul(int x, int y) // concrete method
{
Console.WriteLine(x * y);
}
public void div(int x, int y) // concrete method
{
Console.WriteLine(x / y);
}
}
class AbsChild:AbsParent // inheriting here
{
public void childMethod()
{
Console.WriteLine(" child
method called :");
}
}
class entrypoint
{
static void Main(string[] args)
{
AbsChild obj=new AbsChild();
obj.add(10,20); //Implements required in child class for
obj.sub(20,10); // all abstract methods of parent class.
obj.mul(2, 3);
obj.div(15, 5);
Console.ReadLine();
}
}
}
//output:
//Implements
required in child class for all abstract methods of parent class.
EX-2 Improve Error, Now implementing the abstract
methods under the child class.
using System;
using System.Collections;
namespace ConsOverLoad
{
public abstract class AbsParent
// abstract class
{
public abstract void add(int x, int y); // abstract method1 .semicolon must
public abstract void sub(int x, int y); // abstract method2
.semicolon must
public void mul(int x, int y) // concrete method
{
Console.WriteLine(x * y);
}
public void div(int x, int y) //
concrete method
{
Console.WriteLine(x / y);
}
}
class AbsChild : AbsParent // inheriting here
{
public override void add(int x, int y) // we must override,to implement
{
Console.WriteLine(x + y);
}
public override void sub(int x, int y) // we must override. To implement
{
Console.WriteLine(x - y);
}
}
class entrypoint
{
static void Main(string[] args)
{
AbsChild obj=new AbsChild();
obj.add(10,20);
obj.sub(20,10);
obj.mul(2, 3);
obj.div(15, 5);
Console.ReadLine();
}
}
}
//output:
//30
//10
//6
//3
//Here
we implemented all abstracts method of parent class by using ‘override’
modifier
Ex3:
No
need to implements all the abstract methods if any of the child class already
implemented. If first child class have not implemented the abstracts methods
the the next any childs class responsible to implements the abstract classes.
See below Example.
using System;
using System.Collections;
using System.Collections.Generic;
namespace ConsOverLoad
{
public abstract class AbsParent
// abstract class
{
public abstract void add(int x, int y); // abstract method1 .semicolon must
public abstract void sub(int x, int y); // abstract method2
.semicolon must
public void mul(int x, int y) // concrete method
{
Console.WriteLine(x * y);
}
public void div(int x, int y) // concrete method
{
Console.WriteLine(x / y);
}
}
class AbsChild : AbsParent // inheriting here
{
public override void add(int x, int y) // we must override, otherwise not possible to
implementing the abs Methods.
{
Console.WriteLine(x + y);
}
public override void sub(int x, int y) // we must override, otherwise not possible to implement
{
Console.WriteLine(x - y);
}
}
class AbsChild2 : AbsChild // 2nd child class
{
}
class entrypoint
{
static void Main(string[] args)
{
AbsChild2 obj=new AbsChild2(); //Here-we created 2nd child class object here. and consume
abstract class all methos, no need to implements again.
obj.add(10,20);
obj.sub(20,10);
obj.mul(2, 3);
obj.div(15, 5);
Console.ReadLine();
}
}
}
//output:
//30
//10
//6
//3
…………………………………………………………………...
Interface
Defn: It is also a user define type like a class but can
contain only abstract members on it.
* All the abstracts members that are defined under a
interface must be implemented under a
child class.
* A interface does not contains any non abstract
methods(members).
* The multiple inheritance through interface can possible
but multiple
inheritance through classes is not possible.
*Syntax for interface :
[modifiers]
interface [interface_name]
{
Abstract members
declare here or(abstract method declared here).
}
·
We can not declare variables under the interface.
·
Every abstract methods of an interface bydefault abstract so no need to
use ‘abstract’ modifier on it again (explicitly).
·
By default interface is public(i.e public interface), whereas private in
case of class.
·
Interface inherit multiple interface.
EX1
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace testprogs
{
interface A // by Default public interface A
{
// this
is abstract method but no need to write- abstract void add(); its by default
abstract
void add(int x, int y);
void sub(int x, int y);
}
interface B
{
void mul(int x, int y);
void div(int x, int y);
}
class Test : A, B // This class show multiple inheritance.
{
public void add(int x, int y)
{
Console.WriteLine(x+y);
}
public void sub(int x, int y)
{
Console.WriteLine(x-y);
}
public void mul(int x, int y)
{
Console.WriteLine(x*y);
}
public void div(int x, int y)
{
Console.WriteLine(x/y);
}
}
class Entry
{
static void Main(string[] args)
{
Test obj = new Test();
obj.add(20,10);
obj.sub(20, 10);
obj.mul(20, 10);
obj.div(20, 10);
or,
//Test
obj = new Test();
//A
i1 = obj;
//B
i2 = obj;
//create
a reference of interfaces with the help of child class object.
//i1.add(20,
10);
//i1.sub(20,
10);
//i2.mul(20,
10);
//i2.div(20,
10);
Console.ReadLine();
}
}
}
//output
//30
//10
//200
//2
EX2
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace testprogs
{
interface inter1
{
void Hello();
}
interface inter2
{
void Hello();
}
class Test : inter1, inter2
{
void inter1.Hello()
// possible
{
Console.WriteLine("Hello called
for interface1");
}
void inter2.Hello()
{
Console.WriteLine("Hello called
for interface2 ");
}
}
class intry
{
static void Main(string[] args)
{
Test obj = new Test();
inter1 i1 = obj; // child object reference must.
inter2 i2 = obj;
i1.Hello();
i2.Hello();
Console.ReadLine();
}
}
}
//output:
//Hello
claaed for interface1
//Hello
claaed for interface2
Delegate in C#
Def:Delegate is a type which holds the method(s) reference
in an object.
Effective use of delegate to improve the performance of
applications. The method can call in 2 different ways in c#
1)Using object of
a class if it is not static and
2)Name of the
class if it is static.
Using Delegate both
1 and 2 we can call the methods
Note:
*Delegate can be define under a namespace or under a class
also
Demo1: For class
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace testprogs
{
class c1
{
public static void m1()
{
Console.WriteLine("Method-1 for
class1");
}
}
class Entry
{
static void Main(string[] args)
{
c1.m1();
Console.ReadLine();
}
}
}
//output:
//Method-1
for class1
EX-1 single cast delegate example:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace testprogs
{
public delegate void del1(int x, int y, int z); // it is not
defined under a class.
public delegate string dell2(string s);
class Test
{
//here
we have implemented the delegate. so no. of parameters in delegate must same
no. of parameters in implemented method [myadd].
//delegate
variables name may differ as myadd(int a, int b, int c) we have taken here.
public void myadd(int a, int b, int c) // non-static method.
{
Console.WriteLine(a+b+c);
}
public static string myname(string s) // static method.
{
return "Hello" + s;
}
}
class Entry
{
static void Main(string[] args)
{
//
To call dell1, need to create object becz it is non static method
Test ob = new Test();
del1 d1 = ob.myadd;
d1(2, 3, 4);
//To
call dell2 no need to create object becz it is static method so
// through class name we can
call.
dell2 d2 = Test.myname; // very very important.
Console.WriteLine(d2("UJJWAL
PAUL"));
Console.ReadLine();
}
}
}
//Output:
//9
//HelloUJJWALPAUL
EX-2
MultiCast Delegate example:
When
delegate hold and invokes more than one methods is called multicast delegate
and such delegate-
*Return type must be void defined
*No. of parameters in delegate
should be same the no. of methods.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace testprogs
{
public delegate void dell(int x, int y);
class Test
{
public void add(int a, int b) // must void in case of multicast delegate.
{
Console.WriteLine(a+b);
}
public void sub(int a, int b)
{
Console.WriteLine(a - b);
}
public void mul(int a, int b)
{
Console.WriteLine(a * b);
}
public void div(int a, int b)
{
Console.WriteLine(a / b);
}
}
class Entry
{
static void Main(string[] args)
{
Test ob = new Test();
dell d1 = ob.add;
dell d2 = ob.sub;
dell d3 = ob.mul;
dell d4 = ob.div;
d1(20, 10);
d2(20, 10);
d3(20, 10);
d4(20, 10);
Console.ReadLine();
}
}
}
//output:
//30
//10
//200
//2
Ex-3
multicast delegate: (play with static method). No required Class object to call
method.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace testprogs
{
public delegate void multidell();
class Test
{
public static void display1() // static method
{
Console.WriteLine("Delhi");
}
public static void dispaly2() // static method
{
Console.WriteLine("mumbai");
}
}
class Entry
{
static void Main(string[] args)
{
multidell m1 = new multidell(Test.display1); // directly call by class name.
multidell m2 = new multidell(Test.dispaly2); // directly call by class name.
multidell m3 = m1 + m2;
// Delhi
Mumbai
multidell m4 = m2 + m1;
// Mumbai Delhi
multidell m5 = m3 + m1;
//Delhi Mumbai Delhi
multidell m6 = m3 - m1;
//m2 becz m3=m1+m2 so Mumbai
m3();
Console.WriteLine();
m4();
Console.WriteLine();
m5();
Console.WriteLine();
m6();
Console.ReadLine();
}
}
}
//Output:
//Delhi
//Mumbai
//Mumbai
//Delhi
//Delhi
//Mumbai
//Delhi
//Mumbai
(ACCESS MODIFIERS in C#)
Or,(Access specifiers)
1)public : (ALL can access)
- AnyWhere can accessible within the same
class , another classes ,
By same assembly and Another assembly also.
No restrict.
2)private : (Only within the same class can access)
-This is accessible within the same class
only outside the class
Not
possible to access.
3)protected :(within the same class and any drive(or
child) class can
Access)
-
4)internal :
(same assembly only can access)
-This is accessible within the same
assembly. it will not
accessible the any other assembly
5) Protected internal : (same assembly and another assembly by
Inheriting the base class whose member is
protected internal can
access)
- This is accessible Anywhere within the
same assemly.And also
accessible from within a derived(or child)
class of another
assemble.(Note:Here we need to inherit the
class of 1 st assemblt
by the another assembly. See pragim videos
for c#).
Important
Notes :
·
5-modifier:
Public,private,protected,internal,protected
internal.
·
Return Type: (2-types)
-return value(define int
string,char, bool.-All DataTypes)
-Non-return value(define as void)
*About class: (Default modifier is internal)
(modifier) class
(class_Name)
Case1: class
customer (Here Bydefault class modifier is private)-Valid
Case2: public class
customer (Here public class) // valid
Case3: protected
class customer (Here not possible)
//invalid
Case4: internal
class customer (Here internal class)//Valid
Case5: internal
class customer (Here protected internal class)-invalid
*About method:
(default Modifier is private)
(modifier)
(return type) (method_Name)(Parameters)
Below All are valid-
Case1:public void add(int x,int y)//public modifier.
Case2:void add(int
x,int y)// default private modifier.
Case3:protected
void add(int x,int y)//protected modifier.
Case4:internal void add(int x,int y)//internal modifier.
Case5:protected
internal void add(int x,int y)//protected internal
// modifier.
*About variable: (default modifier)
All modifiers
allowed as
public int x=10;
private int x=10;
protected int x=10;
internal int x=10;
protected internal
int x=10;
*static—
Example:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Csharp_Test
{
//***
public class ReadOnlyTest
{
class SampleClass
{
public int x;
//
Initialize a readonly field
public readonly int y = 25;
public readonly int z;
public SampleClass()
{
//
Initialize a readonly instance field
z = 24;
}
public SampleClass(int p1, int p2, int p3)
{
x = p1;
y = p2;
z = p3;
}
}
static void Main()
{
SampleClass p1 = new SampleClass(11, 21, 32); // OK
Console.WriteLine("p1: x={0},
y={1}, z={2}", p1.x, p1.y, p1.z);
SampleClass p2 = new SampleClass();
p2.x = 55; // OK
Console.WriteLine("p2: x={0},
y={1}, z={2}", p2.x, p2.y, p2.z);
Console.ReadLine();
}
}
/*
Output:
p1: x=11, y=21, z=32
p2: x=55, y=25, z=24
*/
}
Use the access
modifiers, public, protected, internal, or private, to specify one of the following declared
accessibility levels for members.
Declared accessibility
|
Meaning
|
Public
|
Access is not restricted.
|
Protected
|
Access is limited to the containing class or
types derived from the containing class.
|
Internal
|
Access is limited to the current assembly.
|
protected internal
|
Access is limited to the current assembly or
types derived from the containing class.
|
Private
|
Access is limited to the containing type.
|
Only one access
modifier is allowed for a member or type, except when you use the protected internal combination.
Access modifiers are
not allowed on namespaces. Namespaces have no access restrictions.
Depending on the
context in which a member declaration occurs, only certain declared
accessibilities are permitted. If no access modifier is specified in a member
declaration, a default accessibility is used.
Top-level types, which
are not nested in other types, can only have internal or public accessibility.
The default accessibility for these types is internal.
Nested types, which
are members of other types, can have declared accessibilities as indicated in
the following table.
Members of
|
Default member accessibility
|
Allowed declared accessibility of the member
|
Enum
|
Public
|
None
|
Class
|
Private
|
public
protected
internal
private
protected internal
|
Interface
|
Public
|
None
|
Struct
|
Private
|
public
internal
private
|
All C#
KeyWords
C#
Keywords
Visual Studio 2013
Keywords are
predefined, reserved identifiers that have special meanings to the compiler.
They cannot be used as identifiers in your program unless they include @ as a prefix. For
example, @if is a valid identifier but if is not
because if is a keyword.
The first table in
this topic lists keywords that are reserved identifiers in any part of a C#
program. The second table in this topic lists the contextual keywords in C#.
Contextual keywords have special meaning only in a limited program context and
can be used as identifiers outside that context. Generally, as new keywords are
added to the C# language, they are added as contextual keywords in order to
avoid breaking programs written in earlier versions.
A contextual keyword
is used to provide a specific meaning in the code, but it is not a reserved
word in C#. Some contextual keywords, such as partial and where,
have special meanings in two or more contexts.
Non-nested types, enumeration and delegate accessibilities (may only have internal or
public accessibility)
| Default | Permitted declared accessibilities
------------------------------------------------------------------
namespace | public | none (always implicitly public)
enum | public | none (always implicitly public)
interface | internal | public, internal
class | internal | public, internal
struct | internal | public, internal
delegate | internal | public, internal
Nested type
and member accessiblities
| Default | Permitted declared accessibilities
------------------------------------------------------------------
namespace | public | none (always implicitly public)
enum | public | none (always implicitly public)
interface | public | none
class | private | All¹
struct | private | public, internal, private²
delegate | private | All¹
constructor | private | All¹
interface member | public | none (always implicitly public)
method | private | All¹
field | private | All¹
user-defined
operator|
none | public (must be declared public)
¹ All === public,
protected, internal, private, protected internal
² structs cannot inherit
from structs or classes (although they can, interfaces), hence protected is not
a valid modifier
The accessibility of a
nested type depends on its accessibility domain, which is determined by both
the declared accessibility of the member and the accessibility domain of the
immediately containing type. However, the accessibility domain of a nested type
cannot exceed that of the containing type.
Note: CIL also has the provision for protected and internal (as opposed to the existing
protected "or" internal), but to my knowledge this is not currently
available for use in C#.
Absolutely knowledgeable content. Thanks for sharing this kind of content. It is very helpful and very informative and I really learned a lot from it. Further More Information About Dot Net Training Institute in Delhi So Contact Here-+91-9311002620 Or Visit Website- https://htsindia.com/Courses/microsoft-courses/dotnettraininginstituteinsouthdelhi
ReplyDelete