Lecture#4 C#.NET Switch Statement with Console Application using Visual Studio 2022 in Urdu/ Hindi

Опубликовано: 03 Апрель 2026
на канале: ST Learning Training Institute
15
1

The C#.NET `switch` statement allows you to execute one of many code blocks based on the value of a variable or expression. It is a control flow statement that is useful for making decisions in your program. Below is a short description of how to implement a `switch` statement within a simple console application using Visual Studio 2022.

Steps to Create a Console Application with a Switch Statement in Visual Studio 2022

1. **Open Visual Studio 2022**:
Start Visual Studio 2022 and create a new project.

2. **Create a New Console Application**:
Select `Create a new project`.
Choose `Console App (.NET Core)` or `Console App (.NET Framework)` based on your requirement.
Click `Next`, name your project (e.g., `SwitchStatementDemo`), choose a location, and click `Create`.

3. **Write the Switch Statement**:
In the `Program.cs` file, modify the `Main` method to include a switch statement. Here's an example:

```csharp
using System;

namespace SwitchStatementDemo
{
class Program
{
static void Main(string[] args)
{
Console.WriteLine("Enter a number between 1 and 5:");
int number = Convert.ToInt32(Console.ReadLine());

switch (number)
{
case 1:
Console.WriteLine("You entered One.");
break;
case 2:
Console.WriteLine("You entered Two.");
break;
case 3:
Console.WriteLine("You entered Three.");
break;
case 4:
Console.WriteLine("You entered Four.");
break;
case 5:
Console.WriteLine("You entered Five.");
break;
default:
Console.WriteLine("The number is not between 1 and 5.");
break;
}
}
}
}
```

4. **Run the Application**:
Press `Ctrl + F5` to run the application without debugging.
Enter a number between 1 and 5 when prompted, and observe the corresponding output message.

Explanation of the Code

**Using Directive**:
`using System;` is necessary to access basic classes like `Console`.

**Namespace and Class**:
`namespace SwitchStatementDemo` and `class Program` define the structure of your application.

**Main Method**:
`static void Main(string[] args)` is the entry point of the application.

**Reading Input**:
`Console.ReadLine()` is used to read user input from the console.
`Convert.ToInt32(Console.ReadLine())` converts the input string to an integer.

**Switch Statement**:
The `switch (number)` statement evaluates the value of `number`.
`case` labels specify the possible values of `number` and execute the corresponding code block.
`break;` statements terminate the case blocks to prevent fall-through.
`default:` is the fallback case if none of the specified cases match.

This example demonstrates a basic usage of the `switch` statement in a C#.NET console application, allowing you to handle multiple conditions based on user input efficiently.