Online C# Compiler – Instantly Run C# Code in Your Browser

Run and test C# code online with our browser-based C# compiler. Perfect for learning .NET syntax, experimenting with ideas, or quickly prototyping C# applications.

🚀 1 total executions (1 this month)

📚 The best C# courses you should try

Loading...

💡 C# Basics Guide for Beginners

1. Declaring Variables and Constants

C# uses strongly-typed variable declarations. Use const for compile-time constants and readonly for runtime constants.

int age = 30;
double pi = 3.14159;
char grade = 'A';
string name = "Alice";
bool isActive = true;

// Constants
const int MaxUsers = 100;
const string Company = "CodeUtility";

2. Conditionals (if / switch)

Use if, else if, and switch for control flow.

int x = 2;
if (x == 1)
{
    Console.WriteLine("One");
}
else if (x == 2)
{
    Console.WriteLine("Two");
}
else
{
    Console.WriteLine("Other");
}

switch (x)
{
    case 1:
        Console.WriteLine("One");
        break;
    case 2:
        Console.WriteLine("Two");
        break;
    default:
        Console.WriteLine("Other");
        break;
}

3. Loops

C# supports for, while, and foreach loops.

for (int i = 0; i < 3; i++)
{
    Console.WriteLine(i);
}

int n = 3;
while (n > 0)
{
    Console.WriteLine(n);
    n--;
}

4. Arrays

Arrays store fixed-size collections of elements of the same type.

int[] numbers = { 10, 20, 30 };
Console.WriteLine(numbers[1]);

5. List Manipulation

Use List<T> for dynamic collections.

List<int> nums = new List<int> { 1, 2, 3 };
nums.Add(4);
nums.Remove(2);

foreach (int n in nums)
{
    Console.Write(n + " ");
}

6. Console Input/Output

Use Console.WriteLine and Console.ReadLine for basic I/O.

Console.Write("Enter your name: ");
string name = Console.ReadLine();
Console.WriteLine($"Hello, {name}!");

7. Functions

Define methods using a return type, name, and parameters.

int Add(int a, int b)
{
    return a + b;
}

Console.WriteLine(Add(3, 4));

8. Dictionaries

Dictionary<TKey, TValue> stores key-value pairs.

Dictionary<string, int> ages = new Dictionary<string, int>();
ages["Alice"] = 30;
Console.WriteLine(ages["Alice"]);

9. Exception Handling

Handle runtime errors using try, catch, and finally.

try
{
    throw new Exception("Something went wrong");
}
catch (Exception ex)
{
    Console.WriteLine(ex.Message);
}

10. File I/O

Use File and StreamReader/StreamWriter for file operations.

File.WriteAllText("file.txt", "Hello File");
string text = File.ReadAllText("file.txt");
Console.WriteLine(text);

11. String Manipulation

C# strings support methods like Length, Substring, Contains.

string message = "Hello World";
Console.WriteLine(message.Length);
Console.WriteLine(message.Substring(0, 5));
Console.WriteLine(message.Contains("World"));

12. Classes & Objects

C# supports object-oriented programming through classes and objects.

class Person
{
    public string Name;
    public Person(string name) => Name = name;
    public void Greet() => Console.WriteLine($"Hi, I'm {Name}");
}

Person p = new Person("Alice");
p.Greet();