Unit 2
Unit 2
1. Properties of a Class in C#
In C#, a class is a blueprint or template used to create
objects. Classes contain fields, methods, properties,
constructors, and events that define the behavior of an
object. The properties of a class refer to its attributes or
features that define its functionality. The key properties of a
class include:
Encapsulation: The process of bundling the data (fields)
and methods (functions) that operate on the data within
one unit or class. This hides the internal state of an
object and only exposes a controlled interface for
interacting with it.
Inheritance: In C#, classes can inherit properties and
methods from other classes. The derived class inherits
the behavior of the base class, making it easier to reuse
and extend existing code.
Polymorphism: It allows the same method to behave
differently based on the object that invokes it. There are
two types of polymorphism in C#: compile-time (method
overloading) and runtime (method overriding).
Abstraction: Abstraction means hiding the
implementation details from the user and providing a
simple interface. It helps in focusing on the essential
features of an object, ignoring unnecessary details.
Access Modifiers: Access modifiers in C# control the
visibility of members (methods, properties, etc.) of a
class. These modifiers are used to specify the
accessibility level (public, private, protected, etc.) of
each member.
Constructors: A constructor is a special method used for
initializing objects. It is called automatically when an
object is created. Constructors can be overloaded with
different parameters to initialize objects in various ways.
Example:
public class Car
{
public string make;
public string model;
private int year;
3. Conditional Statements
Conditional statements allow the program to execute
different sets of instructions based on whether a condition is
true or false. They help in decision-making by controlling the
flow of the program. Common conditional statements in C#
are if, else, else if, and switch.
if statement: Executes a block of code if a specified
condition evaluates to true.
else if statement: Provides additional conditions to test
if the initial if condition is false.
else statement: Executes a block of code if none of the
previous conditions are true.
switch statement: Provides an alternative to multiple if-
else statements when checking a single variable for
multiple possible values.
Example:
int age = 18;
6. Constructors
In C#, a constructor is a special method used to initialize
objects. It is called when an instance of the class is created.
Constructors do not return a value. They have the same name
as the class and can be overloaded to accept different
parameters for object initialization.
Default Constructor: A constructor without parameters.
Parameterized Constructor: A constructor that accepts
parameters to initialize an object with specific values.
Example:
public class Car
{
public string make;
public string model;
private int year;
// Parameterized Constructor
public Car(string make, string model, int year)
{
this.make = make;
this.model = model;
this.year = year;
}
7. Method Overloading
Method overloading is a feature that allows multiple
methods in the same class to have the same name but with
different parameter types or numbers. This enables methods
to perform similar tasks with different types of inputs.
Example:
public class Calculator
{
public int Add(int a, int b)
{
return a + b;
}
class Program
{
static void Main()
{
Calculator calc = new Calculator();
9. Loops in C#
Loops in C# are used to repeatedly execute a block of code
based on a condition. The most commonly used loops are for,
while, and do-while loops.
For Loop: A for loop is used when the number of
iterations is known beforehand.
for (int i = 0; i < 5; i++)
{
Console.WriteLine(i); // Output: 0 1 2 3 4
}
While Loop: A while loop repeats a block of code as long
as the condition is true. It checks the condition before
the first iteration.
int i = 0;
while (i < 5)
{
Console.WriteLine(i); // Output: 0 1 2 3 4
i++;
}
Do-While Loop: A do-while loop executes the block of
code at least once and then checks the condition.
int i = 0;
do
{
Console.WriteLine(i); //