Getting Started with C#


1. Set Up Your Environment

To start programming in C#, you’ll need the following:

Download and Install .NET SDK

The .NET SDK includes the tools and libraries required to build and run C# programs.

Choose an IDE (Integrated Development Environment)

  • Visual Studio (recommended): The most popular IDE for C# development.
    • Download it from Visual Studio’s official website.
    • During installation, select the “ASP.NET and web development” or “.NET desktop development” workload depending on your project type.
  • Visual Studio Code: A lightweight, cross-platform code editor.
    • Install the C# extension from the extensions marketplace.

Verify Installation

To check if .NET is installed correctly, open a terminal or command prompt and type:

dotnet --version

You should see the installed version of .NET.


2. Your First C# Program

Let’s create and run a simple “Hello, World!” program.

Using .NET CLI

  1. Open a terminal or command prompt.
  2. Create a new project: dotnet new console -n HelloWorld
    • This creates a folder named HelloWorld with all the files needed for a simple console app.
  3. Navigate to the project folder: cd HelloWorld
  4. Open the Program.cs file. It should look like this: using System; class Program { static void Main(string[] args) { Console.WriteLine("Hello, World!"); } }
  5. Run the program: dotnet run
  6. Output: Hello, World!

3. Basic Syntax

Here’s an overview of C#’s fundamental building blocks:

Variables and Data Types

string name = "John";
int age = 25;
double height = 5.9;
bool isStudent = true;

Control Statements

  • If-Else Statement: if (age > 18) { Console.WriteLine("Adult"); } else { Console.WriteLine("Minor"); }
  • Loops: for (int i = 0; i < 5; i++) { Console.WriteLine(i); }

Methods

class Program
{
    static void Main(string[] args)
    {
        Greet("Alice");
    }

    static void Greet(string name)
    {
        Console.WriteLine($"Hello, {name}!");
    }
}

4. Key Features of C#

  • Object-Oriented Programming (OOP): C# supports classes, inheritance, polymorphism, and interfaces. class Person { public string Name { get; set; } public void Speak() { Console.WriteLine($"Hi, I am {Name}"); } }
  • LINQ (Language Integrated Query): Makes querying data collections easy. var numbers = new int[] { 1, 2, 3, 4, 5 }; var evenNumbers = numbers.Where(n => n % 2 == 0); foreach (var num in evenNumbers) { Console.WriteLine(num); }
  • Asynchronous Programming: C# supports async/await for concurrency. async Task FetchData() { Console.WriteLine("Fetching data..."); await Task.Delay(1000); // Simulate a delay Console.WriteLine("Data fetched!"); }

5. Where to Go from Here

Learn C# Fundamentals

  • Explore topics like arrays, collections, exception handling, and file I/O.
  • Practice with small projects like a calculator, a to-do list app, or a number guessing game.

Dive Into Frameworks

  • ASP.NET Core: For building web applications.
  • Unity: For game development.
  • WinForms/WPF: For desktop applications.

Resources for Learning

  • Books:
    • C# 10 and .NET 6 – Modern Cross-Platform Development by Mark J. Price
    • Head First C#: A Learner’s Guide to Real-World Programming by Jennifer Greene and Andrew Stellman

Getting Started with C#


1. Set Up Your Environment

To start programming in C#, you’ll need the following:

Download and Install .NET SDK

The .NET SDK includes the tools and libraries required to build and run C# programs.

Choose an IDE (Integrated Development Environment)

  • Visual Studio (recommended): The most popular IDE for C# development.
    • Download it from Visual Studio’s official website.
    • During installation, select the “ASP.NET and web development” or “.NET desktop development” workload depending on your project type.
  • Visual Studio Code: A lightweight, cross-platform code editor.
    • Install the C# extension from the extensions marketplace.

Verify Installation

To check if .NET is installed correctly, open a terminal or command prompt and type:

dotnet --version

You should see the installed version of .NET.


2. Your First C# Program

Let’s create and run a simple “Hello, World!” program.

Using .NET CLI

  1. Open a terminal or command prompt.
  2. Create a new project: dotnet new console -n HelloWorld
    • This creates a folder named HelloWorld with all the files needed for a simple console app.
  3. Navigate to the project folder: cd HelloWorld
  4. Open the Program.cs file. It should look like this: using System; class Program { static void Main(string[] args) { Console.WriteLine("Hello, World!"); } }
  5. Run the program: dotnet run
  6. Output: Hello, World!

3. Basic Syntax

Here’s an overview of C#’s fundamental building blocks:

Variables and Data Types

string name = "John";
int age = 25;
double height = 5.9;
bool isStudent = true;

Control Statements

  • If-Else Statement: if (age > 18) { Console.WriteLine("Adult"); } else { Console.WriteLine("Minor"); }
  • Loops: for (int i = 0; i < 5; i++) { Console.WriteLine(i); }

Methods

class Program
{
    static void Main(string[] args)
    {
        Greet("Alice");
    }

    static void Greet(string name)
    {
        Console.WriteLine($"Hello, {name}!");
    }
}

4. Key Features of C#

  • Object-Oriented Programming (OOP): C# supports classes, inheritance, polymorphism, and interfaces. class Person { public string Name { get; set; } public void Speak() { Console.WriteLine($"Hi, I am {Name}"); } }
  • LINQ (Language Integrated Query): Makes querying data collections easy. var numbers = new int[] { 1, 2, 3, 4, 5 }; var evenNumbers = numbers.Where(n => n % 2 == 0); foreach (var num in evenNumbers) { Console.WriteLine(num); }
  • Asynchronous Programming: C# supports async/await for concurrency. async Task FetchData() { Console.WriteLine("Fetching data..."); await Task.Delay(1000); // Simulate a delay Console.WriteLine("Data fetched!"); }

5. Where to Go from Here

Learn C# Fundamentals

  • Explore topics like arrays, collections, exception handling, and file I/O.
  • Practice with small projects like a calculator, a to-do list app, or a number guessing game.

Dive Into Frameworks

  • ASP.NET Core: For building web applications.
  • Unity: For game development.
  • WinForms/WPF: For desktop applications.

Resources for Learning

  • Books:
    • C# 10 and .NET 6 – Modern Cross-Platform Development by Mark J. Price
    • Head First C#: A Learner’s Guide to Real-World Programming by Jennifer Greene and Andrew Stellman