0% found this document useful (0 votes)
10 views4 pages

Construcrtor

The document provides examples of constructors and method overloading in C#. It explains the use of default and parameterized constructors in a Car class, as well as method overloading in a Calculator class. Additionally, it describes different types of loops in C#, including for, while, do-while, and foreach loops, along with their syntax and use cases.

Uploaded by

omsaidesai9
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)
10 views4 pages

Construcrtor

The document provides examples of constructors and method overloading in C#. It explains the use of default and parameterized constructors in a Car class, as well as method overloading in a Calculator class. Additionally, it describes different types of loops in C#, including for, while, do-while, and foreach loops, along with their syntax and use cases.

Uploaded by

omsaidesai9
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/ 4

CONSTRUCTOR EXAMPLE:

class Car

public string Model;

public int Year;

// Default Constructor

public Car()

Model = "Unknown";

Year = 2000;

// Parameterized Constructor

public Car(string model, int year)

Model = model;

Year = year;

class Program

static void Main()

// Using default constructor

Car car1 = new Car();

Console.WriteLine($"Car 1: Model = {car1.Model}, Year = {car1.Year}");

// Using parameterized constructor

Car car2 = new Car("Toyota", 2022);

Console.WriteLine($"Car 2: Model = {car2.Model}, Year = {car2.Year}");

}
METHOD OVERLOADING:
class Calculator

// Method to add two integers

public int Add(int a, int b)

return a + b;

// Method to add three integers

public int Add(int a, int b, int c)

return a + b + c;

// Method to add two doubles

public double Add(double a, double b)

return a + b;

class Program

static void Main()

Calculator calc = new Calculator();

// Calling overloaded methods

Console.WriteLine(calc.Add(2, 3)); // Calls Add(int, int)

Console.WriteLine(calc.Add(2, 3, 4)); // Calls Add(int, int, int)

Console.WriteLine(calc.Add(2.5, 3.5)); // Calls Add(double, double)

}
Explain loops in C#.
In C#, loops are used to repeat a block of code multiple times based on a condition. There are several
types of loops in C#, each serving different purposes:

for loop: Used when the number of iterations is known beforehand.

Syntax:

for (initialization; condition; increment)


{
// Code to execute
}

Example:

for (int i = 0; i < 5; i++)


{
Console.WriteLine(i); // Output: 0 1 2 3 4
}

while loop: Used when the condition is checked before each iteration, and the loop runs as long as
the condition remains true.

Syntax:

while (condition)
{
// Code to execute
}

Example:

int i = 0;
while (i < 5)
{
Console.WriteLine(i); // Output: 0 1 2 3 4
i++;
}

do-while loop: Similar to the while loop, but the condition is checked after the loop is executed, so it
always runs at least once.

Syntax:

do
{
// Code to execute
} while (condition);
Example:

int i = 0;
do
{
Console.WriteLine(i); // Output: 0 1 2 3 4
i++;
} while (i < 5);

foreach loop: Used to iterate through each element in a collection, such as an array or list.

Syntax:

foreach (type element in collection)


{
// Code to execute
}

Example:

int[] numbers = { 1, 2, 3, 4, 5 };
foreach (int num in numbers)
{
Console.WriteLine(num); // Output: 1 2 3 4 5
}

Key Differences:

For loop is typically used when the number of iterations is known.


While loop is used when the condition needs to be checked before each iteration.
Do-while loop is useful when the loop should run at least once, regardless of the condition.
Foreach loop is convenient for iterating over collections, such as arrays or lists.

You might also like