SlideShare a Scribd company logo
Oops
Class – collection of objects
Object – instance of a class
Encapsulation – process of binding data members and member functions into single un
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ConsoleApplication1
{
class rectangle
{
public double length;
public double width;
public double GetArea()
{
return length * width;
}
public void Display()
{
Console.WriteLine("Length: {0}", length);
Console.WriteLine("Width: {0}", width);
Console.WriteLine("Area: {0}", GetArea());
}
}
class Program
{
static void Main(string[] args)
{
rectangle r = new rectangle();
r.length = 3.5;
r.width = 4.6;
r.Display();
}
}
}
Abstraction - Abstraction is a process of hiding the implementation details and displaying the essential features.
.Net has five access Specifiers
Public -- Accessible outside the class through object reference.
Private -- Accessible inside the class only through member functions.
Protected -- Just like private but Accessible in derived classes also through member
functions.
Internal -- Visible inside the assembly. Accessible through objects.
Protected Internal -- Visible inside the assembly through objects and in derived classes outside the assembly through member func
Public
A1 A2
C1 C3
√ √
C2 C4
√ √
Protected internal
A1 A2
C1 C3:C2
√ √
C2 C4
√ ×
Private
A1 A2
C1 C3
√ ×
C2 C4
× ×
Protected
A1 A2
C1 C3
√ ×
C2:C1 C4
√ ×
Internal
A1 A2
C1 C3
√ ×
C2 C4
√ ×
Inheritance:
Inheritance is a process of deriving the new class from already existing class
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ConsoleApplication1
{
class Shape
{
public void setWidth(int w)
{
width = w;
}
public void setHeight(int h)
{
height = h;
}
protected int width;
protected int height;
}
// Derived class
class Rectangle : Shape
{
public int getArea()
{
return (width * height);
}
}
class Program
{
static void Main(string[] args)
{
Rectangle r = new Rectangle();
r.setWidth(9);
r.setHeight(5);
r.getArea();
Console.WriteLine(r.getArea());
}
}
}
Polymorphism:
When a message can be processed in different ways is called polymorphism. Polymorphism means many forms.
Polymorphism is of two types:
Compile time polymorphism/Overloading – method name must be same parameters and return type may be varied
Function overloading
Operator overloading
Runtime polymorphism/Overriding – changing the functionality if a method without changing its signature
Function Overloading
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ConsoleApplication1
{
class Printdata
{
void print(int i)
{
Console.WriteLine("Printing int: {0}", i);
}
void print(double f)
{
Console.WriteLine("Printing float: {0}", f);
}
void print(string s)
{
Console.WriteLine("Printing string: {0}", s);
}
class Program
{
static void Main(string[] args)
{
Printdata p = new Printdata();
p.print(2);
p.print(3.6);
p.print("pavan");
Console.ReadKey();
}
}
}
}
Operator Overloading
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ConsoleApplication1
{
class Box
{
private double length; // Length of a box
private double width; // Width of a box
private double height; // Height of a box
public double getVolume()
{
return length * width * height;
}
public void setLength( double len )
{
length = len;
}
public void setWidth( double wid )
{
width = wid;
}
public void setHeight( double hei )
{
height = hei;
}
// Overload + operator to add two Box objects.
public static Box operator+ (Box b, Box c)
{
Box box = new Box();
box.length = b.length + c.length;
box.width = b.width + c.width;
box.height = b.height + c.height;
return box;
}
}
class Program
{
static void Main(string[] args)
{
Box Box1 = new Box(); // Declare Box1 of type Box
Box Box2 = new Box(); // Declare Box2 of type Box
Box Box3 = new Box(); // Declare Box3 of type Box
double volume = 0.0; // Store the volume of a box here
// box 1 specification
Box1.setLength(6.0);
Box1.setWidth(7.0);
Box1.setHeight(5.0);
// box 2 specification
Box2.setLength(12.0);
Box2.setWidth(13.0);
Box2.setHeight(10.0);
// volume of box 1
volume = Box1.getVolume();
Console.WriteLine("Volume of Box1 : {0}", volume);
// volume of box 2
volume = Box2.getVolume();
Console.WriteLine("Volume of Box2 : {0}", volume);
// Add two object as follows:
Box3 = Box1 + Box2;
// volume of box 3
volume = Box3.getVolume();
Console.WriteLine("Volume of Box3 : {0}", volume);
Console.ReadKey();
}
}
}
Runtime polymorphism/Overriding
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ConsoleApplication1
{
public class BaseClass
{
public virtual void Method1()
{
Console.Write("Base Class Method");
}
}
// Derived class
public class DerivedClass : BaseClass
{
public override void Method1()
{
Console.Write("Derived Class Method");
}
}
class Program
{
static void Main(string[] args)
{
// calling the overriden method
DerivedClass objDC = new DerivedClass();
objDC.Method1();
// calling the baesd class method
BaseClass objBC = (BaseClass)objDC;
objDC.Method1();
}
}
}
Overriding in C#
1. Virtual – This keyword is used with a base class which signifies that the method
of a base class can be overridden.
2. Override – This keyword is used with a derived class which signifies that derived
class overrides a method of a base class.
3. Base – This keyword is used in a derived class to call the base class method.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ConsoleApplication1
{
class Bird // base class
{
public void fly() // base class method
{
Console.WriteLine("Birds are flying");
}
}
class Peacock : Bird // derived class
{
public new void fly() // derived class method
{
Console.WriteLine("Peacock is flying");
}
}
class Program
{
static void Main(string[] args)
{
Bird b = new Peacock();
b.fly();
Console.ReadLine();
}
}
}
Example 1 – Without Virtual and Override Keywords
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ConsoleApplication1
{
class Bird // base class
{
public void fly() // base class method
{
Console.WriteLine("Birds are flying");
}
}
class Peacock : Bird // derived class
{
public new void fly() // derived class method
{
Console.WriteLine("Peacock is flying");
}
}
class Program
{
static void Main(string[] args)
{
Bird b = new Peacock();
b.fly();
Console.ReadLine();
}
}
}
Example 2 (a)- With Virtual and Override Keywords
Constructors
A class constructor is a special member function of a class that is executed whenever we create new objects of that class.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ConsoleApplication1
{
class Line
{
private double length; // Length of a line
public Line()
{
Console.WriteLine("Object is being created");
}
public void setLength(double len)
{
length = len;
}
public double getLength()
{
return length;
}
class Program
{
static void Main(string[] args)
{
Line line = new Line();
// set line length
line.setLength(6.0);
Console.WriteLine("Length of line : {0}", line.getLength());
Console.ReadKey();
}
}
}
}
Destructors
A destructor is a special member function of a class that is executed whenever an object of its class goes out of scope. A destructor will have exact
same name as the class prefixed with a tilde (~) and it can neither return a value nor can it take any parameters.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ConsoleApplication1
{
class Line
{
private double length; // Length of a line
public Line() // constructor
{
Console.WriteLine("Object is being created");
}
~Line() //destructor
{
Console.WriteLine("Object is being deleted");
}
public void setLength(double len)
{
length = len;
}
public double getLength()
{
return length;
}
class Program
{
static void Main(string[] args)
{
Line line = new Line();
// set line length
line.setLength(6.0);
Console.WriteLine("Length of line : {0}", line.getLength());
}
}
}
}
Interface : should be used if you want to imply a rule on the components which may
or may not be related to each other
Abstract Class : should be used where you want to have some basic or default behavior
or implementation for components related to each other
Pros:
Interface: Allows multiple inheritance
provides abstraction by not exposing what exact kind of object is being used in the contex
provides consistency by a specific signature of the contract
Abstract Class: faster then interface
has flexibility in the implementation (you can implement it fully or partially) can be
easily changed without breaking the derived classes
Cons:
Interface : Must implement all the contracts defined
cannot have variables or delegates
once defined cannot be changed without breaking all the classes
Abstract Class : cannot be instantiated does not support multiple inheritance
Abstract class
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ConsoleApplication1
{
abstract class M1
{
public int add(int a, int b)
{
return (a + b);
}
}
class M2 :M1
{
public int mul(int a, int b)
{
return a * b;
}
}
class Program
{
static void Main(string[] args)
{
M2 ob = new M2();
int result = ob.add(10, 20);
Console.WriteLine("the result is {0}", result);
int muls = ob.mul(10, 20);
Console.WriteLine("the result is {0}", muls);
Console.ReadLine();
}
}
}
Delegates
C# delegates are similar to pointers to functions in C or C++. A delegate is a reference type variable that holds the reference to a method.
The reference can be changed at runtime.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
delegate int NumberChanger(int n);
namespace ConsoleApplication1
{
class TestDelegate
{
static int num = 10;
public static int AddNum(int p)
{
num += p;
return num;
}
public static int MultNum(int q)
{
num *= q;
return num;
}
public static int getNum()
{
return num;
}
class Program
{
static void Main(string[] args)
{ //create delegate instances
NumberChanger nc1 = new NumberChanger(AddNum);
NumberChanger nc2 = new NumberChanger(MultNum); //calling the methods using the delegate objects
nc1(25);
Console.WriteLine("Value of Num: {0}", getNum());
nc2(5);
Console.WriteLine("Value of Num: {0}", getNum());
Console.ReadKey();
}
}
}
}
Sealed Class
Class is defined as sealed class, this class cannot be inherited.
A sealed class cannot be used as a base class. For this reason, it cannot also be an abstract class.
Sealed classes are primarily used to prevent derivation.
Because they can never be used as a base class,
some run-time optimizations can make calling sealed class members slightly faster.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
SealedClass sealedCls = new SealedClass();
int total = sealedCls.Add(4, 5);
Console.WriteLine("Total = " + total.ToString());
}
}
// Sealed class
sealed class SealedClass
{
public int Add(int x, int y)
{
return x + y;
}
}
}
}
Partial Class
Splitting up of a class in to two or more sub classes is called as partial classes.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ConsoleApplication1
{
public partial class MyTest
{
private int a;
private int b;
public void getAnswer(int a, int b)
{
this.a = a;
this.b = b;
}
}
public partial class MyTest
{
public void PrintCoOrds()
{
Console.WriteLine("Integer values: {0},{1}", a, b);
Console.WriteLine("Addition: {0}", a + b);
Console.WriteLine("Mulitiply: {0}", a * b);
}
}
class Program
{
static void Main(string[] args)
{
MyTest ts = new MyTest();
ts.getAnswer(12, 25);
ts.PrintCoOrds();
Console.Read();
}
}
}

