SlideShare a Scribd company logo
Created By-

PRANAB BHATTACHARYA
MCA-TY
Roll no. : 14
 Overview
 Classes and objects

Class members

Methods

Constructors

Destructors

Nested Classes

Access Modifiers and Access levels

Static (Shared) Classes and Members

Anonymous types
 Inheritance

Overriding members
• Sealed and Abstract classes
 Interfaces
1
o .NET

Framework languages provide full support for
Object-oriented programming
o Encapsulation, Inheritance, Polymorphism
o Encapsulation means that a group of related
properties, methods, and other members are treated as a
single unit or object.
o Inheritance describes the ability to create new
classes based on an existing class.
o Polymorphism means that you can have multiple
classes that can be used interchangeably, even though
each class implements the same properties or methods
in different ways.

2
o Classes describe the type of objects…
o …while objects are usable instances of classes.
o Using the blueprint analogy, a class is a
blueprint, and an object is a building made from
that blueprint.
To define a class and object:
class SampleClass
{
}
SampleClass SC1 = new SampleClass();
3
o A method is an action that an object can perform.
To define a method of a class:
class SampleClass
{
public int sampleMethod(string sampleParam)
{
// Insert code here
}
}
4
o A class can have several implementations, or
overloads, of the same method that differ in the
number of parameters or parameter types.
oTo overload a method:
public int sampleMethod(string sampleParam) {};
public int sampleMethod(int sampleParam) {}
Public int sampleMethod(int param1,int param2){};
o In most cases you declare a method within a class
definition. However, both Visual Basic and C# also
support extension methods that allow you to add
methods to an existing class outside the actual

5
o Constructors are class methods that are executed
automatically when an object of a given type is created. (Run
before any other code)
o Constructors usually Initialize the data members.
oSupport Overloading.

To define a constructor for a class:
public class SampleClass
{
int I;
public SampleClass()
{
I = 1;
}

6
o Destructors are used to destruct instances of classes.
o In the .NET Framework, the garbage collector
automatically manages the allocation and release of
memory for the managed objects in your application.
o Destructors are still needed to clean up any
unmanaged resources that your application creates.
o There can be only one destructor for a class.
7
o A class defined within another class is called nested.
(Default - private)
class Container
{
class Nested
{
// Add code here.
}
}

o To create an instance of the nested class, use the name
of the container class followed by the dot and then
followed by the name of the nested class:
Container.Nested nestedInstance = new

8
o All classes and class members can specify what access level
they provide to other classes by using access modifiers.
The following access modifiers are available:
C# Modifier
Definition
___________________________________________________
Public
The type or member can be accessed by any other
code in
the same assembly or another assembly that
references it.
Private
The type or member can only be accessed by code
in the
same class.
Protected The type or member can only be accessed by code
in the
same class or in a derived class.
Internal
The type or member can be accessed by any
code in the

9
o A static member of the class is a
property, procedure, or field that is shared by all
instances of a class.
o To define a static (shared) member:
static class SampleClass
{
public static string SampleString = "Sample
String";
}

10
o To access the static member, use the name of the
class without creating an object of this class:
Console.WriteLine(SampleClass.SampleStrin
g);
o Static (shared) classes in C# have static (shared)
members only and cannot be instantiated.
o Static (shared) members also cannot access nonstatic (non-shared) properties, fields or methods
11
o Enable you to create objects without writing a class
definition for the data type. (compiler generates a
class)
oThe class has no usable name and contains the
properties you specify in declaring the object.
o To create an instance of an anonymous type:
// sampleObject is an instance of a simple anonymous
type.
var sampleObject =
new { FirstProperty = "A", SecondProperty = "B" };

12
o Creates a new class that reuses, extends, and modifies
the behavior that is defined in another class.
Base Class
– The class whose members are
inherited
Derived Class – The class that inherits those
members.
o In C#, all classes implicitely inherit from the Object
class.
* .NET Framework doesn’t support MULTIPLE
INHERITANCE i.e. One derived class can not have more
than One base class
Syntax :

13
o By default all classes can be inherited.
o We can specify whether a class must not be used as a
base class, or create a class that can be used as a base
class only.
To specify that a class cannot be used as a base class:
public sealed class A { }

