SlideShare a Scribd company logo
1 of 20
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

What's hot

Object-oriented programming
Object-oriented programmingObject-oriented programming
Object-oriented programmingNeelesh Shukla
 
20. Object-Oriented Programming Fundamental Principles
20. Object-Oriented Programming Fundamental Principles20. Object-Oriented Programming Fundamental Principles
20. Object-Oriented Programming Fundamental PrinciplesIntro C# Book
 
Classes and Objects in C#
Classes and Objects in C#Classes and Objects in C#
Classes and Objects in C#Adeel Rasheed
 
C# Exceptions Handling
C# Exceptions Handling C# Exceptions Handling
C# Exceptions Handling sharqiyem
 
JavaScript - Chapter 5 - Operators
 JavaScript - Chapter 5 - Operators JavaScript - Chapter 5 - Operators
JavaScript - Chapter 5 - OperatorsWebStackAcademy
 
JavaScript - Chapter 12 - Document Object Model
  JavaScript - Chapter 12 - Document Object Model  JavaScript - Chapter 12 - Document Object Model
JavaScript - Chapter 12 - Document Object ModelWebStackAcademy
 
JavaScript - Chapter 8 - Objects
 JavaScript - Chapter 8 - Objects JavaScript - Chapter 8 - Objects
JavaScript - Chapter 8 - ObjectsWebStackAcademy
 
Inheritance in Object Oriented Programming
Inheritance in Object Oriented ProgrammingInheritance in Object Oriented Programming
Inheritance in Object Oriented ProgrammingAshita Agrawal
 
Access modifiers
Access modifiersAccess modifiers
Access modifiersJadavsejal
 
Java Basics
Java BasicsJava Basics
Java BasicsSunil OS
 
Pure virtual function and abstract class
Pure virtual function and abstract classPure virtual function and abstract class
Pure virtual function and abstract classAmit Trivedi
 
Javascript Prototype Visualized
Javascript Prototype VisualizedJavascript Prototype Visualized
Javascript Prototype Visualized军 沈
 
Javascript variables and datatypes
Javascript variables and datatypesJavascript variables and datatypes
Javascript variables and datatypesVarun C M
 

What's hot (20)

Object-oriented programming
Object-oriented programmingObject-oriented programming
Object-oriented programming
 
20. Object-Oriented Programming Fundamental Principles
20. Object-Oriented Programming Fundamental Principles20. Object-Oriented Programming Fundamental Principles
20. Object-Oriented Programming Fundamental Principles
 
Classes and Objects in C#
Classes and Objects in C#Classes and Objects in C#
Classes and Objects in C#
 
Unary operator overloading
Unary operator overloadingUnary operator overloading
Unary operator overloading
 
C# Exceptions Handling
C# Exceptions Handling C# Exceptions Handling
C# Exceptions Handling
 
Java I/o streams
Java I/o streamsJava I/o streams
Java I/o streams
 
C# classes objects
C#  classes objectsC#  classes objects
C# classes objects
 
C# in depth
C# in depthC# in depth
C# in depth
 
JavaScript - Chapter 5 - Operators
 JavaScript - Chapter 5 - Operators JavaScript - Chapter 5 - Operators
JavaScript - Chapter 5 - Operators
 
JavaScript - Chapter 12 - Document Object Model
  JavaScript - Chapter 12 - Document Object Model  JavaScript - Chapter 12 - Document Object Model
JavaScript - Chapter 12 - Document Object Model
 
C# String
C# StringC# String
C# String
 
JavaScript - Chapter 8 - Objects
 JavaScript - Chapter 8 - Objects JavaScript - Chapter 8 - Objects
JavaScript - Chapter 8 - Objects
 
Inheritance in Object Oriented Programming
Inheritance in Object Oriented ProgrammingInheritance in Object Oriented Programming
Inheritance in Object Oriented Programming
 
JAVA OOP
JAVA OOPJAVA OOP
JAVA OOP
 
Access modifiers
Access modifiersAccess modifiers
Access modifiers
 
Java Basics
Java BasicsJava Basics
Java Basics
 
Pure virtual function and abstract class
Pure virtual function and abstract classPure virtual function and abstract class
Pure virtual function and abstract class
 
Dot net assembly
Dot net assemblyDot net assembly
Dot net assembly
 
Javascript Prototype Visualized
Javascript Prototype VisualizedJavascript Prototype Visualized
Javascript Prototype Visualized
 
Javascript variables and datatypes
Javascript variables and datatypesJavascript variables and datatypes
Javascript variables and datatypes
 

Similar to Object Oriented Programming with C#

Similar to Object Oriented Programming with C# (20)

Question and answer Programming
Question and answer ProgrammingQuestion and answer Programming
Question and answer Programming
 
Object and Classes in Java
Object and Classes in JavaObject and Classes in Java
Object and Classes in Java
 
Oops
OopsOops
Oops
 
Java basics
Java basicsJava basics
Java basics
 
