0% found this document useful (0 votes)
8 views63 pages

C# Notes

.NET Framework is a software development platform created by Microsoft, designed to overcome the limitations of the previous Component Object Model (COM) framework. It provides a cross-platform, open-source environment for building various applications using multiple programming languages and libraries. The framework includes key components such as the Common Language Runtime (CLR) and Base Class Libraries (BCL), enabling developers to create applications that can run on different operating systems.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
8 views63 pages

C# Notes

.NET Framework is a software development platform created by Microsoft, designed to overcome the limitations of the previous Component Object Model (COM) framework. It provides a cross-platform, open-source environment for building various applications using multiple programming languages and libraries. The framework includes key components such as the Common Language Runtime (CLR) and Base Class Libraries (BCL), enabling developers to create applications that can run on different operating systems.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 63

Introduc on to .

NET Framework
Here, I am going to give you an overview of the DOT NET Framework. This is important for
you as a dot net developer to know the history and evolu on of the DOT NET Framework.
Here, in this session, first, we will discuss what was there before the .NET Framework and
what problems we face in that, and how we overcome all those problems in the .NET
Framework. Before DOT NET Framework COM is there. So, let us first discuss what is COM
and what problems we face in COM.

What is COM?
COM stands for Component Object Model. The COM is one of the Microso Frameworks.
Using this Framework, we can develop Windows Applica ons (Desktop or Standalone
Applica ons for Windows OS) as well as Web Applica ons. In earlier COM, VB is the
programming language that is used to implement Windows applica ons and ASP is the
technology used to implement web applica ons.
What are the disadvantages of COM?
There are two major disadvantages of the COM Framework. They are as follows:
1. Incomplete Object-Oriented Programming means it will not support all the features
of OOPs.
2. Pla orm Dependent means COM applica ons can run on only Windows OS.

To overcome the above problems, Microsoft introduces .NET Framework.

What does .NET Represent?


NET stands for Network Enabled Technology (Internet). In .NET, dot (.) refers
to Object-Oriented, and NET refers to the internet. So, the complete .NET means
through Object-Oriented we can implement internet-based applications.
According to Microsoft, .NET is a Free, Cross-Platform, Open-Source developer
platform for building many different types of applications. With .NET, we can use
multiple languages (C#, VB, F#, etc.), Editors (Visual Studio, Visual Studio Code,
Visual Studio for Mac, OmniSharp, JetBrains Rider, etc), and Libraries to build for
Web, Mobile, Desktop, Games, IoT, and more.
Cross Platform: Whether you are working in C#, F#, or Visual Basic, your code will
run on any compatible operating system. You can build many types of apps with
.NET. Some are Cross-Platform, and some target a specific set of operating systems
and devices.
Libraries: To extend func onality, Microso and others maintain a healthy .NET package
ecosystem. NuGet is a package manager built specifically for .NET that contains over 100,000
packages.
What is a Framework?
A framework is a so ware. Or you can say a framework is a collec on of many small
technologies integrated together to develop applica ons that can be executed anywhere.
What does the .NET Framework Provide?
The DOT NET Framework provides two things as follows
1. BCL (Base Class Libraries)
2. CLR (Common Language Run me)
What is BCL?
Base Class Libraries (BCL) are designed by Microso . Without BCL we can’t write any code in
.NET. So, BCL is also known as the basic building block of .NET Programs. These are installed
into the machine when we installed the .NET framework. BCL contains pre-defined classes
and these classes are used for the purpose of applica on development.
What is CLR?
CLR stands for Common Language Run me and it is the core component under the
.NET framework which is responsible for conver ng the MSIL (Microso Intermediate
Language) code into na ve code. In our CLR session, we will discuss CLR in detail.

In the .NET framework, the code is compiled twice.


1. In the 1st compila on, the source code is compiled by the respec ve language
compiler and generates the intermediate code which is known as MSIL (Microso
Intermediate Language) or IL (Intermediate language code), or Managed Code.
2. In the 2nd compila on, MSIL is converted into Na ve code (na ve code means code
specific to the Opera ng system so that the code is executed by the Opera ng
System) and this is done by CLR.
Always 1st compilation is slow and 2nd compilation is fast.
What is JIT?
JIT stands for the Just-in-Time compiler. It is the component of CLR that is
responsible for converting MSIL code into Native Code. Native code is code that is
directly understandable by the operating system.
Different types of .NET Framework
The .NET framework is available in three different flavors
1. .NET Framework: .NET Framework is the original implementa on of .NET. It supports
running websites, services, desktop applica ons, and more on Windows OS Only.
2. .NET: .NET is a cross-pla orm implementa on for running websites, services, and
console applica ons on Windows, Linux, and macOS. .NET is open source on GitHub
and .NET was previously called .NET Core.
3. Xamarin/Mono: Xamarin/Mono is a .NET implementa on for running apps on all the
major mobile opera ng systems, including iOS and Android.
Note: .NET Framework is Pla orm-Dependent while .NET or .NET Core is Pla orm
Independent. Here, we are not talking about Web Applica ons. Web Applica ons are
independent of Opera ng Systems.
What is not .NET?
1. .NET is not an Opera ng system.
2. .NET is not a database
3. It is not an ERP applica on.
4. .NET is not a Tes ng Tool.
5. It is not a programming language.
What is Exactly .NET?
.NET is a framework tool that supports many programming languages and many
technologies. .NET support 60+ programming languages. Of 60+ programming
languages, 9 are designed by Microsoft and the remaining are designed by non-
Microsoft. Microsoft-designed programming languages are as follows:
1. VB.NET
2. C#.NET
3. VC++.NET
4. J#.NET
5. F#.NET
6. Jscript.NET
7. WindowsPowerShell
8. Iron phyton
9. Iron Ruby
Technologies supported by the .NET framework are as follows
1. ASP.NET (Ac ve Server Pages.NET) – MVC, Web API, Core MVC, Core Web API, Core
Blazor, etc.
2. ADO.NET (Ac ve Data Object.NET)
3. WCF (Windows Communica on Founda on)
4. WPF (Windows Presenta on Founda on)
5. WWF (Windows Workflow Founda on)
6. AJAX (Asynchronous JavaScript and XML)
7. LINQ (Language Integrated Query)
8. En ty Framework
What are a Language and its need?
1. Language acts as the mediator between the programmer and the system.
2. It offers some rules and regula ons for wri ng the program.
3. The language also offers some libraries which are required for wri ng the program.
What are Technology and its Needs?
Technology is always designed for a particular purpose. For example, the development of
web-related applications in .NET using the technology ASP.NET. But the technology does
not offer any specific rules for writing the programs. That’s why technology can’t be
implemented individually. VB.NET, C#.NET, and F#.NET are programming languages.
Using any of these languages we can implement windows/desktop applications
individually. Every language is having its own compiler
.NET Framework Architecture and Components
In this ar cle, I am going to discuss .NET Framework Architecture and Components in detail.
Please read our previous ar cle where we give a brief introduc on to .NET Framework. At
the end of this ar cle, you will understand what is .NET Framework, .Net Framework
Architecture, .NET Components, and .NET Framework Design Principle are.
.Net Framework Architecture
The two major components of the .NET Framework are the Common Language Run me and
the .NET Framework Class Library.
1. CLR: The Common Language Run me (CLR) is the execu on engine that handles
running applica ons. It provides services like thread management, garbage
collec on, type safety, excep on handling, and more.
2. BCL: The Base Class Library provides a set of APIs and types for common
func onality. It provides types for strings, dates, numbers, etc. The Class Library
includes APIs for reading and wri ng files, connec ng to databases, drawing, and
more.
The .NET applica ons are wri en in C#, F#, or VB programming languages. The Source Code
is compiled into an intermediate language code called IL or MSIL or CIL (Common
Intermediate Language). And the Compiled code is stored in assemblies with .DLL or .EXE file
extension.
When an applica on runs, the CLR takes the Assembly (IL Code or MSIL Code, or CIL) and
uses the Just-in-Time compiler (JIT) to convert the MSIL or IL code into machine code that
can execute on the specific architecture of the computer it is running on.
What is .NET Framework used for?
.NET Framework is used to create and run so ware applica ons. .NET apps can run on many
opera ng systems, using different implementa ons of .NET (.NET Framework, .NET Core or
.NET, and Xamarin/Mono). The .NET Framework is used for running .NET apps on Windows,
The .NET Core or .NET is used for running .NET Apps on Windows, Linux, and macOS.
And Xamarin/Mono is used for running apps on all the major mobile opera ng systems,
including iOS and Android.
Who uses .NET Framework?
So ware developers and the users of their applica ons both use .NET Framework:
1. Users of applica ons built with the .NET Framework need to have .NET Framework
installed. In most cases, .NET Framework is already installed with Windows. If
needed, you can download .NET Framework.
2. So ware developers use .NET Framework to build many different types of
applica ons such as websites, services, desktop apps, and more with Visual Studio.
Visual Studio is an integrated development environment (IDE) that provides
development produc vity tools and debugging capabili es.
Why do I need .NET Framework?
You need .NET Framework installed on your machine to run applica ons on Windows that
were created using .NET Framework. It is already included in many versions of Windows. You
only need to download and install .NET Framework if prompted to do so.
How does .NET Framework work?
.NET Framework applica ons are developed using C#, F#, or VB Programming Language and
compiled into Common Intermediate Language (CIL) or MSIL (Microso Intermediate
Language). The Common Language Run me (CLR) runs .NET applica ons on a given
machine, conver ng the CIL code or MSIL code to machine code that the corresponding
machine can execute.
What are the main Components/Features of the .NET Framework?
The two major components of the .NET Framework are the Common Language Run me
(CLR) and the .NET Framework Class Library. The CLR is the execu on engine that handles
running applica ons. The Base Class Library provides a set of APIs and types for common
func onality.
What is the difference between .NET and .NET Framework?
.NET and .NET Framework share many of the same components and you can share code
across the two. Some of the key differences between them are as follows:
1. .NET is Cross-Pla orm and runs on Linux, macOS, and Windows OS. .NET
Framework only runs on Windows OS.
2. .NET is Open-Source and accepts contribu ons from the community. The .NET
Framework source code is available but does not take direct contribu ons.
3. .NET Framework is included in Windows and automa cally updated machine-wide
by Windows Update. .NET is shipped independently.
Applica ons Developed using .NET Framework
The types of applica ons that can be built in the .Net framework are classified broadly into
the following categories.
WinForms – This is used for developing Forms-based applica ons, which would run on an
end-user machine. Notepad is an example of a client-based applica on. Windows Forms is a
smart client technology for the .NET Framework, a set of managed libraries that simplify
common applica on tasks such as reading and wri ng to the file system.
ASP.NET – This is used for developing web-based applica ons, which are made to run on any
browser such as Edge, Chrome or Firefox. ASP.NET is a web framework designed and
developed by Microso . It is used to develop websites, web applica ons, and web services.
It provides a fantas c integra on of HTML, CSS, and JavaScript. It was first released in
January 2002.
1. The Web applica on would be processed on a server, which would have Internet
Informa on Services Installed.
2. Internet Informa on Services or IIS is a Microso component that is used to execute
an ASP.NET applica on.
3. The result of the execu on is then sent to the client machines, and the output is
shown in the browser.
ADO.NET: This technology is used to develop applications to interact with databases
such as Oracle or Microsoft SQL Server. ADO.NET is a module of the .Net Framework,
which is used to establish a connection between applications and data sources. Data
sources can be such as SQL Server and XML. ADO .NET consists of classes that can
be used to connect, retrieve, insert, and delete data.
WCF (Windows Communication Foundation): It is a framework for building service-
oriented applications. Using WCF, you can send data as asynchronous messages
from one service endpoint to another.

LINQ (Language Integrated Query): It is a query language, introduced in .NET 3.5