 To specify that a class can be used as a base class
only and cannot be instantiated :
public abstract class B { }

14
o By default, a derived class inherits all members
from its base class.
oyou want to change the behavior of the inherited
member, you need to override it
oWhat is Overriding ??
-> Overriding is defining a new implementation of a
method, property or event in the derived class.
15
Overriding modifiers..
C# Modifier
Definition
virtual (C# Reference) - Allows a class member to be
overridden in a derived class.
override (C# Reference) - Overrides a virtual
(overridable) member defined in the base class.
abstract (C# Reference) - Requires that a class member
to be overridden in the derived class.

16
o Interfaces, like classes, define a set of
properties, methods, and events.
o But unlike classes, interfaces do not provide
implementation.
oThey are implemented by classes, and defined
as separate entities from classes.

oAn interface represents a contract, in that a
class that implements an interface must
implement every aspect of that interface exactly

17
o To define an interface:
interface ISampleInterface
{
void doSomething();
}

o To implement an interface in a class:
class SampleClass : ISampleInterface
{
void ISampleInterface.doSomething()
{
// Method implementation.
}
}

18
19

More Related Content

PPT
C# Basics
PPTX
C# classes objects
PPT
Abstract class
PPT
C#.NET
PDF
Chapter 02: Classes Objects and Methods Java by Tushar B Kute
PPTX
Object oriented programming in python
PDF
Object oriented programming With C#
C# Basics
C# classes objects
Abstract class
C#.NET
Chapter 02: Classes Objects and Methods Java by Tushar B Kute
Object oriented programming in python
Object oriented programming With C#

What's hot (20)

PDF
PPTX
Basic Concepts of OOPs (Object Oriented Programming in Java)
PPT
C# basics
PPT
C# Exceptions Handling
PDF
PPTX
Exception Handling in C#
PPTX
Oops concept in c++ unit 3 -topic 4
PPT
Inheritance and Polymorphism
PPTX
PPT
Abstract class in java
PPTX
Classes and Objects in C#
PDF
Object-oriented Programming-with C#
PPTX
Delegates and events
PPTX
C# Constructors
PPT
Inheritance C#
PPTX
Object oriented programming
PPTX
Introduction To C#
PDF
Constructors and destructors
PPTX
C# in depth
Basic Concepts of OOPs (Object Oriented Programming in Java)
C# basics
C# Exceptions Handling
Exception Handling in C#
Oops concept in c++ unit 3 -topic 4
Inheritance and Polymorphism
Abstract class in java
Classes and Objects in C#
Object-oriented Programming-with C#
Delegates and events
C# Constructors
Inheritance C#
Object oriented programming
Introduction To C#
Constructors and destructors
C# in depth
Ad

Similar to Object Oriented Programming with C# (20)

DOCX
Question and answer Programming
PPT
Object and Classes in Java
PPTX
Java basics
PPTX
Abstraction in java [abstract classes and Interfaces
PPTX
python.pptx
PPTX
‫Object Oriented Programming_Lecture 3
PDF
PDF
Class in Java, Declaring a Class, Declaring a Member in a Class.pdf
PPTX
Unit3 part1-class
PPTX
Java chapter 5
PPTX
object oriented programming-classes and objects.pptx
PPTX
Ch-2ppt.pptx
PDF
Java 06
PPT
9781439035665 ppt ch10
PPTX
Lesson 13 object and class
PDF
Learn C# Programming - Classes & Inheritance
PPT
Inheritance in java.ppt
PDF
oops-123991513147-phpapp02.pdf
Question and answer Programming
Object and Classes in Java
Java basics
Abstraction in java [abstract classes and Interfaces
python.pptx
‫Object Oriented Programming_Lecture 3
Class in Java, Declaring a Class, Declaring a Member in a Class.pdf
Unit3 part1-class
Java chapter 5
object oriented programming-classes and objects.pptx
Ch-2ppt.pptx
Java 06
9781439035665 ppt ch10
Lesson 13 object and class
Learn C# Programming - Classes & Inheritance
Inheritance in java.ppt
oops-123991513147-phpapp02.pdf
Ad

Recently uploaded (20)

PDF
Modernizing your data center with Dell and AMD
PPTX
VMware vSphere Foundation How to Sell Presentation-Ver1.4-2-14-2024.pptx
PDF
Reach Out and Touch Someone: Haptics and Empathic Computing
PDF
Electronic commerce courselecture one. Pdf
PPTX
Detection-First SIEM: Rule Types, Dashboards, and Threat-Informed Strategy
DOCX
The AUB Centre for AI in Media Proposal.docx
PDF
The Rise and Fall of 3GPP – Time for a Sabbatical?
PDF
TokAI - TikTok AI Agent : The First AI Application That Analyzes 10,000+ Vira...
PPTX
20250228 LYD VKU AI Blended-Learning.pptx
PDF
Spectral efficient network and resource selection model in 5G networks
PDF
Mobile App Security Testing_ A Comprehensive Guide.pdf
PDF
KodekX | Application Modernization Development
PPTX
KOM of Painting work and Equipment Insulation REV00 update 25-dec.pptx
PDF
Encapsulation_ Review paper, used for researhc scholars
PDF
Peak of Data & AI Encore- AI for Metadata and Smarter Workflows
PDF
7 ChatGPT Prompts to Help You Define Your Ideal Customer Profile.pdf
PDF
Unlocking AI with Model Context Protocol (MCP)
PDF
Approach and Philosophy of On baking technology
PDF
Empathic Computing: Creating Shared Understanding
PDF
Diabetes mellitus diagnosis method based random forest with bat algorithm
Modernizing your data center with Dell and AMD
VMware vSphere Foundation How to Sell Presentation-Ver1.4-2-14-2024.pptx
Reach Out and Touch Someone: Haptics and Empathic Computing
Electronic commerce courselecture one. Pdf
Detection-First SIEM: Rule Types, Dashboards, and Threat-Informed Strategy
The AUB Centre for AI in Media Proposal.docx
The Rise and Fall of 3GPP – Time for a Sabbatical?
TokAI - TikTok AI Agent : The First AI Application That Analyzes 10,000+ Vira...
20250228 LYD VKU AI Blended-Learning.pptx
Spectral efficient network and resource selection model in 5G networks
Mobile App Security Testing_ A Comprehensive Guide.pdf
KodekX | Application Modernization Development
KOM of Painting work and Equipment Insulation REV00 update 25-dec.pptx
Encapsulation_ Review paper, used for researhc scholars
Peak of Data & AI Encore- AI for Metadata and Smarter Workflows
7 ChatGPT Prompts to Help You Define Your Ideal Customer Profile.pdf
Unlocking AI with Model Context Protocol (MCP)
Approach and Philosophy of On baking technology
Empathic Computing: Creating Shared Understanding
Diabetes mellitus diagnosis method based random forest with bat algorithm

Object Oriented Programming with C#

  • 2.  Overview  Classes and objects  Class members  Methods  Constructors  Destructors  Nested Classes  Access Modifiers and Access levels  Static (Shared) Classes and Members  Anonymous types  Inheritance  Overriding members • Sealed and Abstract classes  Interfaces 1
  • 3. o .NET Framework languages provide full support for Object-oriented programming o Encapsulation, Inheritance, Polymorphism o Encapsulation means that a group of related properties, methods, and other members are treated as a single unit or object. o Inheritance describes the ability to create new classes based on an existing class. o Polymorphism means that you can have multiple classes that can be used interchangeably, even though each class implements the same properties or methods in different ways. 2
  • 4. o Classes describe the type of objects… o …while objects are usable instances of classes. o Using the blueprint analogy, a class is a blueprint, and an object is a building made from that blueprint. To define a class and object: class SampleClass { } SampleClass SC1 = new SampleClass(); 3
  • 5. o A method is an action that an object can perform. To define a method of a class: class SampleClass { public int sampleMethod(string sampleParam) { // Insert code here } } 4
  • 6. o A class can have several implementations, or overloads, of the same method that differ in the number of parameters or parameter types. oTo overload a method: public int sampleMethod(string sampleParam) {}; public int sampleMethod(int sampleParam) {} Public int sampleMethod(int param1,int param2){}; o In most cases you declare a method within a class definition. However, both Visual Basic and C# also support extension methods that allow you to add methods to an existing class outside the actual 5
  • 7. o Constructors are class methods that are executed automatically when an object of a given type is created. (Run before any other code) o Constructors usually Initialize the data members. oSupport Overloading. To define a constructor for a class: public class SampleClass { int I; public SampleClass() { I = 1; } 6
  • 8. o Destructors are used to destruct instances of classes. o In the .NET Framework, the garbage collector automatically manages the allocation and release of memory for the managed objects in your application. o Destructors are still needed to clean up any unmanaged resources that your application creates. o There can be only one destructor for a class. 7
  • 9. o A class defined within another class is called nested. (Default - private) class Container { class Nested { // Add code here. } } o To create an instance of the nested class, use the name of the container class followed by the dot and then followed by the name of the nested class: Container.Nested nestedInstance = new 8
  • 10. o All classes and class members can specify what access level they provide to other classes by using access modifiers. The following access modifiers are available: C# Modifier Definition ___________________________________________________ Public The type or member can be accessed by any other code in the same assembly or another assembly that references it. Private The type or member can only be accessed by code in the same class. Protected The type or member can only be accessed by code in the same class or in a derived class. Internal The type or member can be accessed by any code in the 9
  • 11. o A static member of the class is a property, procedure, or field that is shared by all instances of a class. o To define a static (shared) member: static class SampleClass { public static string SampleString = "Sample String"; } 10
  • 12. o To access the static member, use the name of the class without creating an object of this class: Console.WriteLine(SampleClass.SampleStrin g); o Static (shared) classes in C# have static (shared) members only and cannot be instantiated. o Static (shared) members also cannot access nonstatic (non-shared) properties, fields or methods 11
  • 13. o Enable you to create objects without writing a class definition for the data type. (compiler generates a class) oThe class has no usable name and contains the properties you specify in declaring the object. o To create an instance of an anonymous type: // sampleObject is an instance of a simple anonymous type. var sampleObject = new { FirstProperty = "A", SecondProperty = "B" }; 12
  • 14. o Creates a new class that reuses, extends, and modifies the behavior that is defined in another class. Base Class – The class whose members are inherited Derived Class – The class that inherits those members. o In C#, all classes implicitely inherit from the Object class. * .NET Framework doesn’t support MULTIPLE INHERITANCE i.e. One derived class can not have more than One base class Syntax : 13
  • 15. o By default all classes can be inherited. o We can specify whether a class must not be used as a base class, or create a class that can be used as a base class only. To specify that a class cannot be used as a base class: public sealed class A { }  To specify that a class can be used as a base class only and cannot be instantiated : public abstract class B { } 14
  • 16. o By default, a derived class inherits all members from its base class. oyou want to change the behavior of the inherited member, you need to override it oWhat is Overriding ?? -> Overriding is defining a new implementation of a method, property or event in the derived class. 15
  • 17. Overriding modifiers.. C# Modifier Definition virtual (C# Reference) - Allows a class member to be overridden in a derived class. override (C# Reference) - Overrides a virtual (overridable) member defined in the base class. abstract (C# Reference) - Requires that a class member to be overridden in the derived class. 16
  • 18. o Interfaces, like classes, define a set of properties, methods, and events. o But unlike classes, interfaces do not provide implementation. oThey are implemented by classes, and defined as separate entities from classes. oAn interface represents a contract, in that a class that implements an interface must implement every aspect of that interface exactly 17
  • 19. o To define an interface: interface ISampleInterface { void doSomething(); } o To implement an interface in a class: class SampleClass : ISampleInterface { void ISampleInterface.doSomething() { // Method implementation. } } 18
  • 20. 19