Quiz on AI Interviews Prep Live Training Corporate Training

CSharp Tutorials.

This tutorial teaches C# from basics to essential concepts with examples.


1. What is C#?

C# (C-Sharp) is a modern, object-oriented programming language developed by Microsoft. It is widely used for:

  • Desktop applications
  • Web applications (ASP.NET)
  • Game development (Unity)
  • Mobile apps

2. Setting Up C#

You can write C# using:

  • Visual Studio
  • Visual Studio Code
  • .NET SDK
dotnet new console -n MyApp
cd MyApp
dotnet run

3. Your First C# Program

using System;

class Program
{
    static void Main()
    {
        Console.WriteLine("Hello, World!");
    }
}
Every C# program starts execution from the Main() method.

4. Variables and Data Types

int age = 25;
double price = 99.99;
char grade = 'A';
string name = "John";
bool isActive = true;

5. User Input and Output

Console.Write("Enter your name: ");
string name = Console.ReadLine();
Console.WriteLine("Hello " + name);

6. Conditional Statements

if-else

int number = 10;

if (number > 0)
{
    Console.WriteLine("Positive");
}
else
{
    Console.WriteLine("Negative");
}

switch

int day = 3;

switch (day)
{
    case 1:
        Console.WriteLine("Monday");
        break;
    case 2:
        Console.WriteLine("Tuesday");
        break;
    default:
        Console.WriteLine("Other day");
        break;
}

7. Loops

for Loop

for (int i = 1; i <= 5; i++)
{
    Console.WriteLine(i);
}

while Loop

int i = 1;
while (i <= 5)
{
    Console.WriteLine(i);
    i++;
}

8. Methods (Functions)

static void Greet(string name)
{
    Console.WriteLine("Hello " + name);
}

Calling a Method

Greet("Alice");

9. Arrays

int[] numbers = { 1, 2, 3, 4, 5 };

foreach (int num in numbers)
{
    Console.WriteLine(num);
}

10. Object-Oriented Programming (OOP)

Class and Object

class Person
{
    public string Name;
    public int Age;

    public void Introduce()
    {
        Console.WriteLine("My name is " + Name);
    }
}

Person p = new Person();
p.Name = "Sarah";
p.Age = 30;
p.Introduce();

11. Constructors

class Car
{
    public string Model;

    public Car(string model)
    {
        Model = model;
    }
}

12. Inheritance

class Animal
{
    public void Eat()
    {
        Console.WriteLine("Eating...");
    }
}

class Dog : Animal
{
    public void Bark()
    {
        Console.WriteLine("Barking...");
    }
}

13. Exception Handling

try
{
    int x = int.Parse("abc");
}
catch (Exception e)
{
    Console.WriteLine("Error: " + e.Message);
}

14. Collections (List)

using System.Collections.Generic;

List names = new List();
names.Add("Alice");
names.Add("Bob");

foreach (string n in names)
{
    Console.WriteLine(n);
}

15. Conclusion

C# is powerful, flexible, and widely used across many platforms. Practice regularly and build small projects to master it.

Next Topics to Learn:

  • LINQ
  • File Handling
  • ASP.NET
  • Entity Framework
  • Unity Game Development