framework. It is used to make the query for data sources with C# or Visual Basics
programming languages.
Entity Framework: It is an ORM-based open-source framework that is used to work
with a database using .NET objects. It eliminates a lot of developers’ effort to handle
the database. It is Microsoft’s recommended technology to deal with the database.

Parallel LINQ: Parallel LINQ or PLINQ is a parallel implementation of LINQ to objects.


It combines the simplicity and readability of LINQ and provides the power of parallel
programming. It can improve and provide fast speed to execute the LINQ query by
using all available computer capabilities.
Introduc on to C# Programming Language

Why did C#.NET come to the market?


C#.NET Programming Language is mainly designed to overcome the disadvantages of C and C++ and
to develop internet applica ons (web applica ons) by achieving pla orm independence.
Why C# is so much popular nowadays?
C#.NET is so much popular nowadays because of the following reasons.
C# is Simple and Familiar:
C# is simple because C# simplifies the programmer’s job by avoiding certain features of C and C++. C#
avoids explicit memory management. Memory management in C# is automa c. It is done by CLR. So,
there is no chance of memory leakage. There is a pointer concept in C# but it is restricted. C# is very
familiar to programmers because it is modeled on the C and C++ languages. C# uses many features of
C and C++; therefore, C# codes look like C and C++ codes. We can say C# is the simplified version of C
and C++.
C# is Portable:
Portability allows the programmer to write the same code for different machines (opera ng
systems). C# provides portability in two ways
1. Source Code Portability
2. IL Code Portability (DLL and EXE)
In C#, we can write the source code in Mul ple OS and get the output. Even though we can IL Code
Portability is also available. So, once you have the DLL and EXE, then you can use those DLLs and EXEs
on different OS and you can get the output as expected. Whereas in the case of C and C++ there is
only source code portability.
C# is Architecturally Neutral:
The behavior of C# programs doesn’t change when we move from one system to another which
means it will provide the same output in every machine because in C# memory layout decisions are
not made at compile me, it is made at run me by CLR. It is because of CTS (Common Type System)
and in our coming session we will discuss it in detail. But in the case of C and C++ programs, the
behavior changes when we move the program from one system to another and the reason for this is
the size of data types varies from OS to OS.
C# is Secured:
C# is secured because the C# programs are executed in a secured environment called CLR. CLR will
provide security to the C# programs.
C# is Robust:
Robust means Strong. C# is a strong type-checking language having strict type-checking during both
compila on me and execu on me which allows us to develop error-free applica ons and
programs.
C# is Distributed:
Using C# Programming Language, we can develop distributed applica ons whose resources are
shared by more than one client. An applica on or program whose resources are shared by more than
one client is called a distributed applica on. As C# is used for developing such applica ons for
example WCF Applica ons, Res ul Services, etc. So, C# is a distributed programming language.
C# is Mul threaded:
A process is divided into several small parts known as threads or lightweight processes. Sending
mul ple threads to the processor for processing is known as mul threading. Mul -threading means
handling mul ple tasks simultaneously. For example, we can listen to music while scrolling a page
and at the same me we can download an applica on from the internet on a computer.
If mul ple threads are used to execute your applica on code, then it is called Mul threading.
Mul threading is a mechanism to implement Concurrent Programming where mul ple threads
execute the code simultaneously. And using C# Programming language we can develop mul thread
applica ons. So, C# supports mul threaded programming.
C# is Dynamic:
C# 4.0 introduced a new type called dynamic that avoids compile- me type checking. A dynamic type
escapes type checking at compile- me; instead, it resolves the type at run me. So, if you don’t
know the type of data that you want to store, then you can use dynamic as the data type, and at
run me based on value or data, the type will automa cally resolve. So, C# is dynamic.
C# is Compiled and Interpreted:
We know a programming language is either compiled or interpreted. But C# combines both
approaches. That’s why C# is called a two-stage system.
First C# compiler CSC translates source code into an intermediate language code known as MSIL
(Microso Intermediate Language) or CIL (Common Intermediate Language) code. But these MSIL or
CIL or IL codes are not machine instruc ons. So, in the second stage, these MSIL or CIL, or IL codes
are interpreted by the C# interpreter (One Component of CLR called JIT- Just in Time Compiler). As a
result, machine instruc ons will be generated which are directly executed by the machine. Hence,
we are saying C# is both interpreted and compiled language.
C# is Object-Oriented:
Except for the primi ve data types, all elements in C# are objects. Object-oriented is not a
programming language, it is a programming technique or concept, or principle which defines a set of
rules and regula ons for organizing the data and instruc ons. The concepts provided by oops are as
follows
1. Encapsula on
2. Abstrac on
3. Polymorphism
4. Inheritance
The Programming Language which supports these four features is known as an Object-Oriented
Programming Language. And C# Programming Language supports these four features, so C# is
Object-Oriented.
C# is Pla orm Independent:
Many Programming Languages are compa ble with only one pla orm. Now, with the introduc on
of .NET Core or .NET, C# was specifically designed to be pla orm-independent. C# applica ons with
.NET Core or .NET can be run on Windows, Linux, and Mac opera ng systems. So, we can say C# is
Pla orm Independent with .NET Core or .NET.
Automa c Memory Management:
The .NET Framework automa cally manages memory alloca on and de-alloca on for objects. When
a dot net applica on runs, lots of objects are created. At a given point in me, it is possible that
some of those objects are not used by the applica on. Garbage Collector in .NET Framework is
nothing but a Small Rou ne or you can say it is a Background Process that runs periodically and try to
iden fy what objects are not being used currently by the applica on and de-allocates the memory of
those objects. So, as a developer, we need not worry about how memory alloca on and dealloca on
are done in .NET Applica ons.
Excep on Handling:
C# Provides Strong Excep on Handling Features which can be used to stop the Abnormal Termina on
of the program and you can also provide user-understandable messages when an excep on is raised.
C# Defini on based on the above features:
C# is just a Simple, Secure, Robust, Portable, Pla orm-Independent, Architectural Neutral,
Mul threaded, Automa c Memory Management, Object-Oriented Programming Language with a
strong type Excep on Handling mechanism for developing different kinds of applica ons such as
Web, Windows Form, Console, Web Services, Mobile Apps, etc. which can be run on different
Opera ng Systems such as Windows, Linus, and Mac.
Features of C#
Although C# constructs closely follow tradi onal high-level languages, C and C++ and being an
object-oriented programming languages. It has a strong resemblance with Java, it has numerous
strong programming features that make it endearing to a number of programmers worldwide.
Following is the list of a few important features of C# −
1. Simple: C# is a simple language in the sense that it provides a structured approach (to break
the problem into parts), a rich set of library func ons, data types, etc.
2. Modern Programming Language: C# programming is based upon the current trend and it is
very powerful and simple for building scalable, interoperable, and robust applica ons.
3. Object-Oriented: C# is an object-oriented programming language. OOPs makes development
and maintenance easier whereas in Procedure-oriented programming language it is not easy
to manage if code grows as the project size grows.
4. Type-Safe: C# type safe code can only access the memory loca on that it has permission to
execute. Therefore, it improves the security of the program.
5. Interoperability: The interoperability process enables the C# programs to do almost anything
that a na ve C++ applica on can do.
6. Scalable and Updateable: C# is an automa c scalable and updateable programming
language. For upda ng our applica on, we delete the old files and update them with new
ones.
7. Component Oriented: C# is a component-oriented programming language. It is the
predominant so ware development methodology used to develop more robust and highly
scalable applica ons.
8. Structured Programming Language: C# is a structured programming language in the sense
that we can break the program into parts using func ons. So, it is easy to understand and
modify.
9. Rich Library: C# provides a lot of inbuilt func ons that make development fast.
10. Fast Speed: The compila on and execu on me of the C# language is fast.
Types of Applica ons Developed using C#:
With the help of the C# programming language, we can develop different types of secured and
robust applica ons:
1. Window applica ons
2. Web applica ons
3. Distributed applica ons
4. Web service applica ons
5. Database applica ons
6. Mobile Applica ons, etc

Object-Oriented Programming (OOPs) in C#


1. Introduc on
 OOP is a programming paradigm that focuses on represen ng problems as collec ons of
interac ng objects.
 It provides a structured approach to so ware development, emphasizing reusability,
maintainability, and flexibility.
 Key principles of OOP:
o Encapsula on: Bundling data (a ributes) and methods (behaviors) that operate on
that data within a single unit (class).
o Inheritance: Crea ng new classes (derived/child classes) from exis ng ones
(base/parent classes), inheri ng their proper es and methods.
o Polymorphism: The ability of objects to take on many forms. It allows objects of
different classes to be treated as objects of a common 1 type.
o Abstrac on: Hiding complex implementa on details and exposing only the essen al
features of an object.
2. Problems with Func onal Programming
 Reusability: Difficult to reuse code across different parts of the applica on.
 Extensibility: Difficult to add new features or modify exis ng func onality without significant
code changes.
 Maintainability: Code becomes complex and difficult to understand and modify as the
applica on grows.
 Simplicity: Lack of modularity can lead to sca ered code and increased complexity.
3. How OOP Addresses Func onal Programming Problems
 Reusability: Achieved through inheritance and class-based design.
 Extensibility: Inheritance and polymorphism enable easy extension of exis ng classes.
 Maintainability: Encapsula on and modularity improve code organiza on and readability.
 Simplicity: Abstrac on hides complexity, making the code easier to understand and use.
4. Key Concepts
 Class: A blueprint or template for crea ng objects. It defines the proper es (data members)
and methods (member func ons) that objects of that class will have.
 Object: An instance of a class. It represents a real-world en ty with specific values for its
proper es.
5. OOP Principles in Detail
 Encapsula on
o Defini on: Bundling data and methods that operate on that data within a single unit
(class).
o Benefits:
 Protects data integrity by controlling access to internal state.
 Improves code maintainability and reduces the risk of unintended side
effects.
o Implementa on:
 Use access modifiers (public, private, protected, internal) to control the
accessibility of class members.
 Inheritance
o Defini on: Crea ng new classes (derived classes) from exis ng classes (base classes).
o Benefits:
 Promotes code reusability.
 Enables crea on of hierarchical rela onships between classes.
 Facilitates code extensibility.
