Welcome to the C# Tutorial! This guide will help you get started with one of the most popular programming languages for .NET applications. Whether you're a beginner or looking to expand your knowledge, this tutorial is designed to cover the basics and beyond.

Getting Started

Before you dive in, make sure you have the following prerequisites:

  • A computer with a Windows, macOS, or Linux operating system.
  • The .NET SDK installed on your machine.
  • A code editor or IDE, such as Visual Studio or Visual Studio Code.

Basic Syntax

Here's a simple example of C# code that prints "Hello, World!" to the console:

using System;

class Program
{
    static void Main()
    {
        Console.WriteLine("Hello, World!");
    }
}

Variables and Data Types

In C#, variables are used to store data. Here are some common data types:

  • int: Integer values.
  • float: Floating-point numbers.
  • string: Text values.
  • bool: Boolean values (true or false).

Here's an example of how to declare and use variables:

int age = 25;
float pi = 3.14f;
string name = "John Doe";
bool isStudent = true;

Control Structures

Control structures allow you to control the flow of your program. Here are some common control structures:

  • if statements: Execute a block of code if a condition is true.
  • for loops: Execute a block of code a specific number of times.
  • while loops: Execute a block of code as long as a condition is true.

Here's an example of an if statement:

if (age > 18)
{
    Console.WriteLine("You are an adult.");
}

Functions

Functions are reusable blocks of code that perform a specific task. Here's an example of a function that calculates the square of a number:

public static int Square(int number)
{
    return number * number;
}

Object-Oriented Programming

C# is an object-oriented programming language. This means you can create classes and objects to represent real-world entities.

Here's an example of a simple class:

public class Person
{
    public string Name { get; set; }
    public int Age { get; set; }

    public Person(string name, int age)
    {
        Name = name;
        Age = age;
    }

    public void Greet()
    {
        Console.WriteLine($"Hello, my name is {Name} and I am {Age} years old.");
    }
}

Further Reading

For more information on C#, we recommend checking out the following resources:

Return to the tutorials page