More Related Content

PPTX
Inheritance
DOCX
C# Unit 2 notes
PDF
Java design patterns
PPTX
C# .NET: Language Features and Creating .NET Projects, Namespaces Classes and...
DOCX
PPTX
Polymorphism in C# Function overloading in C#
PPTX
Class and object
PDF
Polymorphism, Abstarct Class and Interface in C#
Inheritance
C# Unit 2 notes
Java design patterns
C# .NET: Language Features and Creating .NET Projects, Namespaces Classes and...
Polymorphism in C# Function overloading in C#
Class and object
Polymorphism, Abstarct Class and Interface in C#

Similar to Presentation.pptx (20)

PPS
Interface
DOCX
Srgoc dotnet
PPTX
Advanced programming topics asma
PDF
Global objects in Node.pdf
PPTX
ECMAScript 2015
PPTX
Polymorphism
PPT
Java oops PPT
PPTX
Inheritance & interface ppt Inheritance
PPT
02-OOP with Java.ppt
PPT
025466482929 -OOP with Java Development Kit.ppt
PPT
04 threads
PPT
Thread
DOCX
using System.docx
PDF
Xamarin: Namespace and Classes
PDF
Object Oriented Solved Practice Programs C++ Exams
PPT
Object Oriented Programming with Java
PPTX
Polymorphism & Templates
PPTX
FINAL_DAY10_INTERFACES_roles and benefits.pptx
PPT
Tech talk
PDF
C# (This keyword, Properties, Inheritance, Base Keyword)
Interface
Srgoc dotnet
Advanced programming topics asma
Global objects in Node.pdf
ECMAScript 2015
Polymorphism
Java oops PPT
Inheritance & interface ppt Inheritance
02-OOP with Java.ppt
025466482929 -OOP with Java Development Kit.ppt
04 threads
Thread
using System.docx
Xamarin: Namespace and Classes
Object Oriented Solved Practice Programs C++ Exams
Object Oriented Programming with Java
Polymorphism & Templates
FINAL_DAY10_INTERFACES_roles and benefits.pptx
Tech talk
C# (This keyword, Properties, Inheritance, Base Keyword)