Abstraction in java [abstract classes and Interfaces
Abstraction in java [abstract classes and InterfacesAbstraction in java [abstract classes and Interfaces
Abstraction in java [abstract classes and Interfaces
 
C# program structure
C# program structureC# program structure
C# program structure
 
python.pptx
python.pptxpython.pptx
python.pptx
 
‫Object Oriented Programming_Lecture 3
‫Object Oriented Programming_Lecture 3‫Object Oriented Programming_Lecture 3
‫Object Oriented Programming_Lecture 3
 
Lecture 9
Lecture 9Lecture 9
Lecture 9
 
Unit3 part1-class
Unit3 part1-classUnit3 part1-class
Unit3 part1-class
 
Java chapter 5
Java chapter 5Java chapter 5
Java chapter 5
 
Ch-2ppt.pptx
Ch-2ppt.pptxCh-2ppt.pptx
Ch-2ppt.pptx
 
Java 06
Java 06Java 06
Java 06
 
9781439035665 ppt ch10
9781439035665 ppt ch109781439035665 ppt ch10
9781439035665 ppt ch10
 
Lesson 13 object and class
Lesson 13 object and classLesson 13 object and class
Lesson 13 object and class
 
Learn C# Programming - Classes & Inheritance
Learn C# Programming - Classes & InheritanceLearn C# Programming - Classes & Inheritance
Learn C# Programming - Classes & Inheritance
 
Inheritance in java.ppt
Inheritance in java.pptInheritance in java.ppt
Inheritance in java.ppt
 
oops-123991513147-phpapp02.pdf
oops-123991513147-phpapp02.pdfoops-123991513147-phpapp02.pdf
oops-123991513147-phpapp02.pdf
 
Opp concept in c++
Opp concept in c++Opp concept in c++
Opp concept in c++
 
Inner Classes & Multi Threading in JAVA
Inner Classes & Multi Threading in JAVAInner Classes & Multi Threading in JAVA
Inner Classes & Multi Threading in JAVA
 

Recently uploaded

Finology Group – Insurtech Innovation Award 2024
Finology Group – Insurtech Innovation Award 2024Finology Group – Insurtech Innovation Award 2024
Finology Group – Insurtech Innovation Award 2024The Digital Insurer
 
2024: Domino Containers - The Next Step. News from the Domino Container commu...
2024: Domino Containers - The Next Step. News from the Domino Container commu...2024: Domino Containers - The Next Step. News from the Domino Container commu...
2024: Domino Containers - The Next Step. News from the Domino Container commu...Martijn de Jong
 
08448380779 Call Girls In Diplomatic Enclave Women Seeking Men
08448380779 Call Girls In Diplomatic Enclave Women Seeking Men08448380779 Call Girls In Diplomatic Enclave Women Seeking Men
08448380779 Call Girls In Diplomatic Enclave Women Seeking MenDelhi Call girls
 
TrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
TrustArc Webinar - Stay Ahead of US State Data Privacy Law DevelopmentsTrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
TrustArc Webinar - Stay Ahead of US State Data Privacy Law DevelopmentsTrustArc
 
CNv6 Instructor Chapter 6 Quality of Service
CNv6 Instructor Chapter 6 Quality of ServiceCNv6 Instructor Chapter 6 Quality of Service
CNv6 Instructor Chapter 6 Quality of Servicegiselly40
 
Automating Google Workspace (GWS) & more with Apps Script
Automating Google Workspace (GWS) & more with Apps ScriptAutomating Google Workspace (GWS) & more with Apps Script
Automating Google Workspace (GWS) & more with Apps Scriptwesley chun
 
The 7 Things I Know About Cyber Security After 25 Years | April 2024
The 7 Things I Know About Cyber Security After 25 Years | April 2024The 7 Things I Know About Cyber Security After 25 Years | April 2024
The 7 Things I Know About Cyber Security After 25 Years | April 2024Rafal Los
 
EIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptx
EIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptxEIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptx
EIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptxEarley Information Science
 
Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...
Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...
Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...Igalia
 
IAC 2024 - IA Fast Track to Search Focused AI Solutions
IAC 2024 - IA Fast Track to Search Focused AI SolutionsIAC 2024 - IA Fast Track to Search Focused AI Solutions
IAC 2024 - IA Fast Track to Search Focused AI SolutionsEnterprise Knowledge
 
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
04-2024-HHUG-Sales-and-Marketing-Alignment.pptxHampshireHUG
 
Real Time Object Detection Using Open CV
Real Time Object Detection Using Open CVReal Time Object Detection Using Open CV
Real Time Object Detection Using Open CVKhem
 
Breaking the Kubernetes Kill Chain: Host Path Mount
Breaking the Kubernetes Kill Chain: Host Path MountBreaking the Kubernetes Kill Chain: Host Path Mount
Breaking the Kubernetes Kill Chain: Host Path MountPuma Security, LLC
 
Powerful Google developer tools for immediate impact! (2023-24 C)
Powerful Google developer tools for immediate impact! (2023-24 C)Powerful Google developer tools for immediate impact! (2023-24 C)
Powerful Google developer tools for immediate impact! (2023-24 C)wesley chun
 
The Codex of Business Writing Software for Real-World Solutions 2.pptx
The Codex of Business Writing Software for Real-World Solutions 2.pptxThe Codex of Business Writing Software for Real-World Solutions 2.pptx
The Codex of Business Writing Software for Real-World Solutions 2.pptxMalak Abu Hammad
 
[2024]Digital Global Overview Report 2024 Meltwater.pdf
[2024]Digital Global Overview Report 2024 Meltwater.pdf[2024]Digital Global Overview Report 2024 Meltwater.pdf
[2024]Digital Global Overview Report 2024 Meltwater.pdfhans926745
 
Factors to Consider When Choosing Accounts Payable Services Providers.pptx
Factors to Consider When Choosing Accounts Payable Services Providers.pptxFactors to Consider When Choosing Accounts Payable Services Providers.pptx
Factors to Consider When Choosing Accounts Payable Services Providers.pptxKatpro Technologies
 
How to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected WorkerHow to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected WorkerThousandEyes
 
Scaling API-first – The story of a global engineering organization
Scaling API-first – The story of a global engineering organizationScaling API-first – The story of a global engineering organization
Scaling API-first – The story of a global engineering organizationRadu Cotescu
 
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024The Digital Insurer
 

Recently uploaded (20)

Finology Group – Insurtech Innovation Award 2024
Finology Group – Insurtech Innovation Award 2024Finology Group – Insurtech Innovation Award 2024
Finology Group – Insurtech Innovation Award 2024
 
2024: Domino Containers - The Next Step. News from the Domino Container commu...
2024: Domino Containers - The Next Step. News from the Domino Container commu...2024: Domino Containers - The Next Step. News from the Domino Container commu...
2024: Domino Containers - The Next Step. News from the Domino Container commu...
 
08448380779 Call Girls In Diplomatic Enclave Women Seeking Men
08448380779 Call Girls In Diplomatic Enclave Women Seeking Men08448380779 Call Girls In Diplomatic Enclave Women Seeking Men
08448380779 Call Girls In Diplomatic Enclave Women Seeking Men
 
TrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
TrustArc Webinar - Stay Ahead of US State Data Privacy Law DevelopmentsTrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
TrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
 
CNv6 Instructor Chapter 6 Quality of Service
CNv6 Instructor Chapter 6 Quality of ServiceCNv6 Instructor Chapter 6 Quality of Service
CNv6 Instructor Chapter 6 Quality of Service
 
Automating Google Workspace (GWS) & more with Apps Script
Automating Google Workspace (GWS) & more with Apps ScriptAutomating Google Workspace (GWS) & more with Apps Script
Automating Google Workspace (GWS) & more with Apps Script
 
The 7 Things I Know About Cyber Security After 25 Years | April 2024
The 7 Things I Know About Cyber Security After 25 Years | April 2024The 7 Things I Know About Cyber Security After 25 Years | April 2024
The 7 Things I Know About Cyber Security After 25 Years | April 2024
 
EIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptx
EIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptxEIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptx
EIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptx
 
Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...
Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...
Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...
 
IAC 2024 - IA Fast Track to Search Focused AI Solutions
IAC 2024 - IA Fast Track to Search Focused AI SolutionsIAC 2024 - IA Fast Track to Search Focused AI Solutions
IAC 2024 - IA Fast Track to Search Focused AI Solutions
 
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
 
Real Time Object Detection Using Open CV
Real Time Object Detection Using Open CVReal Time Object Detection Using Open CV
Real Time Object Detection Using Open CV
 
Breaking the Kubernetes Kill Chain: Host Path Mount
Breaking the Kubernetes Kill Chain: Host Path MountBreaking the Kubernetes Kill Chain: Host Path Mount
Breaking the Kubernetes Kill Chain: Host Path Mount
 
Powerful Google developer tools for immediate impact! (2023-24 C)
Powerful Google developer tools for immediate impact! (2023-24 C)Powerful Google developer tools for immediate impact! (2023-24 C)
Powerful Google developer tools for immediate impact! (2023-24 C)
 
The Codex of Business Writing Software for Real-World Solutions 2.pptx
The Codex of Business Writing Software for Real-World Solutions 2.pptxThe Codex of Business Writing Software for Real-World Solutions 2.pptx
The Codex of Business Writing Software for Real-World Solutions 2.pptx
 
[2024]Digital Global Overview Report 2024 Meltwater.pdf
[2024]Digital Global Overview Report 2024 Meltwater.pdf[2024]Digital Global Overview Report 2024 Meltwater.pdf
[2024]Digital Global Overview Report 2024 Meltwater.pdf
 
Factors to Consider When Choosing Accounts Payable Services Providers.pptx
Factors to Consider When Choosing Accounts Payable Services Providers.pptxFactors to Consider When Choosing Accounts Payable Services Providers.pptx
Factors to Consider When Choosing Accounts Payable Services Providers.pptx
 
How to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected WorkerHow to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected Worker
 
Scaling API-first – The story of a global engineering organization
Scaling API-first – The story of a global engineering organizationScaling API-first – The story of a global engineering organization
Scaling API-first – The story of a global engineering organization
 
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
 

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