0% found this document useful (0 votes)
6 views6 pages

Week_3

This lecture covers key concepts in C# including Enumerated Types (Enums), Structures (structs), Arrays, String manipulation, and Lists. It explains the definition, syntax, and advantages of Enums and structs, as well as error handling and debugging techniques in C#. Students are encouraged to engage in activities such as creating their own enums and understanding exception handling mechanisms.
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)
6 views6 pages

Week_3

This lecture covers key concepts in C# including Enumerated Types (Enums), Structures (structs), Arrays, String manipulation, and Lists. It explains the definition, syntax, and advantages of Enums and structs, as well as error handling and debugging techniques in C#. Students are encouraged to engage in activities such as creating their own enums and understanding exception handling mechanisms.
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/ 6

WEEK-3

Lecture Objectives:

By the end of this lecture, students should be able to:

1. Understand Enumerated Types (Enums) and their use in C#.

2. Explain Structures (structs) and how they differ from classes.

3. Define and use Arrays in C#.

4. Manipulate Strings effectively using C#.

5. Work with Lists and understand their advantages over arrays.

1. Enumerated Types (Enums) in C#

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 user defined data types.

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

enum DaysOfWeek { Sunday, Monday, Tuesday, Wednesday, Thursday, Friday, Saturday }

Real-Life Example:
Think of a traffic light system:

csharp

enum TrafficLight { Red, Yellow, Green }

Here, `Red` might be 0, `Yellow` = 1, and `Green` = 2 by default.

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);

// C# program to illustrate the enums with their default values


using System;
namespace ConsoleApplication1
{
// making an enumerator 'month'
enum month
{ jan, feb, mar, apr, may, jun, jul, aug, sep,oct,nov,dec}
class Program
{
static void Main(string[] args)
{
// getting the integer values of data members..
Console.WriteLine("The value of jan in month " + "enum is " + (int)month.jan);
Console.WriteLine("The value of feb in month " + "enum is " + (int)month.feb);
Console.WriteLine("The value of mar in month " + "enum is " + (int)month.mar);
Console.WriteLine("The value of apr in month " + "enum is " + (int)month.apr);
Console.WriteLine("The value of may in month " + "enum is " + (int)month.may);
}
}}
Key Features of Enums:

Improves code readability.

Prevents invalid values by restricting options.

Can assign custom integer values.

Example: Assigning Custom Values

csharp

enum Status { Success = 1, Error = -1, Pending = 0 }

Student Activity (5 min):

Write an enum for different car types (Sedan, SUV, Hatchback, Truck).

Assign custom values to each type.

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;

Difference Between `struct` and `class`:

Feature Struct Class


Type Value Type Reference Type
Memory Allocation Stack Heap

Introduction to Error Handling

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.

Why is Error Handling Important?

Prevents application crashes.


Provides meaningful error messages.
Helps in debugging and logging errors.
Improves user experience by handling issues gracefully.

Error Type Description Example


Syntax Error Errors due to incorrect C# int x = "hello";
syntax
Program runs but gives wrong if (x > 10) { x--; } (Incorrect
Logical Error
output. condition)
Runtime Error Errors that occur while the Division by zero, null reference,
program runs file not found
Exception Handling in C#

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.

Understanding Try, Catch, Finally, and Throw

4.1 Try-Catch-Finally Statement

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#

Debugging is the process of identifying and fixing errors in a program.

Common Debugging Techniques

Breakpoints: Pause execution at a specific line to inspect values.


Step Over (F10): Execute one line at a time, skipping method calls.
Step Into (F11): Execute one line at a time, going inside method calls.
Watch Window: Track variable values during execution.
Immediate Window: Execute expressions while debugging.

You might also like