Recently uploaded (20)

PDF
OBE - B.A.(HON'S) IN INTERIOR ARCHITECTURE -Ar.MOHIUDDIN.pdf
PDF
Computing-Curriculum for Schools in Ghana
PDF
HVAC Specification 2024 according to central public works department
PDF
LDMMIA Reiki Yoga Finals Review Spring Summer
PDF
Chinmaya Tiranga quiz Grand Finale.pdf
PDF
Empowerment Technology for Senior High School Guide
PDF
ChatGPT for Dummies - Pam Baker Ccesa007.pdf
PDF
Τίμαιος είναι φιλοσοφικός διάλογος του Πλάτωνα
PDF
MBA _Common_ 2nd year Syllabus _2021-22_.pdf
PDF
BP 704 T. NOVEL DRUG DELIVERY SYSTEMS (UNIT 1)
PDF
My India Quiz Book_20210205121199924.pdf
PDF
CISA (Certified Information Systems Auditor) Domain-Wise Summary.pdf
PPTX
ELIAS-SEZIURE AND EPilepsy semmioan session.pptx
PDF
What if we spent less time fighting change, and more time building what’s rig...
PDF
Weekly quiz Compilation Jan -July 25.pdf
PDF
IGGE1 Understanding the Self1234567891011
PPTX
Onco Emergencies - Spinal cord compression Superior vena cava syndrome Febr...
PDF
advance database management system book.pdf
DOC
Soft-furnishing-By-Architect-A.F.M.Mohiuddin-Akhand.doc
PDF
Paper A Mock Exam 9_ Attempt review.pdf.
OBE - B.A.(HON'S) IN INTERIOR ARCHITECTURE -Ar.MOHIUDDIN.pdf
Computing-Curriculum for Schools in Ghana
HVAC Specification 2024 according to central public works department
LDMMIA Reiki Yoga Finals Review Spring Summer
Chinmaya Tiranga quiz Grand Finale.pdf
Empowerment Technology for Senior High School Guide
ChatGPT for Dummies - Pam Baker Ccesa007.pdf
Τίμαιος είναι φιλοσοφικός διάλογος του Πλάτωνα
MBA _Common_ 2nd year Syllabus _2021-22_.pdf
BP 704 T. NOVEL DRUG DELIVERY SYSTEMS (UNIT 1)
My India Quiz Book_20210205121199924.pdf
CISA (Certified Information Systems Auditor) Domain-Wise Summary.pdf
ELIAS-SEZIURE AND EPilepsy semmioan session.pptx
What if we spent less time fighting change, and more time building what’s rig...
Weekly quiz Compilation Jan -July 25.pdf
IGGE1 Understanding the Self1234567891011
Onco Emergencies - Spinal cord compression Superior vena cava syndrome Febr...
advance database management system book.pdf
Soft-furnishing-By-Architect-A.F.M.Mohiuddin-Akhand.doc
Paper A Mock Exam 9_ Attempt review.pdf.

Presentation.pptx

  • 1. Oops Class – collection of objects Object – instance of a class Encapsulation – process of binding data members and member functions into single un using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace ConsoleApplication1 { class rectangle { public double length; public double width; public double GetArea() { return length * width; } public void Display() { Console.WriteLine("Length: {0}", length); Console.WriteLine("Width: {0}", width); Console.WriteLine("Area: {0}", GetArea()); } }
  • 2. class Program { static void Main(string[] args) { rectangle r = new rectangle(); r.length = 3.5; r.width = 4.6; r.Display(); } } } Abstraction - Abstraction is a process of hiding the implementation details and displaying the essential features. .Net has five access Specifiers Public -- Accessible outside the class through object reference. Private -- Accessible inside the class only through member functions. Protected -- Just like private but Accessible in derived classes also through member functions. Internal -- Visible inside the assembly. Accessible through objects. Protected Internal -- Visible inside the assembly through objects and in derived classes outside the assembly through member func
  • 3. Public A1 A2 C1 C3 √ √ C2 C4 √ √ Protected internal A1 A2 C1 C3:C2 √ √ C2 C4 √ × Private A1 A2 C1 C3 √ × C2 C4 × × Protected A1 A2 C1 C3 √ × C2:C1 C4 √ × Internal A1 A2 C1 C3 √ × C2 C4 √ ×
  • 4. Inheritance: Inheritance is a process of deriving the new class from already existing class using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace ConsoleApplication1 { class Shape { public void setWidth(int w) { width = w; } public void setHeight(int h) { height = h; } protected int width; protected int height; } // Derived class class Rectangle : Shape { public int getArea() { return (width * height); } }
  • 5. class Program { static void Main(string[] args) { Rectangle r = new Rectangle(); r.setWidth(9); r.setHeight(5); r.getArea(); Console.WriteLine(r.getArea()); } } } Polymorphism: When a message can be processed in different ways is called polymorphism. Polymorphism means many forms. Polymorphism is of two types: Compile time polymorphism/Overloading – method name must be same parameters and return type may be varied Function overloading Operator overloading Runtime polymorphism/Overriding – changing the functionality if a method without changing its signature
  • 6. Function Overloading using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace ConsoleApplication1 { class Printdata { void print(int i) { Console.WriteLine("Printing int: {0}", i); } void print(double f) { Console.WriteLine("Printing float: {0}", f); } void print(string s) { Console.WriteLine("Printing string: {0}", s); } class Program { static void Main(string[] args) { Printdata p = new Printdata(); p.print(2); p.print(3.6); p.print("pavan"); Console.ReadKey(); } } } }
  • 7. Operator Overloading using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace ConsoleApplication1 { class Box { private double length; // Length of a box private double width; // Width of a box private double height; // Height of a box public double getVolume() { return length * width * height; } public void setLength( double len ) { length = len; } public void setWidth( double wid ) { width = wid; } public void setHeight( double hei ) { height = hei; }
  • 8. // Overload + operator to add two Box objects. public static Box operator+ (Box b, Box c) { Box box = new Box(); box.length = b.length + c.length; box.width = b.width + c.width; box.height = b.height + c.height; return box; } } class Program { static void Main(string[] args) { Box Box1 = new Box(); // Declare Box1 of type Box Box Box2 = new Box(); // Declare Box2 of type Box Box Box3 = new Box(); // Declare Box3 of type Box double volume = 0.0; // Store the volume of a box here // box 1 specification Box1.setLength(6.0); Box1.setWidth(7.0); Box1.setHeight(5.0); // box 2 specification Box2.setLength(12.0); Box2.setWidth(13.0); Box2.setHeight(10.0);
  • 9. // volume of box 1 volume = Box1.getVolume(); Console.WriteLine("Volume of Box1 : {0}", volume); // volume of box 2 volume = Box2.getVolume(); Console.WriteLine("Volume of Box2 : {0}", volume); // Add two object as follows: Box3 = Box1 + Box2; // volume of box 3 volume = Box3.getVolume(); Console.WriteLine("Volume of Box3 : {0}", volume); Console.ReadKey(); } } }
  • 10. Runtime polymorphism/Overriding using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace ConsoleApplication1 { public class BaseClass { public virtual void Method1() { Console.Write("Base Class Method"); } } // Derived class public class DerivedClass : BaseClass { public override void Method1() { Console.Write("Derived Class Method"); } } class Program { static void Main(string[] args) { // calling the overriden method DerivedClass objDC = new DerivedClass(); objDC.Method1(); // calling the baesd class method BaseClass objBC = (BaseClass)objDC; objDC.Method1(); } } }
  • 11. Overriding in C# 1. Virtual – This keyword is used with a base class which signifies that the method of a base class can be overridden. 2. Override – This keyword is used with a derived class which signifies that derived class overrides a method of a base class. 3. Base – This keyword is used in a derived class to call the base class method.
  • 12. using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace ConsoleApplication1 { class Bird // base class { public void fly() // base class method { Console.WriteLine("Birds are flying"); } } class Peacock : Bird // derived class { public new void fly() // derived class method { Console.WriteLine("Peacock is flying"); } } class Program { static void Main(string[] args) { Bird b = new Peacock(); b.fly(); Console.ReadLine(); } } } Example 1 – Without Virtual and Override Keywords
  • 13. using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace ConsoleApplication1 { class Bird // base class { public void fly() // base class method { Console.WriteLine("Birds are flying"); } } class Peacock : Bird // derived class { public new void fly() // derived class method { Console.WriteLine("Peacock is flying"); } } class Program { static void Main(string[] args) { Bird b = new Peacock(); b.fly(); Console.ReadLine(); } } } Example 2 (a)- With Virtual and Override Keywords
  • 14. Constructors A class constructor is a special member function of a class that is executed whenever we create new objects of that class. using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace ConsoleApplication1 { class Line { private double length; // Length of a line public Line() { Console.WriteLine("Object is being created"); } public void setLength(double len) { length = len; } public double getLength() { return length; } class Program { static void Main(string[] args) { Line line = new Line(); // set line length line.setLength(6.0); Console.WriteLine("Length of line : {0}", line.getLength()); Console.ReadKey(); } } } }
  • 15. Destructors A destructor is a special member function of a class that is executed whenever an object of its class goes out of scope. A destructor will have exact same name as the class prefixed with a tilde (~) and it can neither return a value nor can it take any parameters. using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace ConsoleApplication1 { class Line { private double length; // Length of a line public Line() // constructor { Console.WriteLine("Object is being created"); } ~Line() //destructor { Console.WriteLine("Object is being deleted"); } public void setLength(double len) { length = len; } public double getLength() { return length; } class Program { static void Main(string[] args) { Line line = new Line(); // set line length line.setLength(6.0); Console.WriteLine("Length of line : {0}", line.getLength()); } } } }
  • 16. Interface : should be used if you want to imply a rule on the components which may or may not be related to each other Abstract Class : should be used where you want to have some basic or default behavior or implementation for components related to each other Pros: Interface: Allows multiple inheritance provides abstraction by not exposing what exact kind of object is being used in the contex provides consistency by a specific signature of the contract Abstract Class: faster then interface has flexibility in the implementation (you can implement it fully or partially) can be easily changed without breaking the derived classes Cons: Interface : Must implement all the contracts defined cannot have variables or delegates once defined cannot be changed without breaking all the classes Abstract Class : cannot be instantiated does not support multiple inheritance
  • 17. Abstract class using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace ConsoleApplication1 { abstract class M1 { public int add(int a, int b) { return (a + b); } } class M2 :M1 { public int mul(int a, int b) { return a * b; } } class Program { static void Main(string[] args) { M2 ob = new M2(); int result = ob.add(10, 20); Console.WriteLine("the result is {0}", result); int muls = ob.mul(10, 20); Console.WriteLine("the result is {0}", muls); Console.ReadLine(); } } }
  • 18. Delegates C# delegates are similar to pointers to functions in C or C++. A delegate is a reference type variable that holds the reference to a method. The reference can be changed at runtime. using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; delegate int NumberChanger(int n); namespace ConsoleApplication1 { class TestDelegate { static int num = 10; public static int AddNum(int p) { num += p; return num; } public static int MultNum(int q) { num *= q; return num; } public static int getNum() { return num; } class Program { static void Main(string[] args) { //create delegate instances NumberChanger nc1 = new NumberChanger(AddNum); NumberChanger nc2 = new NumberChanger(MultNum); //calling the methods using the delegate objects nc1(25); Console.WriteLine("Value of Num: {0}", getNum()); nc2(5); Console.WriteLine("Value of Num: {0}", getNum()); Console.ReadKey(); } } } }
  • 19. Sealed Class Class is defined as sealed class, this class cannot be inherited. A sealed class cannot be used as a base class. For this reason, it cannot also be an abstract class. Sealed classes are primarily used to prevent derivation. Because they can never be used as a base class, some run-time optimizations can make calling sealed class members slightly faster. using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace ConsoleApplication1 { class Program { static void Main(string[] args) { SealedClass sealedCls = new SealedClass(); int total = sealedCls.Add(4, 5); Console.WriteLine("Total = " + total.ToString()); } } // Sealed class sealed class SealedClass { public int Add(int x, int y) { return x + y; } } } }
  • 20. Partial Class Splitting up of a class in to two or more sub classes is called as partial classes. using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace ConsoleApplication1 { public partial class MyTest { private int a; private int b; public void getAnswer(int a, int b) { this.a = a; this.b = b; } } public partial class MyTest { public void PrintCoOrds() { Console.WriteLine("Integer values: {0},{1}", a, b); Console.WriteLine("Addition: {0}", a + b); Console.WriteLine("Mulitiply: {0}", a * b); } } class Program { static void Main(string[] args) { MyTest ts = new MyTest(); ts.getAnswer(12, 25); ts.PrintCoOrds(); Console.Read(); } } }