Introduction to C#

A step by step quide to the first C# programs.

The basic structure of a C# program using the Visual Studio IDE is the following:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace CSharpTutorials {
    class Program{
        static void Main(string[] args){

            // ...your program is here...

        }
    }
}

Output instructions

static void Main (string[] args) {
    Console.WriteLine("Hello World!");
    Console.ReadLine();
}

The line Console.ReadLine(); is needed to keep the terminal alive.

Variables

static void Main (string[] args) {
    string name = "John";
    Console.WriteLine("Hello " + name);
    int age = 19;
    Console.WriteLine ("Your age is " + age)
    Console.ReadLine();
}

Data types in C#

// data types
string phrase = "Hello world";
char letter = 'a';
int number = 42;
double tax = 0.24;

//number to string
string numero = number.ToString();
Console.WriteLine(numero); // output: 42

// string to int or double
string input = Console.ReadLine();
int x = int.Parse(input);
Console.WriteLine(x+1);

string input2 = Console.ReadLine(); 
double d = double.Parse(input2); 
Console.WriteLine(d);

//int to string
int y = 42;
Console.WriteLine(y.ToString());