Week_3
Week_3
Lecture Objectives:
Definition:
An enumeration is a set of named integer constants. An enumerated type is declared using the enum
keyword.
An enum (enumeration) in C# is a value type that defines a set of named integer constants. It helps
improve code readability by replacing hard-coded numbers with meaningful names.
C# enumerations are value data type. In other words, enumeration contains its own values.
It is used to assign the names or string values to integral constants, that make a program easy to read
and maintain.
enum <enum_name>
enum <enum_name>
{
enumeration list
};
Where,
The enum_name specifies the enumeration type name.
The enumeration list is a comma-separated list of identifiers.An Enum (Enumerated Type) is a special
value type in C# used to define a set of named integer constants.
Syntax:
csharp
Real-Life Example:
Think of a traffic light system:
csharp
By default, the first member of an enum has the value 0 and the value of each successive enum
member is increased by 1. For example, in the following enumeration, Monday is 0, Tuesday is 1,
Wednesday is 2 and so forth.
• Example:
enum WeekDays
{
Monday, Tuesday, Wednesday, Thursday, Friday, Saturday, Sunday
}
Console.WriteLine((int)WeekDays.Monday);
Console.WriteLine((int)WeekDays.Friday);
csharp
Write an enum for different car types (Sedan, SUV, Hatchback, Truck).
Write an enum for different car types (Sedan, SUV, Hatchback, Truck).
enum CarType { Sedan, SUV, Hatchback, Truck }
using System;
class Program
{
static void Main()
{
CarType myCar = CarType.SUV;
Console.WriteLine("My car type is: " + myCar);
}
}
Assign custom values to each type.
enum CarType
{
Sedan = 1,
SUV = 2,
Hatchback = 3,
Truck = 4
}
2. Structures (struct) in C#
Definition:
A `struct` in C# is a value type that groups related variables together. It is useful when you need
lightweight objects that do not require inheritance.
Syntax:
csharp
struct Student
{
public string Name;
public int Age;
public double GPA;
}
Example Usage:
csharp
Student student1;
student1.Name = "John";
student1.Age = 21;
student1.GPA = 3.8;
Errors and exceptions are unavoidable in programming. C# provides a structured way to handle
errors using exception handling mechanisms such as try, catch, finally, and throw.
An exception is an event that disrupts the program's normal flow. C# provides a structured way to
handle exceptions using try, catch, finally, and throw.
try
{
int num1 = 10, num2 = 0;
int result = num1 / num2; // Division by zero!
Console.WriteLine(result);
}
catch (Exception ex)
{
Console.WriteLine("Error: " + ex.Message);
}
Output: Error: Attempted to divide by zero.
✔ Benefit: Prevents the program from crashing and provides a meaningful error message.
Keyword Purpose
try Contains the code that might cause an exception.
catch Handles exceptions and prevents program crashes.
finally (Optional) Always executes, used for cleanup (closing files, DB connections, etc.).
throw Manually triggers an exception.
try
{
int[] arr = { 1, 2, 3 };
Console.WriteLine(arr[5]); // Index out of bounds!
}
catch (IndexOutOfRangeException ex)
{
Console.WriteLine("Error: " + ex.Message);
}
finally
{
Console.WriteLine("Execution completed.");
}
Debugging in C#