Introduction to C#

Tutorial 1 of 5

Introduction to C

1. Introduction

Welcome to this beginner-friendly tutorial on C#. Our goal is to give you an understanding of the basics of C# programming. We will cover various key concepts and syntax, providing you a solid foundation to build upon.

By the end of this tutorial, you will learn:
- Basic Syntax of C#
- Data Types and Variables
- Control Structures
- Object-Oriented Programming (OOP) concepts

Prerequisites:
While no prior knowledge of C# is necessary, basic understanding of programming concepts like variables, loops, and functions would be beneficial.

2. Step-by-Step Guide

C# Syntax

C# is a statically-typed language, which means we need to declare the type of a variable when we declare it.

int number = 10; // declaring an int variable

Data Types and Variables

C# includes several different types of data including integers (int), floating point numbers (float, double), characters (char), and strings (string).

int i = 10; // Integer
double d = 20.5; // Double
char c = 'a'; // Character
string s = "hello"; // String

Control Structures

Control structures like if, else, for, and while are fundamental to any programming language, including C#.

// If else structure
if (i > 0)
{
    Console.WriteLine("Positive number");
}
else
{
    Console.WriteLine("Non-positive number");
}

// For loop
for (int j = 0; j < 5; j++)
{
    Console.WriteLine(j);
}

// While loop
while (i > 0)
{
    Console.WriteLine(i);
    i--;
}

Object-Oriented Programming (OOP)

C# is an object-oriented language, which means it allows us to encapsulate data and methods into objects.

class HelloClass
{
    private string message = "Hello, World!";

    public void DisplayMessage()
    {
        Console.WriteLine(message);
    }
}

HelloClass hello = new HelloClass();
hello.DisplayMessage(); // Outputs: Hello, World!

3. Code Examples

Example 1: Hello, World!

using System;

class Program
{
    static void Main()
    {
        Console.WriteLine("Hello, World!");
    }
}
// This will output: Hello, World!

Example 2: Data Types and Variables

using System;

class Program
{
    static void Main()
    {
        int number = 10; // Integer
        Console.WriteLine(number); // Outputs: 10
    }
}

Example 3: Control Structures

using System;

class Program
{
    static void Main()
    {
        for (int i = 0; i < 5; i++) 
        {
            Console.WriteLine(i); // Outputs: 0, 1, 2, 3, 4
        }
    }
}

4. Summary

In this tutorial, we have covered the basics of C# including syntax, data types, control structures, and OOP principles. These concepts form the foundation of C# programming.

To continue your learning journey, try creating simple applications, explore more about C# classes, interfaces, and delve into more advanced topics like LINQ and async programming.

5. Practice Exercises

Exercise 1: Write a program that asks the user for their name and greets them with their name.

Solution:

using System;

class Program
{
    static void Main()
    {
        Console.Write("Enter your name: ");
        string name = Console.ReadLine();
        Console.WriteLine("Hello, " + name);
    }
}

Exercise 2: Write a program that prints the sum of two numbers.

Solution:

using System;

class Program
{
    static void Main()
    {
        int num1 = 10;
        int num2 = 20;
        int sum = num1 + num2;
        Console.WriteLine("The sum is " + sum); // Outputs: The sum is 30
    }
}

Exercise 3: Write a program that prints the multiplication table of a given number.

Solution:

using System;

class Program
{
    static void Main()
    {
        int num = 5;
        for (int i = 1; i <= 10; i++)
        {
            Console.WriteLine(num + " * " + i + " = " + num * i);
        }
    }
}