C Sharp
C Sharp
---
2. **Verify C# Installation**:
After installing, you can verify if C# is working by running this in your
terminal/command prompt:
```bash
dotnet --version
```
This will show the installed version of the .NET SDK.
---
**Code:**
```csharp
using System;
class Program
{
static void Main(string[] args)
{
Console.WriteLine("Hello, World!");
}
}
```
#### Explanation:
- **`using System;`**: This line imports the `System` namespace, which contains the
`Console` class.
- **`class Program`**: Defines a class named `Program`. In C#, everything is
contained within classes or structs.
- **`static void Main(string[] args)`**: This is the entry point of a C#
application. `Main()` is where execution starts. `void` means this function doesn't
return anything.
- **`Console.WriteLine("Hello, World!");`**: This writes text to the console.
To run the program:
1. Save the code in a file named `Program.cs`.
2. Open the terminal/command prompt, navigate to the directory where the file is
saved, and run:
```bash
dotnet new console -n HelloWorldApp
cd HelloWorldApp
dotnet run
```
This should print `Hello, World!` to the console.
---
C# is statically typed, meaning you need to declare the type of each variable.
**Example:**
```csharp
using System;
class Program
{
static void Main(string[] args)
{
int age = 25;
double height = 5.9;
string name = "Alice";
bool isStudent = true;
You can also use the `var` keyword when you want the compiler to infer the type
automatically.
```csharp
var name = "Alice"; // The type is inferred as string
var age = 25; // The type is inferred as int
```
---
### 4. **Control Flow (if/else, loops)**
```csharp
int age = 18;
#### Loops:
- **For Loop**:
```csharp
for (int i = 0; i < 5; i++)
{
Console.WriteLine(i);
}
```
- **While Loop**:
```csharp
int i = 0;
while (i < 5)
{
Console.WriteLine(i);
i++;
}
```
```csharp
int day = 3;
switch (day)
{
case 1:
Console.WriteLine("Monday");
break;
case 2:
Console.WriteLine("Tuesday");
break;
case 3:
Console.WriteLine("Wednesday");
break;
default:
Console.WriteLine("Invalid day");
break;
}
```
---
### 5. **Functions/Methods**
Functions in C# are called methods and are used to encapsulate reusable logic.
**Defining a Method:**
```csharp
using System;
class Program
{
static void Greet(string name)
{
Console.WriteLine($"Hello, {name}!");
}
- **`static`**: Indicates that the method belongs to the class, not an instance of
the class.
- **`void`**: Specifies that the method doesn’t return a value.
- **`string name`**: A parameter that the method accepts.
You can return a value from a method using a return type other than `void`.
```csharp
static int Add(int a, int b)
{
return a + b;
}
```
---
#### Arrays:
Arrays are used to store multiple values of the same type.
```csharp
int[] numbers = { 1, 2, 3, 4, 5 };
Console.WriteLine(numbers[0]); // Output: 1
```
```csharp
using System;
using System.Collections.Generic;
class Program
{
static void Main(string[] args)
{
List<string> fruits = new List<string>();
fruits.Add("Apple");
fruits.Add("Banana");
---
```csharp
using System;
class Person
{
public string Name;
public int Age;
class Program
{
static void Main(string[] args)
{
Person person = new Person(); // Create an instance of the class
person.Name = "Alice";
person.Age = 25;
---
```csharp
try
{
int result = 10 / 0;
}
catch (DivideByZeroException ex)
{
Console.WriteLine("Error: " + ex.Message);
}
finally
{
Console.WriteLine("This will always run.");
}
```
---
```csharp
using System;
using System.Linq;
using System.Collections.Generic;
class Program
{
static void Main(string[] args)
{
List<int> numbers = new List<int> { 1, 2, 3, 4, 5 };
---