o Types:
 Single Inheritance: A class inherits from only one base class.
 Mul ple Inheritance: A class inherits from mul ple base classes (not directly
supported in C#, but can be achieved using interfaces).
o Implementa on:
 Use the : (colon) operator to inherit from a base class.
 Polymorphism
o Defini on: The ability of objects to take on many forms.
o Types:
 Sta c Polymorphism (Compile- me Polymorphism):
 Achieved through method overloading (same method name,
different parameters).
 Dynamic Polymorphism (Run me Polymorphism):
 Achieved through method overriding (same method signature in
derived class).
o Benefits:
 Increases code flexibility and adaptability.
 Enables wri ng more generic and reusable code.
 Abstrac on
o Defini on: Hiding complex implementa on details and exposing only the essen al
features of an object.
o Benefits:
 Simplifies the interface for users of the class.
 Reduces complexity and improves code readability.
o Implementa on:
 Using abstract classes and interfaces.
6. Advantages of OOP in C#
 Modularity: Encapsula on promotes modularity, making code easier to manage and
maintain.
 Reusability: Inheritance and polymorphism enable code reuse, reducing development me
and effort.
 Flexibility: Polymorphism allows for more flexible and adaptable code.
 Maintainability: Encapsula on and modularity improve code maintainability.
 Scalability: OOP principles help create scalable and extensible so ware systems.
 Readability: OOP promotes code that is more human-readable and self-explanatory.
7. Real-world Applica ons of OOP
 Game development: Crea ng game objects (characters, items, etc.)
 Graphical User Interfaces (GUIs): Designing interac ve user interfaces with classes and
objects.
 Web development: Building web applica ons with object-oriented frameworks like ASP.NET
MVC.
 Database applica ons: Crea ng object-rela onal databases (ORMs) to interact with
databases.
 Business applica ons: Modeling real-world business en es (customers, products, orders,
etc.)
Classes and Objects in C#
1. Introduc on
 Class: A blueprint or template that defines the proper es (data members) and methods
(member func ons) that objects of that class will have.
 Object: An instance of a class. It represents a real-world en ty with specific values for its
proper es.
2. Class and Objects from Layman's Perspec ve
 Class: A classifica on or grouping of objects with similar characteris cs. For example, the
class "Car" encompasses all cars, regardless of their specific make or model.
 Object: An individual instance of a class. A specific car (e.g., a red Toyota Camry) is an object
of the class "Car."
3. Class and Objects from Programming Perspec ve
 Class: A user-defined data type that encapsulates data (proper es) and methods (func ons)
that operate on that data.
 Object: An instance of a class. It has its own set of property values and can perform the
ac ons defined by the class's methods.
4. Crea ng a Class and Object in C#
 Class Defini on:
C#
public class MyClass
{
// Proper es
public int Id { get; set; }
public string Name { get; set; }

// Methods
public void Display()
{
Console.WriteLine($"Id: {Id}, Name: {Name}");
}
}
 Object Crea on:
C#
MyClass obj1 = new MyClass();
obj1.Id = 1;
obj1.Name = "John Doe";
obj1.Display();

MyClass obj2 = new MyClass();


obj2.Id = 2;
obj2.Name = "Jane Doe";
obj2.Display();
5. Difference between Class and Objects
Feature Class Object
Defini on Blueprint/Template Instance of a class
Representa on Abstract Concrete
Proper es Defines proper es Holds specific values for proper es
Methods Defines methods Can execute methods
Rela onship One class can create many objects An object belongs to only one class
Export to Sheets
6. Types of Classes in C#
 Abstract Class:
o Cannot be instan ated directly.
o Can contain abstract methods (methods without implementa on).
o Used as a base class for other classes.
o Example:
C#
public abstract class Shape
{
public abstract double CalculateArea();
}
 Concrete Class:
o Can be instan ated directly.
o Contains concrete implementa ons for all methods.
o Example:
C#
public class Circle : Shape
{
public double Radius { get; set; }

public override double CalculateArea()


{
return Math.PI * Radius * Radius;
}
}
 Sealed Class:
o Cannot be inherited from.
o Used to prevent unintended inheritance.
o Example:
C#
public sealed class MySealedClass
{
// ...
}
 Par al Class:
o A class that is divided into mul ple parts across different files.
o Useful for organizing large classes into smaller, more manageable units.
o Example:
C#
// Part 1
public par al class MyClass
{
// ...
}

// Part 2
public par al class MyClass
{
// ...
}
 Sta c Class:
o Contains only sta c members (methods and fields).
o Cannot be instan ated.
o Used for u lity methods and constants.
o Example:
C#
public sta c class MathU ls
{
public sta c double PI = 3.14159;
public sta c int Add(int a, int b)
{
return a + b;
}
}
7. Key Points
 Classes and objects are fundamental concepts in object-oriented programming.
 Understanding the rela onship between classes and objects is essen al for effec ve C#
programming.
 Different types of classes offer varying levels of flexibility and control.
Constructors in C#
1. Introduc on
 A constructor is a special method within a class that is automa cally called when an object of
that class is created.
 Its primary purpose is to ini alize the object's state (proper es/fields).
2. Key Characteris cs
 Name: The constructor's name must be the same as the class name.
 Return Type: Constructors do not have a return type, not even void.
 Purpose:
o Ini alize the object's proper es.
o Allocate necessary resources.
 Types:
o Default Constructor (Parameterless):
 A constructor that takes no arguments.
 The compiler provides a default constructor if no other constructors are
defined in the class.
o Parameterized Constructor:
 A constructor that accepts arguments.
 Allows for flexible object ini aliza on with different values.
3. Implicit Constructor
 The compiler automa cally generates a default constructor (parameterless) if no other
constructors are defined explicitly in the class.
 This implicit constructor ini alizes the fields of the class with their default values (e.g., 0 for
integers, null for strings).
4. Explicit Constructor
 A constructor that is defined explicitly by the programmer.
 Can be parameterless or parameterized.
 Allows for custom ini aliza on logic.
5. Example: Implicit Constructor
C#
public class Employee
{
public int Id { get; set; }
public string Name { get; set; }
}

// The compiler generates an implicit default constructor:


// public Employee()
// {
// Id = 0;
// Name = null;
// }
6. Example: Explicit Parameterless Constructor
C#
public class Employee
{
public int Id { get; set; }
public string Name { get; set; }

public Employee()
{
Console.WriteLine("Default Constructor Called");
}
}
7. Example: Explicit Parameterized Constructor
C#
public class Employee
{
public int Id { get; set; }
public string Name { get; set; }

public Employee(int id, string name)


{
Id = id;
Name = name;
}
}
8. Constructor Overloading
 A class can have mul ple constructors with different signatures (different number or types of
parameters).
 Example:
C#
public class Employee
{
public Employee() { } // Default constructor
public Employee(int id) { Id = id; }
public Employee(int id, string name)
{
Id = id;
Name = name;
}
}
9. Important Points
 Constructors cannot have a return type, not even void.
 Constructors cannot be inherited.
 A class can have only one constructor with no parameters.
 11. Code Example
 C#
 using System;

 public class Program
 {
 public static void Main(string[] args)
 {
 // Create objects using different constructors
 Employee employee1 = new Employee();
 Employee employee2 = new Employee(1);
 Employee employee3 = new Employee(2, "John Doe");

 // Access and display employee information
 Console.WriteLine(employee1.Id); // Output: 0
 Console.WriteLine(employee2.Id); // Output: 1
 Console.WriteLine(employee3.Id); // Output: 2
 Console.WriteLine(employee3.Name); // Output: John Doe

 Console.ReadLine();
 }
 }

 public class Employee
 {
 public int Id { get; set; }
 public string Name { get; set; }

 public Employee() { }
 public Employee(int id) { Id = id; }
 public Employee(int id, string name)
 {
 Id = id;
 Name = name;
 }
 }
 12. Conclusion
 Constructors are essential for object initialization in C#. They provide a mechanism to
control how objects are created and ensure that they are properly initialized with
appropriate values. By understanding and effectively using constructors, you can
write more robust and maintainable C# code.

Constructors in C#
1. Introduction
 A constructor is a special method within a class that is automatically called whenever
an object of that class is created.
 Its primary purpose is to initialize the object's state (properties/fields) with appropriate
values.
2. Key Characteristics
 Name: The constructor's name must be the same as the class name.
 Return Type: Constructors do not have a return type, not even void.
 Purpose:
o Initialize the object's properties.
o Allocate necessary resources.
o Perform custom logic at object creation.
 Types:
o Default Constructor (Parameterless):
 A constructor that takes no arguments.
 The compiler provides a default constructor if no other constructors are
defined in the class. It initializes fields with their default values (e.g., 0
for integers, null for strings).
o Parameterized Constructor:
 A constructor that accepts arguments.
 Allows for flexible object initialization with different values.
3. Implicit Constructor
 The compiler automatically generates a default constructor (parameterless) if no other
constructors are defined explicitly in the class.
4. Explicit Constructor
 A constructor that is defined explicitly by the programmer.
 Can be parameterless or parameterized.
 Allows for custom initialization logic.
5. Example: Implicit Constructor
C#
public class Employee
{
public int Id { get; set; }
public string Name { get; set; }
}
// The compiler generates an implicit default constructor:
// public Employee()
// {
// Id = 0;
// Name = null;
// }
6. Example: Explicit Parameterless Constructor
C#
public class Employee
{
public int Id { get; set; }
public string Name { get; set; }

public Employee()
{
Console.WriteLine("Default Constructor Called");
}
}
7. Example: Explicit Parameterized Constructor
C#
public class Employee
{
public int Id { get; set; }
public string Name { get; set; }

public Employee(int id, string name)


{
Id = id;
Name = name;
}
}
8. Constructor Overloading
 A class can have multiple constructors with different signatures (different number or
types of parameters). This allows for flexibility in object creation.
 Example:
C#
public class Employee
{
public Employee() { } // Default constructor
public Employee(int id) { Id = id; }
public Employee(int id, string name)
{
Id = id;
Name = name;
}
}
9. Important Points
 Constructors cannot have a return type, not even void.
 Constructors cannot be inherited.
 A class can only have one constructor with no parameters (default constructor).
10. Diagram: Class with Constructors

Opens in a new window


stackoverflow.com
class diagram showing a class with a default constructor and a parameterized
constructor
11. Code Example (Combining Different Concepts)
C#
using System;

public class Program


{
public static void Main(string[] args)
{
// Create objects using different constructors
Employee employee1 = new Employee(); // Default constructor
Employee employee2 = new Employee(1); // Parameterized constructor with one
argument
Employee employee3 = new Employee(2, "John Doe"); // Parameterized
constructor with two arguments

// Access and display employee information


Console.WriteLine(employee1.Id); // Output: 0
Console.WriteLine(employee2.Id); // Output: 1
Console.WriteLine(employee3.Id); // Output: 2
Console.WriteLine(employee3.Name); // Output: John Doe

Console.ReadLine();
}
}

public class Employee


{
public int Id { get; set; }
public string Name { get; set; }

public Employee() // Default constructor


{
Console.WriteLine("Default Constructor Called");
}

public Employee(int id) // Parameterized constructor with one argument


{
Id = id;
Console.WriteLine("Parameterized Constructor (1 argument) Called");
}

public Employee(int id,


Static vs. Non-Static Constructors in C#
1. Introduction
 Constructors: Special methods within a class that are automatically called when an
object of that class is created. Their primary role is to initialize the object's state
(properties/fields).
 Static Constructor: A special type of constructor that is declared with the static
keyword.
 Non-Static Constructor (Instance Constructor): A constructor that is not declared
with the static keyword.
2. Key Differences
Non-Static Constructor
Feature Static Constructor
(Instance Constructor)
Called automatically once Called explicitly when an
Execution when the class is first object of the class is
loaded or accessed. created.
Initialize static members Initialize instance
Purpose (fields, properties) of the members (fields,
class. properties) of each object.
Cannot have access
Access Can have access
modifiers (public, private,
Modifiers modifiers.
protected).
Can have parameters
Parameters Cannot have parameters. (parameterized
constructor).
Overloading Cannot be overloaded. Can be overloaded.
Call Executed only once per Executed once for each
Frequency application execution. object created.

Export to Sheets
3. Examples
 Static Constructor:
C#
public class MyClass
{
public static int StaticCount;

static MyClass()
{
Console.WriteLine("Static Constructor Called");
StaticCount = 0;
}

// ... other members ...


}
 Non-Static Constructor:
C#
public class MyClass
{
public int InstanceCount;

public MyClass()
{
Console.WriteLine("Non-Static Constructor Called");
InstanceCount = 1;
}

// ... other members ...


}
4. When is the Static Constructor Executed?
 First Access to the Class: The static constructor is executed the first time any
member of the class is accessed (static or instance member).
 Creating an Instance: If an instance of the class is created, the static constructor is
executed before the instance constructor.
5. Important Points
 Static constructors cannot be called explicitly.
 Static constructors can only access static members of the class.
 If a class contains static fields, the compiler automatically generates a static
constructor to initialize them.
 Non-static constructors are called when an object of the class is created using the new
operator.
6. Code Example
C#
using System;

public class Program


{
public static void Main(string[] args)
{
Console.WriteLine("Main Method Started");

// Accessing a static member will trigger the static constructor


Console.WriteLine(MyClass.StaticCount);

// Creating an object will trigger the static constructor (if not already triggered)
// and then the instance constructor
MyClass obj1 = new MyClass();

MyClass obj2 = new MyClass();

Console.WriteLine("Main Method Ended");


Console.ReadLine();
}
}

public class MyClass


{
public static int StaticCount;

static MyClass()
{
Console.WriteLine("Static Constructor Called");
StaticCount = 0;
}

public int InstanceCount;

public MyClass()
{
Console.WriteLine("Non-Static Constructor Called");
InstanceCount = 1;
}
}
7. Diagram
Openindownaveen-metta.medium.com
class diagram illustrating the execution order of static and nonstatic constructors
8. Conclusion
 Static and non-static constructors play crucial roles in object initialization in C#.
 Understanding their differences and how they are executed is essential for writing
efficient and well-structured C# code.
 By effectively using static and non-static constructors, you can manage the
initialization of class members and control the execution flow within your classes.

Private Constructors in C#
1. Introduction
 A private constructor is a constructor declared with the private access modifier.
 It restricts object creation from outside the class.
2. Key Characteristics
 Access: Can only be accessed within the class itself.
 Object Creation:
o Cannot be used to create objects directly from outside the class.
o Can be used to create objects within the same class.
 Inheritance:
o If a class has only a private constructor, it cannot be inherited by other classes
(except nested classes within the same class).
3. Examples
 Private Constructor Only:
C#
public class MyClass
{
private MyClass()
{
Console.WriteLine("Private Constructor Called");
}

public void SomeMethod()


{
// ...
}
}
// In another class (outside MyClass)
MyClass obj = new MyClass(); // Compile-time error: 'MyClass.MyClass()' is
inaccessible
 Private and Public Constructors:
C#
public class MyClass
{
private MyClass()
{
Console.WriteLine("Private Constructor Called");
}

public MyClass(string message)


{
Console.WriteLine("Public Constructor Called");
}

public void SomeMethod()


{
// ...
}
}

// In another class (outside MyClass)


MyClass obj = new MyClass("Hello"); // Creates an object using the public
constructor
 Creating Objects within the Class:
C#
public class MyClass
{
private MyClass()
{
Console.WriteLine("Private Constructor Called");
}

public static MyClass CreateInstance()


{
return new MyClass();
}

public void SomeMethod()


{
// ...
}
}
// In another class (outside MyClass)
MyClass obj = MyClass.CreateInstance(); // Creates an object using a static factory
method
4. Use Cases
 Singleton Pattern: Ensures only one instance of a class exists throughout the
application.
 Utility Classes: For classes that provide static methods and do not require object
instantiation.
 Nested Classes: For inner classes that are closely related to the outer class and should
not be instantiated independently.
5. Important Notes
 A class with only a private constructor can still be inherited by nested classes within
the same class.
 A private constructor does not restrict inheritance if the class also has a public
constructor.
 If a class has only a private constructor and no other constructors, it cannot be
inherited by classes outside its scope.
6. Singleton Pattern Implementation
C#
public sealed class Singleton
{
private static Singleton instance;
private static readonly object lockObject = new object();

private Singleton()
{
// ...
}

public static Singleton GetInstance()


{
lock (lockObject)
{
if (instance == null)
{
instance = new Singleton();
}
return instance;
}
}
}
7. Conclusion
 Private constructors are a valuable tool in C# for controlling object creation and
enforcing design patterns.
 They can be used to restrict object creation from outside the class, ensuring that
objects are created in a controlled and predictable manner.
 Understanding the limitations and use cases of private constructors is crucial for
effective object-oriented design.

Destructors in C#
1. Introduction
 A destructor is a special method in C# that is automatically called by the garbage
collector when an object is no longer in use.
 Its primary purpose is to perform any necessary cleanup operations before the object's
memory is reclaimed.
2. Key Characteristics
 Name: The destructor's name is the same as the class name preceded by a tilde (~).
 Access Modifier: Destructors cannot have any access modifiers (public, private,
protected).
 Parameters: Destructors cannot have any parameters.
 Return Type: Destructors do not have a return type.
 Execution:
o Called implicitly by the garbage collector when the object is eligible for
garbage collection.
o The exact timing of destructor execution is not deterministic.
3. When is an Object Eligible for Garbage Collection?
 When all references to the object have been lost.
 When the application terminates.
4. Example
C#
public class MyClass
{
public MyClass()
{
Console.WriteLine("Constructor Called");
}

~MyClass()
{
Console.WriteLine("Destructor Called");
}
}
5. Calling the Garbage Collector
 You can request the garbage collector to run by calling GC.Collect().
 However, relying heavily on GC.Collect() can negatively impact performance.
6. Destructor Implementation
 Destructors are implemented using the Finalize method in the .NET runtime.
 The Finalize method is called recursively for all objects in the inheritance chain.
7. Important Points
 Destructors cannot be called explicitly.
 Destructors should be used sparingly and only when necessary to release unmanaged
resources (e.g., file handles, database connections).
 Relying heavily on destructors can negatively impact performance.
8. Dispose Pattern
 The preferred way to release unmanaged resources is by implementing the
IDisposable interface and providing a Dispose() method.
 The Dispose() method allows for explicit resource cleanup and provides more control
over the timing of resource release.
 The Dispose() method should be called by the user of the object when they are
finished with it.
9. Code Example (Dispose Pattern)
C#
public class MyClass : IDisposable
{
private bool disposedValue = false; // To detect redundant calls

protected virtual void Dispose(bool disposing)


{
if (!disposedValue)
{
if (disposing)
{
// Dispose managed resources here
}

// Dispose unmanaged resources here


disposedValue = true;
}
}

~MyClass()
{
// Do not change this code. Put cleanup code in Dispose(bool disposing) above.
Dispose(false);
}

public void Dispose()


{
Dispose(true);
GC.SuppressFinalize(this);
}
}
10. Conclusion
 Destructors are a crucial mechanism for cleaning up unmanaged resources in C#.
 While destructors are automatically called by the garbage collector, it's important to
use them judiciously and consider using the IDisposable interface for more control
over resource management.
 By following best practices for resource management, you can improve the
performance and stability of your applications.

Garbage Collection in .NET Framework


Understanding Memory Management
In a .NET application, you create objects using the new keyword. These objects reside
in memory, and as your application grows, the memory usage can increase. Manually
managing memory allocation and deallocation can be complex and error-prone. This
is where Garbage Collection (GC) comes in.
What is Garbage Collection?
Garbage Collection (GC) is an automatic memory management system in the .NET
Framework. It helps to:
 Allocate memory for objects
 Release memory that is no longer being used by the application
Benefits of Garbage Collection:
 Reduces development complexity by eliminating manual memory management.
 Minimizes the risk of memory leaks, which can lead to performance issues and
crashes.
Generations in Garbage Collection
The .NET GC uses a generational model to optimize memory management. Objects
are categorized into three generations:
 Generation 0 (Gen 0): This generation holds newly created objects. It's frequently
collected due to the high probability of short-lived objects.
 Generation 1 (Gen 1): Objects that survive a Gen 0 collection are promoted to Gen
1. These objects tend to have a longer lifespan but are not permanent. Gen 1 is
collected less frequently than Gen 0.
 Generation 2 (Gen 2): Objects that survive multiple collections in Gen 0 and Gen 1
are promoted to Gen 2. These are long-lived objects that remain in memory for a
longer duration. Gen 2 is collected the least frequently.
Diagram:
+-------------------+
| New | --> Gen 0 (Frequently Collected)
+-------------------+
|
v
+-------------------+
| Survive Gen 0 | --> Gen 1 (Less Frequently Collected)
+-------------------+
|
v
+-------------------+
| Long-Lived Objects| --> Gen 2 (Least Frequently Collected)
+-------------------+
Impact of Destructors on Garbage Collection
Destructors are methods with a tilde (~) preceding the class name. They are intended
to perform cleanup tasks for unmanaged resources (e.g., file handles, database
connections) before an object is garbage collected. However, using destructors for
cleanup can create a double garbage collection loop:
1. The GC identifies an object with a destructor and keeps it in memory instead of
immediate collection.
2. The destructor is called later, and then the object is finally collected.
This loop can negatively impact performance, especially for short-lived objects.
Solution: Finalize Dispose Pattern
To avoid the double garbage collection loop and properly release unmanaged
resources, use the IDisposable interface and the Dispose pattern:
1. Implement the IDisposable interface in your class.
2. Provide a Dispose method for releasing unmanaged resources.
3. Call GC.SuppressFinalize(this) within the Dispose method to prevent the destructor
from being called.
4. In your application code, call the Dispose method when you are finished using the
object.
Example:
C#
public class MyClass : IDisposable
{
private bool disposedValue = false;

protected virtual void Dispose(bool disposing)


{
if (!disposedValue)
{
if (disposing)
{
// Release managed resources
}

// Release unmanaged resources


disposedValue = true;
}
}

public void Dispose()


{
Dispose(true);
GC.SuppressFinalize(this);
}

~MyClass()
{
Dispose(false);
}
}
By following these practices, you can ensure efficient memory management and
prevent memory leaks in your .NET applications.
Additional Notes
 The GC is a complex system with various configurations and settings you can explore
for advanced memory management scenarios.
 Tools like the .NET Memory Profiler can help you analyze memory usage and
identify potential issues.

Differences Between Finalize and Dispose in C#


1. Purpose
 Finalize: Primarily used to release unmanaged resources (e.g., file handles, database
connections) when an object is garbage collected.
 Dispose: Used to release both managed and unmanaged resources in a deterministic
and controlled manner.
2. Invocation
 Finalize: Called implicitly by the garbage collector when the object is no longer
reachable. The timing of the call is non-deterministic.
 Dispose: Called explicitly by the programmer using the Dispose() method. This
provides more control over when resources are released.
3. Control
 Finalize: Offers limited control over the timing of resource release.
 Dispose: Provides more control over the timing of resource release, allowing for
immediate resource cleanup.
4. Determinism
 Finalize: Non-deterministic. The exact time of execution is not guaranteed.
 Dispose: Deterministic. The programmer controls when the Dispose() method is
called, enabling precise resource release.
5. Inheritance
 Finalize: Inherited from the Object class. If overridden, it should always call the base
class Finalize method to ensure proper cleanup.
 Dispose: Part of the IDisposable interface.
6. Resource Management
 Finalize: Primarily for unmanaged resources.
 Dispose: For both managed and unmanaged resources.
7. Performance
 Finalize: Can impact performance due to the unpredictable nature of garbage
collection.
 Dispose: Generally more efficient as it provides more control over resource release.
8. Example
C#
public class ResourceHolder : IDisposable
{
private bool disposedValue = false; // To detect redundant calls

protected virtual void Dispose(bool disposing)


{
if (!disposedValue)
{
if (disposing)
{
// Dispose managed resources here (e.g., other objects)
}

// Dispose unmanaged resources here


// ...

disposedValue = true;
}
}

~ResourceHolder()
{
// Do not change this code. Put cleanup code in Dispose(bool disposing) above.
Dispose(false);
}

public void Dispose()


{
Dispose(true);
GC.SuppressFinalize(this);
}
}
Key Considerations
 IDisposable: Always prefer using the IDisposable interface and the Dispose method
for resource management.
 Finalizer: Use Finalizers sparingly, primarily for unmanaged resources when there is
no guarantee that Dispose will be called.
 Dispose Pattern: Follow the dispose pattern to ensure proper resource cleanup and
avoid double garbage collection.
 GC.SuppressFinalize: Call GC.SuppressFinalize(this) in the Dispose method to
prevent the garbage collector from calling the finalizer if Dispose has already been
called.
Summary
 Finalize and Dispose are both mechanisms for resource cleanup in C#.
 Dispose provides more control, determinism, and performance benefits.
 Use Dispose whenever possible and rely on Finalize only when necessary for
unmanaged resources and when there's no guarantee that Dispose will be called.

Absolutely, here's a comprehensive response to the prompt combining the strengths of


both Response A and Response B, addressing their shortcomings, and incorporating
insights from the ratings:
Access Specifiers in C#
In object-oriented programming (OOP), access specifiers are keywords that control
the visibility and accessibility of classes, members (variables, properties, methods,
constructors, etc.), and other constructs within your code. They play a crucial role in
promoting data encapsulation and code organization, ensuring that only authorized
parts of your program can interact with specific elements.
Types of Access Specifiers in C#
C# supports six primary access specifiers:
1. Private: Members declared as private are only accessible within the class they are
defined in. This level of encapsulation restricts access to other classes, even those
derived from the containing class, within the same assembly.
2. Public: Public members are accessible from anywhere within the program, regardless
of the namespace or assembly in which they are declared. This is typically used for
exposing functionality or data that needs to be consumed by other parts of your
application.
3. Protected: Protected members are accessible from within the containing class and its
derived classes, but not from other unrelated classes, even within the same assembly.
This enforces a hierarchical access model where derived classes can inherit and
potentially extend the functionality of protected members.
4. Internal: Internal members are accessible from within the same assembly where they
are declared. This is useful for restricting access to classes and members that should
only be used by other classes within the same project or application.
5. Protected Internal: This access specifier combines aspects of protected and internal.
Members declared as protected internal are accessible from within the containing
class, its derived classes (regardless of assembly), and other classes within the same
assembly.
6. Private Protected (C# 7.2+): Introduced in C# 7.2, private protected members are
accessible only from the containing class and its derived classes within the same
assembly. This provides a more granular level of control compared to protected
access, restricting visibility to derived classes within the same application.
Understanding Type and Type Members
 Types: In C#, types refer to classes, structs, enums, interfaces, and delegates. They
represent blueprints for creating objects with specific characteristics and behaviors.
 Type Members: These are the elements that make up a type, including variables,
properties, methods, constructors, and events. They define the internal state and
functionality of the type. By default, if no access specifier is explicitly provided, type
members are considered private.
Access Specifiers with Type Members
The following table summarizes the access specifier options for type members and
their corresponding visibility:
Access
Visibility
Specifier
Private Within the containing class
Public Anywhere in the program
Protected Within the class and derived classes (same assembly)
Internal Within the same assembly
Protected Within the class, derived classes (any assembly), and same
Internal assembly
Private
Within the class and derived classes (same assembly)
Protected
Export to Sheets
Access Specifiers with Types
While all six access specifiers can be used with type members, only two are valid for
types themselves:
 Public: Public types can be accessed from anywhere in the program, allowing other
parts of your code to create instances and interact with them.
 Internal: Internal types are restricted to the same assembly, preventing direct usage
from other assemblies. By default, if no access specifier is specified for a type, it's
considered internal.
Example: Understanding Access Specifiers
Let's create a simple example to illustrate the concepts:
C#
public class AssemblyOneClass1
{
private int id; // Private member, only accessible within AssemblyOneClass1

public void Display1()


{
Console.WriteLine(id); // Accessing private member within the class
}
}

public class AssemblyOneClass2 : AssemblyOneClass1 // Derived class


{
// Cannot access private member directly from AssemblyOneClass2
// due to its private access
}

public class AnotherAssemblyClass // Class from a different assembly


{
// Cannot access AssemblyOneClass1 directly because it's internal
// (assuming it's not declared as public)
}
In this example:
 id is a private member, accessible only within AssemblyOneClass1.
 Display1() can access id because it's within the same class.
 AssemblyOneClass2 cannot directly access id due to its private scope.

Absolutely, here's a comprehensive response combining the strengths of the previous


ones, addressing their shortcomings, and incorporating insights from the ratings to
create exceptional exam notes on Encapsulation in C#:
Encapsulation in C#
Encapsulation is a fundamental principle of object-oriented programming (OOP) that
promotes data protection and code organization. It involves bundling data (often
called state or member variables) and the methods (functions that operate on the data)
that manipulate it into a single unit called a class. This unit controls access to its
internal components, ensuring data integrity and promoting better code
maintainability.
Key Concepts
1. Access Specifiers: These keywords define the visibility and accessibility of members
within a class. C# offers six access specifiers:
o Public: Accessible from anywhere in the program.
o Private: Accessible only within the class.
o Protected: Accessible within the class and derived classes (same assembly).
o Internal: Accessible within the same assembly.
o Protected Internal: Accessible within the class, derived classes (any
assembly), and same assembly.
o Private Protected (C# 7.2+): Accessible only within the class and derived
classes (same assembly).
2. Data Hiding: By declaring variables as private, you restrict direct access from
outside the class. Access to these variables is then controlled through public methods
(setters and getters) or properties. This promotes data security and allows for
validation logic to be applied before modifying the data.
Benefits of Encapsulation
 Data Protection: Encapsulation allows you to control how data is accessed and
modified, preventing accidental or malicious changes.
 Data Hiding: Sensitive data is hidden from external code, enhancing security.
 Improved Code Maintainability: Changes to how data is stored or accessed can be
made within the class without affecting other parts of the program that rely on its
public interface.
 Validation: You can implement validation logic in setter methods or properties to
ensure only valid data is assigned to variables.
 Increased Modularity: Code becomes more modular and organized by grouping
related data and behavior within a single unit.
Implementation Examples
1. Using Setter and Getter Methods
C#
public class BankAccount
{
private double _balance;

public double GetBalance()


{
return _balance;
}

public void SetBalance(double value)


{
if (value < 0)
{
throw new ArgumentException("Balance cannot be negative.");
}
_balance = value;
}
}
In this example, _balance is a private variable. The GetBalance() method allows
reading the balance, and the SetBalance() method allows setting the balance with
validation to ensure non-negative values.
2. Using Properties
C#
public class BankAccount
{
private double _balance;

public double Balance


{
get { return _balance; }
set
{
if (value < 0)
{
throw new ArgumentException("Balance cannot be negative.");
}
_balance = value;
}
}
}
This example achieves the same functionality as the previous one using a property
named Balance. Properties provide a cleaner syntax for accessing and modifying
private variables.

Abstrac on in C#
Abstrac on is a fundamental concept in object-oriented programming (OOP) that focuses on
exposing essen al func onali es (what an object does) while hiding implementa on details (how it
does it). It simplifies complex systems by breaking them down into smaller, more manageable units.
This promotes code reusability, maintainability, and flexibility.
Key Concepts
 Focus on Behavior: Define the services (methods) an object provides without revealing the
underlying logic. Users interact with these services without needing to know the intricate
workings of the object.
 Data Hiding: Encapsulate data (member variables) within a class and control access through
methods (ge ers and se ers) or proper es. This safeguards data integrity and prevents
unauthorized modifica ons.
 Interfaces: Define contracts outlining the services an object must implement. Classes can
inherit from interfaces and provide concrete implementa ons for the specified methods.
This promotes loose coupling and allows for interchangeable implementa ons.
 Abstract Classes: Serve as blueprints for derived classes by defining a set of abstract
methods (without implementa on) and poten ally concrete methods. Derived classes
inherit and provide implementa ons for abstract methods, achieving par al or complete
abstrac on.
Benefits of Abstrac on
 Simplified Code: Users focus on how to u lize an object's services without ge ng bogged
down in implementa on details.
 Increased Maintainability: Changes to internal workings don't necessarily affect how users
interact with the object, making modifica ons easier and less error-prone.
 Enhanced Reusability: Well-abstracted components can be reused in different contexts
without modifica on, promo ng code efficiency.
 Improved Security: Sensi ve data can be protected by restric ng access through methods
and proper es, preven ng unauthorized modifica ons.
 Flexibility in Design: Abstract classes and interfaces facilitate the crea on of interchangeable
implementa ons, fostering a more flexible and adaptable codebase.
Implementa on Examples
1. Abstrac on Using Interfaces
C#
public interface IShape
{
double CalculateArea();
}

public class Circle : IShape


{
public double Radius { get; set; }

public double CalculateArea()


{
return Math.PI * Radius * Radius;
}
}

public class Square : IShape


{
public double SideLength { get; set; }

public double CalculateArea()


{
return SideLength * SideLength;
}
}

public class AreaCalculator


{
public double CalculateShapeArea(IShape shape)
{
return shape.CalculateArea();
}
}

// Usage
IShape circle = new Circle { Radius = 5 };
IShape square = new Square { SideLength = 3 };

AreaCalculator calc = new AreaCalculator();


double circleArea = calc.CalculateShapeArea(circle);
double squareArea = calc.CalculateShapeArea(square);

Console.WriteLine($"Circle Area: {circleArea}");


Console.WriteLine($"Square Area: {squareArea}");
In this example, the IShape interface defines a contract for calcula ng the area of a shape. Circle and
Square implement this interface with their specific area calcula on logic, but the AreaCalculator class
only depends on the IShape interface, making it usable with any object that implements IShape.
2. Abstrac on Using Abstract Classes
C#
public abstract class Shape
{
public abstract double CalculateArea();
}

public class Circle : Shape


{
public double Radius { get; set; }

public override double CalculateArea()


{
return Math.PI * Radius * Radius;
}
}

public class Square : Shape


{
public double SideLength { get; set; }

public override double CalculateArea()


{
return SideLength * SideLength;
}
}

public class AreaCalculator


{
public double CalculateShapeArea(Shape shape)
{
return shape.CalculateArea();
}
}

// Usage
Shape circle = new Circle { Radius = 5 };
Shape square = new Square { SideLength = 3 };

AreaCalculator calc = new AreaCalculator();


double circleArea = calc.CalculateShapeArea(circle);
double squareArea = calc.CalculateShapeArea(square);

Console.WriteLine($"Circle Area: {circleArea}");


Console.WriteLine($"Square Area: {squareArea}");
This example is similar to the previous one, but it uses an abstract class Shape that defines the
CalculateArea method as abstract. Derived classes like Circle and Square provide concrete
implementa ons for this method, achieving abstrac on for the area calcula on logic.
Inheritance in C#
1. Defini on
 Inheritance is a fundamental concept in object-oriented programming that allows a class
(called the derived class, subclass, or child class) to inherit proper es and behaviors
(methods) from another class (called the base class, superclass, or parent class).
 This promotes code reusability and helps to organize code into a hierarchical structure.
2. Key Concepts
 Parent/Base/Superclass: The class from which other classes inherit.
 Child/Derived/Subclass: The class that inherits proper es and behaviors from the parent
class.
 Inheritance Rela onship: Established using the colon (:) syntax: class DerivedClass :
BaseClass { ... }
3. Rules of Inheritance
 Rule 1: Parent Class Constructor Accessibility:
o The child class constructor implicitly calls the parent class constructor.
o The parent class constructor must be accessible (public or protected) to the child
class.
o If the parent class constructor is parameterized, the child class constructor must
explicitly call the parent class constructor using the base() keyword and provide the
required arguments.
 Rule 2: Access to Parent Class Members:
o Child classes can access all public and protected members of the parent class.
o Child classes cannot directly access private members of the parent class.
o Parent classes cannot access members that are exclusively defined in the child class.
 Rule 3: Reference Variables and Inheritance:
o A parent class reference variable can be used to refer to an instance of the child
class.
o However, using a parent class reference, you can only access members of the parent
class. You cannot access members that are exclusively defined in the child class.
 Rule 4: Object Class as the Ul mate Parent:
o Every class in C# implicitly inherits from the Object class, which provides
fundamental methods like Equals(), GetHashCode(), GetType(), and ToString().
 Rule 5: Single Inheritance (with Classes)
o C# supports single inheritance with classes. This means a class can only inherit from
one immediate parent class.
 Rule 6: Calling Parent Class Constructors:
o If the parent class constructor is parameterized, the child class constructor must
explicitly call the parent class constructor using the base() keyword and provide the
necessary arguments.
Example
C#
class Animal
{
public void MakeSound()
{
Console.WriteLine("Generic animal sound");
}
}

class Dog : Animal


{
public void Bark()
{
Console.WriteLine("Woof!");
}
}

class Program
{
sta c void Main(string[] args)
{
Dog myDog = new Dog();
myDog.MakeSound(); // Inherited from Animal
myDog.Bark(); // Defined in Dog
}
}
Benefits of Inheritance
 Code Reusability: Avoids code duplica on by inheri ng common proper es and behaviors
from a parent class.
 Improved Code Organiza on: Creates a hierarchical structure that reflects real-world
rela onships.
 Enhanced Maintainability: Changes made to the parent class are automa cally reflected in
all derived classes.
 Extensibility: Allows for the crea on of specialized classes by adding new features or
modifying exis ng behavior.
Key Points
 Inheritance is a powerful tool for crea ng flexible and reusable code.
 Understanding the rules of inheritance is crucial for effec ve object-oriented design.
 U lize inheritance judiciously to maintain a well-structured and maintainable codebase.
Types of Inheritance in C#
In C#, inheritance can be broadly classified into two main types:
1. Implementa on Inheritance
 Defini on: This type of inheritance occurs when a class derives from another class.
 Characteris cs:
o The derived class inherits both data members (fields) and member func ons
(methods) from the base class.
o It allows for code reuse and promotes a hierarchical rela onship between classes.
o Subtypes:
 Single Inheritance: A class inherits from only one immediate base class.
(e.g., class DerivedClass : BaseClass { ... })
 Mul -level Inheritance: A chain of inheritance where a class inherits from
another derived class. (e.g., class A { ... }, class B : A { ... }, class C : B { ... })
 Hierarchical Inheritance: Mul ple classes inherit from a single base class.
(e.g., class A { ... }, class B : A { ... }, class C : A { ... })
2. Interface Inheritance
 Defini on: This type of inheritance occurs when a class implements an interface.
 Characteris cs:
o A class that implements an interface must provide concrete implementa ons for all
the methods declared in the interface.
o Interfaces define a contract that classes must adhere to.
o Allows for polymorphism and loose coupling between classes.
Key Points:
 Mul ple Inheritance (with Classes): C# does not directly support mul ple inheritance with
classes. This means a class cannot inherit from mul ple base classes directly.
 Hybrid Inheritance: A combina on of various inheritance types (single, mul -level,
hierarchical) can be considered hybrid inheritance. However, it's not a dis nct category in C#.
Example: Implementa on Inheritance
C#
public class Animal
{
public void MakeSound()
{
Console.WriteLine("Generic animal sound");
}
}

public class Dog : Animal


{
public void Bark()
{
Console.WriteLine("Woof!");
}
}
In this example, the Dog class inherits from the Animal class, demonstra ng single inheritance.
Example: Interface Inheritance
C#
public interface IShape
{
double CalculateArea();
}

public class Circle : IShape


{
public double Radius { get; set; }

public double CalculateArea()


{
return Math.PI * Radius * Radius;
}
}
In this example, the Circle class implements the IShape interface, providing its own implementa on
for the CalculateArea() method.
In Summary
 Implementa on inheritance focuses on inheri ng data members and member func ons
from a base class.
 Interface inheritance focuses on adhering to a contract defined by an interface.
 Understanding these types of inheritance is crucial for effec ve object-oriented design and
development in C#.
Understanding IS-A and HAS-A Rela onships in C#
In object-oriented programming (OOP), par cularly in C#, two fundamental rela onships exist
between classes: IS-A (inheritance) and HAS-A (composi on). These rela onships define how classes
interact and structure your code effec vely.
IS-A (Inheritance)
 Defini on: The IS-A rela onship represents a hierarchical connec on between a base class
(parent) and derived class (child). The derived class inherits proper es and behaviors from
the base class, essen ally becoming a specialized version of it.
 Characteris cs:
o Inheritance: The derived class inherits data members (fields) and member func ons
(methods) from the base class.
o Polymorphism: The derived class can override or extend func onali es inherited
from the base class.
o Subs tutability: Objects of the derived class can be used wherever objects of the
base class are expected (within certain limita ons).
 Example:
C#
class Animal
{
public void MakeSound()
{
Console.WriteLine("Generic animal sound");
}
}
class Dog : Animal
{
public void Bark()
{
Console.WriteLine("Woof!");
}
}
Here, Dog inherits from Animal, making Dog an IS-A type of Animal.
HAS-A (Composi on)
 Defini on: The HAS-A rela onship describes a composi onal rela onship. It signifies that
one class ("container") contains or is composed of objects from another class. This
rela onship is looser than inheritance.
 Characteris cs:
o Composi on: The container class has an instance (object) of another class as a
member variable.
o Independence: The contained object can exist independently of the container class.
o Encapsula on: The internal workings of the contained object are usually hidden
from the outside container class.
o Flexibility: Objects can be easily replaced or modified without affec ng the
container class.
 Example:
C#
class Address
{
public string Street { get; set; }
public string City { get; set; }
public string State { get; set; }
}

class Employee
{
public int Id { get; set; }
public string Name { get; set; }
public Address Address { get; set; }
}
Here, Employee HAS-A Address through the member variable Address.
Key Differences
Feature IS-A (Inheritance) HAS-A (Composi on)
Nature Inheritance (parent-child) Composi onal
Coupling Stronger Looser
Usage Extension, specializa on Requires func onality/proper es
Flexibility Limited ( ghtly bound) More flexible (easily modified)
Export to Sheets
Choosing the Right Rela onship
The choice between IS-A and HAS-A depends on the nature of the rela onship between your classes.
Ask yourself:
 Is the derived class a specialized version of the base class? (IS-A)
 Does the container class need func onali es provided by another class's object? (HAS-A)
Here's a quick guideline:
 If the statement "DerivedClass IS-A BaseClass" makes sense, use inheritance.
 If the statement "ContainerClass HAS-A AnotherClass" makes sense, use composi on.
Remember
 Inheritance is implemented using the : syntax (e.g., class DerivedClass : BaseClass { ... }).
 Composi on is simply declaring a variable of another class inside a class.
Generaliza on and Specializa on in C#
Generaliza on
 Concept:
o Iden fying commonali es among a group of exis ng classes and crea ng a new,
more general (parent) class.
o This parent class encapsulates the shared features of the child classes.
 Example:
o Consider classes like Circle, Square, and Rectangle. They all share the concept of
being "shapes."
o We can create a generalized class called Shape that includes common proper es like
Area and Perimeter.
o Circle, Square, and Rectangle would then inherit from the Shape class, inheri ng the
common proper es and implemen ng their specific area and perimeter calcula ons.
Specializa on
 Concept:
o Crea ng new, more specialized classes (child classes) from an exis ng base class.
o These derived classes inherit the proper es and behaviors of the base class and add
their own unique characteris cs.
 Example:
o You have a base class Animal with general characteris cs like Eat() and Sleep().
o You can create specialized classes like Dog, Cat, and Bird that inherit from Animal
and add specific behaviors like Bark(), Meow(), and Fly().
Key Differences
Feature Generaliza on Specializa on
Direc on Bo om-up (from specific to general) Top-down (from general to specific)
Purpose Iden fy commonali es, create a superclass Create specialized versions of a base class
Base Class Created a er child classes exist Already exists
Export to Sheets
In C#
 Generaliza on: O en achieved using interfaces or abstract classes.
 Specializa on: Achieved through class inheritance.
Example (Generaliza on)
C#
interface IShape
{
double CalculateArea();
}

class Circle : IShape


{
// ...
}

class Rectangle : IShape


{
// ...
}

class Square : IShape


{
// ...
}
Example (Specializa on)
C#
class Animal
{
public void MakeSound()
{
Console.WriteLine("Generic animal sound");
}
}

class Dog : Animal


{
public void Bark()
{
Console.WriteLine("Woof!");
}
}
In summary:
 Generaliza on is about iden fying commonali es and crea ng a more abstract class.
 Specializa on is about crea ng more specific classes from a general base class.
 Both are crucial for designing well-structured and maintainable object-oriented systems.
Abstract Classes and Abstract Methods in C#
Abstract Methods
 Defini on: An abstract method is a method declared within an abstract class or an interface
that has no implementa on. It only contains a signature (method name, return type, and
parameters).
 Declara on:
C#
public abstract void MyMethod();
 Purpose:
o To define a contract that derived classes must fulfill.
o To provide a common interface for derived classes.
Abstract Classes
 Defini on: A class declared with the abstract keyword. It can contain both abstract and non-
abstract methods.
 Characteris cs:
o Cannot be instan ated directly.
o Serves as a blueprint for other classes.
o Must be inherited by other classes.
o Derived classes must provide implementa ons for all inherited abstract methods.
Key Points
 Rela onship: Abstract methods are typically declared within abstract classes.
 Implementa on: Abstract methods must be implemented by derived classes.
 Instan a on: Abstract classes cannot be instan ated.
 Purpose:
o Enforce a common structure and behavior among derived classes.
o Promote code reusability.
o Support polymorphism.
Example
C#
public abstract class Shape
{
public abstract double CalculateArea();

public virtual void Display()


{
Console.WriteLine("This is a shape.");
}
}

public class Circle : Shape


{
public double Radius { get; set; }

public override double CalculateArea()


{
return Math.PI * Radius * Radius;
}
}

public class Rectangle : Shape


{
public double Width { get; set; }
public double Height { get; set; }

public override double CalculateArea()


{
return Width * Height;
}
}
In this example:
 Shape is an abstract class with an abstract method CalculateArea().
 Circle and Rectangle are derived classes that implement the CalculateArea() method
according to their specific shapes.
When to Use Abstract Classes and Methods
 Define a Common Interface: When you need to define a common set of methods that must
be implemented by derived classes.
 Enforce a Contract: To ensure that derived classes adhere to a specific contract or set of
rules.
 Promote Code Reusability: To share common func onality among derived classes.
 Implement Polymorphism: To enable polymorphic behavior where objects of different
derived classes can be treated as objects of the base class.
 Create Frameworks: To create flexible frameworks where derived classes can extend and
customize the base func onality.
Interfaces in C#
What is an Interface?
 An interface in C# is a blueprint that defines a contract or a set of rules that a class must
adhere to.
 It specifies a list of methods, proper es, events, or indexers that a class implemen ng the
interface must provide.
 Interfaces themselves cannot be instan ated.
Key Characteris cs
 All members of an interface are implicitly public and abstract.
 Interfaces can only contain method signatures (no method bodies), proper es, events, and
indexers.
 Interfaces cannot contain fields, constructors, or destructors.
 A class can implement mul ple interfaces.
 A class that implements an interface must provide concrete implementa ons for all
members of the interface.
How to Define an Interface
C#
interface IMyInterface
{
void MyMethod();
int MyProperty { get; set; }
event EventHandler MyEvent;
}
Example
C#
interface IShape
{
double CalculateArea();
}

class Circle : IShape


{
public double Radius { get; set; }

public double CalculateArea()


{
return Math.PI * Radius * Radius;
}
}

class Rectangle : IShape


{
public double Width { get; set; }
public double Height { get; set; }

public double CalculateArea()


{
return Width * Height;
}
}
In this example:
 IShape is an interface that defines a contract for calcula ng the area of a shape.
 Circle and Rectangle classes implement the IShape interface and provide their own
implementa ons for the CalculateArea() method.
When to Use Interfaces
 Define a Common Contract: To enforce a common set of methods that must be
implemented by different classes.
 Achieve Polymorphism: To treat objects of different classes uniformly.
 Support Mul ple Inheritance: Since C# does not support mul ple inheritance for classes,
interfaces provide a way to achieve similar behavior.
 Loose Coupling: Interfaces promote loose coupling between classes.
 Tes ng and Mocking: Interfaces are crucial for unit tes ng, allowing you to create mock
implementa ons for dependencies.
 Dependency Injec on: Interfaces are o en used in dependency injec on frameworks to
inject dependencies into classes.
Benefits of Using Interfaces
 Flexibility: Interfaces provide flexibility in design and implementa on.
 Reusability: Interfaces promote code reusability by defining common contracts.
 Maintainability: Interfaces make code more maintainable by improving modularity and
reducing dependencies.
 Testability: Interfaces facilitate unit tes ng by enabling the crea on of mock objects.
 Extensibility: Interfaces make it easier to extend func onality by crea ng new classes that
implement the interface.
In Summary
Interfaces are a fundamental concept in object-oriented programming in C#. They provide a powerful
mechanism for defining contracts, promo ng code reusability, and achieving polymorphism. By
understanding and effec vely using interfaces, you can create more flexible, maintainable, and
testable applica ons.
nterface Real-Time Example in C#
This example demonstrates how to use interfaces to model bank accounts in a C# applica on.
1. Define the Interface:
C#
interface IBankAccount
{
bool DepositAmount(decimal amount);
bool WithdrawAmount(decimal amount);
decimal CheckBalance();
}
This interface defines the core func onali es that any type of bank account should provide:
 DepositAmount(decimal amount): Deposits a specified amount into the account. Returns
true if successful, false otherwise.
 WithdrawAmount(decimal amount): Withdraws a specified amount from the account.
Returns true if successful, false otherwise.
 CheckBalance(): Returns the current balance of the account.
2. Create Concrete Account Classes:
 SavingAccount.cs
C#
public class SavingAccount : IBankAccount
{
private decimal _balance = 0;
private readonly decimal _perDayWithdrawLimit = 10000;
private decimal _todayWithdrawal = 0;

public bool DepositAmount(decimal amount)


{
_balance += amount;
Console.WriteLine($"You have Deposited: {amount}");
Console.WriteLine($"Your Account Balance: {_balance}");
return true;
}

public bool WithdrawAmount(decimal amount)


{
if (_balance < amount)
{
Console.WriteLine("You have Insufficient balance!");
return false;
}
else if (_todayWithdrawal + amount > _perDayWithdrawLimit)
{
Console.WriteLine("Withdrawal a empt failed!");
return false;
}
else
{
_balance -= amount;
_todayWithdrawal += amount;
Console.WriteLine($"You have Successfully Withdraw: {amount}");
Console.WriteLine($"Your Account Balance: {_balance}");
return true;
}
}

public decimal CheckBalance()


{
return _balance;
}
}
 CurrentAccount.cs
C#
public class CurrentAccount : IBankAccount
{
private decimal _balance = 0;

public bool DepositAmount(decimal amount)


{
_balance += amount;
Console.WriteLine($"You have Deposited: {amount}");
Console.WriteLine($"Your Account Balance: {_balance}");
return true;
}

public bool WithdrawAmount(decimal amount)


{
if (_balance < amount)
{
Console.WriteLine("You have Insufficient balance!");
return false;
}
else
{
_balance -= amount;
Console.WriteLine($"You have Successfully Withdraw: {amount}");
Console.WriteLine($"Your Account Balance: {_balance}");
return true;
}
}

public decimal CheckBalance()


{
return _balance;
}
}
3. Usage in the Main Method
C#
class Program
{
sta c void Main(string[] args)
{
Console.WriteLine("Saving Account:");
IBankAccount savingAccount = new SavingAccount();
savingAccount.DepositAmount(2000);
savingAccount.DepositAmount(1000);
savingAccount.WithdrawAmount(1500);
savingAccount.WithdrawAmount(5000);
Console.WriteLine($"Saving Account Balance: {savingAccount.CheckBalance()}");

Console.WriteLine("\nCurrent Account:");
IBankAccount currentAccount = new CurrentAccount();
currentAccount.DepositAmount(500);
currentAccount.DepositAmount(1500);
currentAccount.WithdrawAmount(2600);
currentAccount.WithdrawAmount(1000);
Console.WriteLine($"Current Account Balance: {currentAccount.CheckBalance()}");

Console.ReadLine();
}
}
Key Benefits
 Polymorphism: The IBankAccount interface allows you to treat both SavingAccount and
CurrentAccount objects uniformly.
 Extensibility: You can easily add new account types (e.g., "Fixed Deposit") by crea ng new
classes that implement the IBankAccount interface.
 Maintainability: Changes to the interface will automa cally affect all implemen ng classes,
ensuring consistency across the system.
Mul ple Inheritance in C#
C# does not directly support mul ple inheritance for classes. This means a class can only inherit
from one parent class at a me. However, interfaces provide a way to achieve similar func onality.
Why not Mul ple Inheritance with Classes?
The primary reason for not allowing mul ple inheritance with classes is the ambiguity problem.
Imagine a scenario where two parent classes have methods with the same name and signature.
When inheri ng from both classes, the compiler wouldn't know which method implementa on to
use, leading to errors.
Interfaces to the Rescue
Interfaces define contracts that classes can implement. A class can implement mul ple interfaces,
inheri ng their methods and proper es without the ambiguity problem. Here's how it works:
 Interfaces only contain method declara ons, not implementa ons.
 Classes implemen ng an interface must provide implementa ons for all declared methods.
Benefits of Using Interfaces for Mul ple Inheritance
 Flexibility: Classes can inherit behavior from mul ple interfaces without conflicts.
 Extensibility: New interfaces can be created without modifying exis ng classes.
 Loose Coupling: Interfaces promote loose coupling between classes, making code more
maintainable and testable.
Example: Bank Account System
Let's consider a bank account system with different account types (Savings and Current). Here's how
we can use interfaces for mul ple inheritance:
1. Define an IBankAccount interface with common func onali es like Deposit, Withdraw, and
CheckBalance.
2. Create separate SavingsAccount and CurrentAccount classes implemen ng IBankAccount.
3. Each class provides specific implementa ons for deposit/withdrawal rules based on their
account type.
Code Example
C#
interface IBankAccount
{
bool Deposit(decimal amount);
bool Withdraw(decimal amount);
decimal CheckBalance();
}

class SavingsAccount : IBankAccount


{
private decimal _balance;
private readonly decimal _perDayWithdrawLimit;

public bool Deposit(decimal amount)


{
// Implement deposit logic for Savings Account
}

public bool Withdraw(decimal amount)


{
// Implement withdrawal logic with per-day limit check
}

public decimal CheckBalance()


{
return _balance;
}
}

class CurrentAccount : IBankAccount


{
private decimal _balance;

public bool Deposit(decimal amount)


{
// Implement deposit logic for Current Account
}

public bool Withdraw(decimal amount)


{
// Implement withdrawal logic without any limit check
}

public decimal CheckBalance()


{
return _balance;
}
}
Key Points to Remember
 Interfaces cannot be instan ated.
 A class can implement mul ple interfaces.
 Interfaces promote loose coupling and cleaner code design.
Addi onal Considera ons
 While interfaces offer a good alterna ve to mul ple inheritance with classes, there might be
situa ons where true mul ple inheritance is desirable. In such cases, consider using design
pa erns like the Favor Composi on Over Inheritance principle.
 When using mul ple interfaces, ensure there are no conflic ng method signatures across
interfaces.
Polymorphism in C#
Polymorphism is a fundamental concept in object-oriented programming (OOP) that allows objects
of different classes to 1 respond differently to the same method call. It literally means "many forms"
and enables code to be more flexible and reusable.

1. github.com

github.com

Types of Polymorphism in C#
There are two main types of polymorphism in C#:
1. Compile-Time Polymorphism (Sta c Polymorphism, Early Binding):
o The behavior of a method is determined at compile me based on the method
signature (name and number and types of parameters).
o Achieved through method overloading.
o Method overloading allows mul ple methods with the same name but different
signatures within the same class.
o The compiler selects the appropriate method based on the arguments used in the
call.
2. Run-Time Polymorphism (Dynamic Polymorphism, Late Binding):
o The behavior of a method is determined at run me based on the object's actual
type.
o Achieved through inheritance and method overriding.
o A subclass (derived class) can override a method inherited from its parent (base
class) to provide its own implementa on.
o When a method is called on a reference variable of the parent class type, the actual
object's type determines which method implementa on is executed at run me.
Benefits of Polymorphism
 Flexibility: Code can adapt to different situa ons based on the object type.
 Reusability: Methods can be wri en in a generic way to handle different types of objects.
 Maintainability: Code is easier to understand and modify because the logic is centralized in
methods.
Example: Shape Inheritance
C#
class Shape
{
public virtual void Draw() // Virtual method for overriding
{
Console.WriteLine("Drawing a shape");
}
}

class Circle : Shape


{
public override void Draw()
{
Console.WriteLine("Drawing a circle");
}
}

class Square : Shape


{
public override void Draw()
{
Console.WriteLine("Drawing a square");
}
}

class Program
{
sta c void Main(string[] args)
{
Shape[] shapes = { new Circle(), new Square() };
foreach (Shape shape in shapes)
{
shape.Draw(); // Calls the appropriate Draw method based on object type at run me
}
}
}
Output:
Drawing a circle
Drawing a square
Key Points
 Polymorphism is a powerful tool for wri ng flexible and reusable code.
 Compile- me polymorphism uses method signatures for binding, while run- me
polymorphism uses object types for binding.
 Inheritance and method overriding are essen al for achieving run- me polymorphism.
 Consider using interfaces for alterna ve inheritance-like behavior without the ambiguity
problem of mul ple class inheritance.
Method Overloading in C#
Method overloading is a fundamental concept in object-oriented programming (OOP) that allows you
to define mul ple methods with the same name within a class, but with different signatures. The
signature of a method is determined by the number, type, and order of its parameters.
Key Points:
 Allows for cleaner and more intui ve code.
 Improves code readability and maintainability.
 Enables polymorphism (one method name, different behaviors based on arguments).
 Achieved through differences in method signatures, not return types.
Benefits of Method Overloading:
 Flexibility: Code can adapt to different situa ons based on the arguments provided.
 Reusability: Methods can be wri en in a generic way to handle various data types.
 Readability: Code becomes more readable as overloaded methods o en have clear names
reflec ng their purpose.
Example:
C#
class Shape
{
public virtual void Draw() // Virtual method for overriding
{
Console.WriteLine("Drawing a shape");
}
}

class Circle : Shape


{
public override void Draw()
{
Console.WriteLine("Drawing a circle");
}
}

class Square : Shape


{
public override void Draw()
{
Console.WriteLine("Drawing a square");
}
}

class Program
{
sta c void Main(string[] args)
{
Shape[] shapes = { new Circle(), new Square() };
foreach (Shape shape in shapes)
{
shape.Draw(); // Calls the appropriate Draw method based on object type at run me
(polymorphism)
}
}
}
Output:
Drawing a circle
Drawing a square
When to Use Method Overloading:
 When you need to perform the same opera on on different data types.
 When you want to provide mul ple ways to call a method with varying amounts of data.
 To improve code readability by using descrip ve method names with overloaded versions for
different scenarios.
Things Not Considered in Method Signature:
 Return Type: While methods can have different return types, it's not part of the signature for
overloading. You cannot overload methods based solely on return type differences.
 Access Specifiers (public, private, protected): These can be different for overloaded
methods.
 Access Modifiers (sealed, sta c, virtual): These can also be different for overloaded
methods.
Overloading vs. Overriding:
 Overloading: Occurs within the same class, methods have the same name but different
signatures.
 Overriding: Occurs in inheritance hierarchies, a subclass redefines a method inherited from
its parent class.
Addi onal Notes:
 Method overloading is resolved at compile me.
 C# allows overloading constructors (special methods for object crea on) based on parameter
differences.
Method Overriding in C#
Method overriding is a fundamental object-oriented programming (OOP) concept that allows you to
redefine the behavior of an inherited method in a subclass. It enables polymorphism, a cornerstone
of OOP that allows for "one method name, different behaviors based on arguments."
Key Points:
 Redefining Inherited Methods: Subclasses (derived classes) can override methods inherited
from their parent classes (base classes).
 Signature Matching: The overriding method must have the same signature (name,
parameter list) as the overridden method.
 Access Modifiers: Overridden methods can have different access modifiers (public, private,
protected) than the overridden method.
 Virtual Keyword: The parent class method must be declared as virtual to allow overriding in
subclasses.
 override Keyword: The subclass method uses the override keyword to explicitly indicate it's
overriding a parent class method.
 Benefits:
o Flexibility: Code adapts to specific situa ons based on arguments provided.
o Reusability: Methods are wri en generically to handle various data types.
o Readability: Clear method names and overriding behavior enhance code clarity.
Diagram:
+--------------+
| Base Class | (Parent)
+--------------+
|
(Inherits) +--------------+
| | Sub Class 1 | (Child 1)
+--------------+ |
| +--------------+
| (Inherits) | Sub Class 2 | (Child 2)
+--------------+ |
+--------------+
| Sub Class N | (Child N)
+--------------+

- Overridden methods can exist in mul ple subclasses of the base class.
- Each subclass can poten ally have its own implementa on of the overridden method.
Code Example:
C#
class Shape
{
public virtual void Draw() // Virtual method for overriding
{
Console.WriteLine("Drawing a shape");
}
}

class Circle : Shape


{
public override void Draw()
{
Console.WriteLine("Drawing a circle");
}
}

class Square : Shape


{
public override void Draw()
{
Console.WriteLine("Drawing a square");
}
}

class Program
{
sta c void Main(string[] args)
{
Shape[] shapes = { new Circle(), new Square() };
foreach (Shape shape in shapes)
{
shape.Draw(); // Calls the appropriate Draw method based on object type at run me
(polymorphism)
}
}
}
Output:
Drawing a circle
Drawing a square
Explana on:
1. The Shape class defines a Draw() method declared as virtual. This allows subclasses to
override it.
2. The Circle and Square classes inherit from Shape.
3. Both Circle and Square override the Draw() method with their specific implementa on for
drawing a circle and a square, respec vely.
4. In the Main() method, an array of Shape objects holds instances of Circle and Square.
5. The foreach loop iterates through the shapes array.
6. When shape.Draw() is called, the run me determines the actual type of the object (Circle or
Square) and invokes the overridden method defined in that specific subclass. This
demonstrates polymorphism in ac on.
Key Differences from Method Overloading:
Feature Method Overriding Method Overloading
Signature Same name, same signature Same name, different signature
Loca on Subclass Same class
Inheritance Required Not required
Keyword override keyword used No special keyword needed
Purpose Redefine inherited behavior Provide mul ple func onali es
Polymorphism Enables dynamic polymorphism Enables sta c polymorphism
Export to Sheets
Addi onal Notes:
 Method overriding is resolved at run me.
 C# allows overriding constructors (special methods for object crea on) based on parameter
differences.
 Overridden methods can call the overridden method using the base keyword.
Method Hiding in C#
Method Hiding (also known as Shadowing) allows you to redefine a method inherited from a base
class in a derived class, using the same name and signature (name and parameter list). However,
unlike Method Overriding, it's done without explicitly declaring the derived class method as override.
Key Points:
 Redefining Inherited Methods: Derived classes can hide inherited methods from the base
class.
 Signature Matching: The hidden method must have the same signature as the base class
method.
 Access Modifiers: Visibility of the hidden method can be different from the base class
method.
 No virtual or override Keywords: Unlike Overriding, neither virtual nor override keywords
are used.
 Op onal new Keyword: While not strictly necessary, using new explicitly clarifies to the
compiler that you intend to hide the base class method.
 Compiler Warning: Without the new keyword, the compiler will issue a warning about hiding
the base class method.
Diagram:
+--------------+
| Base Class | (Parent)
+--------------+
|
(Inherits) +--------------+
| | Derived Class | (Child)
+--------------+
|
| (Hides Methods)
Code Example:
C#
class Shape
{
public void Draw() // Base class method
{
Console.WriteLine("Drawing a shape");
}
}

class Circle : Shape


{
public new void Draw() // Hides the base class Draw method
{
Console.WriteLine("Drawing a circle");
}
}

class Square : Shape


{
public void Draw() // Overrides the base class Draw method (op onal)
{
Console.WriteLine("Drawing a square");
}
}

class Program
{
sta c void Main(string[] args)
{
Shape[] shapes = { new Circle(), new Square() };
foreach (Shape shape in shapes)
{
shape.Draw(); // Calls the appropriate Draw method based on object type
}
}
}
Output:
Drawing a circle
Drawing a square
Explana on:
1. The Shape class defines a Draw() method.
2. The Circle class inherits from Shape.
3. The Circle class defines its own Draw() method with the new keyword, hiding the base class
method.
4. The Square class inherits from Shape and overrides the Draw() method using the override
keyword (op onal for non-abstract methods).
5. In the Main() method, an array of Shape objects holds instances of Circle and Square.
6. The foreach loop iterates through the shapes array.
7. When shape.Draw() is called, the run me determines the actual type of the object (Circle or
Square) and invokes the appropriate hidden or overridden method defined in that specific
subclass.
Key Differences from Method Overriding:
Feature Method Hiding Method Overriding
Signature Same name, same signature Same name, same signature
Loca on Derived class (without override) Derived class (with override)
Required (base class method must
Inheritance Not required
be virtual)
No special keyword needed (op onal
Keyword override keyword used
new)
Purpose Hide base class behavior Redefine base class behavior
Compiler Warning Warning if no new keyword No warning
Accessing Base Class Not directly accessible using derived Can be accessed using base
Method class instance keyword
Export to Sheets
When to Use Method Hiding:
 You don't want the derived class to inherit the base class method behavior but want to keep
the same method name and signature.
 You're working with legacy code where overriding wasn't used consistently.
 You need to explicitly indicate that a method is intended to hide the base class method
(using new).
Considera ons:
 Method hiding can make code less readable and maintainable as the base class method is no
longer directly accessible through the derived class instance.
 Overriding is generally preferred as it maintains a clear rela onship between base and
derived class methods.
Par al Classes and Par al Methods in C#
Par al Classes
 Concept: Allow you to split the defini on of a class across mul ple files.
 Syntax: Use the par al keyword before the class declara on in each file.
 Benefits:
o Improved Code Organiza on: Large classes can be divided into smaller, more
manageable units.
o Collabora on: Mul ple developers can work on different parts of a class
simultaneously.
o Code Genera on: Useful for code generated by tools like En ty Framework, where
you can add custom logic to auto-generated classes.
Example:
 File 1: Employee.cs
C#
public par al class Employee
{
public string FirstName { get; set; }
public string LastName { get; set; }
}
 File 2: Employee.cs
C#
public par al class Employee
{
public double Salary { get; set; }
public void CalculateSalary()
{
// Calculate salary logic
}
}
Par al Methods
 Concept: Allow you to define a method signature without an implementa on. The
implementa on can be provided in another part of the par al class.
 Syntax:
o Declare the method with the par al keyword and void return type.
o Provide the implementa on of the par al method in another part of the par al
class.
 Behavior:
o If the implementa on is not provided, the compiler removes the par al method and
all calls to it.
o Useful for op onal behavior or code genera on scenarios.
Example:
 File 1: MyClass.cs
C#
public par al class MyClass
{
public void DoSomething()
{
Console.WriteLine("Doing something...");
LogEvent(); // Call to the par al method
}

par al void LogEvent(); // Declara on of the par al method


}
 File 2: MyClass.cs
C#
public par al class MyClass
{
par al void LogEvent() // Implementa on of the par al method
{
// Log the event to a file or database
Console.WriteLine("Event logged.");
}
}
Rules for Par al Classes:
 All parts must have the same access modifier (e.g., public, private).
 All parts must have the same base class or interface (if any).
 If any part is declared abstract, the en re class is considered abstract.
 If any part is declared sealed, the en re class is considered sealed.
Rules for Par al Methods:
 Must have void return type.
 Cannot have access modifiers, virtual, abstract, override, new, sealed, or extern modifiers.
 Must be declared within a par al class or struct.
 Implementa on is op onal. If not implemented, the method and its calls are removed by the
compiler.
 Declara on and implementa on must have the same signature.
Key Use Cases:
 Large Classes: Spli ng large classes into smaller, more manageable files improves readability
and maintainability.
 Code Genera on: Useful for working with code generated by tools, allowing you to add
custom logic to auto-generated classes.
 Op onal Behavior: Implement op onal func onality using par al methods, which can be
removed by the compiler if not needed.
In Summary:
Par al classes and par al methods are powerful features in C# that enhance code organiza on,
maintainability, and flexibility. By understanding their usage and limita ons, you can effec vely
leverage these features in your C# applica ons.
Sealed Classes
 Concept: A sealed class prevents inheritance. You cannot create new classes derived from a
sealed class.
 Purpose:
o Enforce a specific behavior or implementa on for a class.
o Prevent unintended modifica ons through inheritance.
o Improve performance by avoiding virtual method calls (discussed later).
 Syntax: Use the sealed keyword before the class declara on.
C#
public sealed class MathU ls
{
public sta c double Add(double a, double b)
{
return a + b;
}
}
Example: In the MathU ls class, the Add method is crucial for calcula ons, and you want to
ensure its implementa on remains unchanged. Sealing the class prevents subclasses from
accidentally overriding it.
Diagram:
Code snippet
graph LR
A[MathU ls (sealed)] --> B{Object usage}
Key Points:
 Sealed classes can s ll be instan ated and their members accessed.
 They can implement interfaces to define contracts.
 Sealed classes are o en used for u lity classes, singletons, and base classes where
inheritance is not desired.
Sealed Methods
 Concept: A sealed method prevents overriding in derived classes.
 Purpose:
o Ensure a specific implementa on is used throughout the inheritance hierarchy.
o Improve performance by avoiding virtual method call overhead (explained later).
 Syntax: Use the sealed keyword with the override keyword when overriding a virtual method
in a derived class.
C#
public class Animal
{
public virtual void MakeSound()
{
Console.WriteLine("Generic animal sound");
}
}

public class Dog : Animal


{
public sealed override void MakeSound()
{
Console.WriteLine("Woof!");
}
}
 Example: In the Animal hierarchy, the Dog class sound should always be "Woof!". Sealing the
MakeSound method in Dog prevents further subclasses from overriding it.
Diagram:
Code snippet
graph LR
A[Animal] --> B{MakeSound()}
B --> C[Dog (sealed MakeSound())]
C --> D{Object usage}
Key Points:
 A method can only be sealed if it's already declared as virtual in the base class.
 Sealed methods are inherited but cannot be overridden.
 They are o en used for core func onali es that shouldn't be altered in subclasses.
Comparison with Related Concepts
 Abstract Classes: Abstract classes define a contract that derived classes must implement.
They cannot be instan ated directly. Sealed classes prevent inheritance, while abstract
classes require inheritance.
 Private Methods: Private methods are hidden within a class and cannot be accessed from
derived classes. Sealed methods can be accessed from derived classes but cannot be
overridden.
When to Use Sealed Classes and Methods
 Use sealed classes when inheritance is not intended and the class implementa on should be
final.
 Use sealed methods when a specific behavior needs to be enforced throughout the class
hierarchy.
 Consider performance implica ons - sealed methods avoid virtual method call overhead,
which can be beneficial in performance-cri cal scenarios.
In Summary
Sealed classes and methods are powerful tools in C# for controlling inheritance and ensuring specific
behaviors. By understanding their concepts, purposes, and use cases, you can effec vely design
robust and maintainable object-oriented code.

You might also like