Skip to main content

Online C# Compiler

Program.cs
Ready

About C#

C# is Microsoft's object-oriented language for .NET, used for back-end services, desktop apps and Unity games.

How to take input in C#

Console.ReadLine() returns one line as a string. Convert it with int.Parse, or split it for several values.

int n = int.Parse(Console.ReadLine());
string[] parts = Console.ReadLine().Split(' ');
int[] arr = parts.Select(int.Parse).ToArray();   // needs using System.Linq;

C# programs to try

Copy one into the editor above, put any input in the Input tab and press Run.

Even or odd

using System;

class Program {
    static void Main() {
        int n = int.Parse(Console.ReadLine());
        Console.WriteLine(n % 2 == 0 ? "Even" : "Odd");
    }
}

Input: 17

Fibonacci series

using System;

class Program {
    static void Main() {
        int n = int.Parse(Console.ReadLine());
        long a = 0, b = 1;
        for (int i = 0; i < n; i++) {
            Console.Write(a + " ");
            (a, b) = (b, a + b);
        }
        Console.WriteLine();
    }
}

Input: 10

Sort a list with LINQ

using System;
using System.Linq;

class Program {
    static void Main() {
        var nums = Console.ReadLine().Split(' ').Select(int.Parse);
        Console.WriteLine(string.Join(", ", nums.OrderByDescending(x => x)));
    }
}

Input: 42 7 19 3 25

Common C# errors and how to fix them

System.FormatException
int.Parse got text that isn't a number, or several numbers on one line. Split the line first.
System.NullReferenceException
Console.ReadLine() returned null because there was no more input. Add the missing lines to the Input tab.
error CS1002: ; expected
A statement is missing its semicolon near the line in the message.

Frequently asked questions

Is this online C# compiler free?

Yes. Write and run C# code in your browser for free, with nothing to install and no setup.

How do I give input to my C# program?

Type it in the Input tab before pressing Run and read each line with Console.ReadLine().

Can I use LINQ?

Yes. Add using System.Linq; at the top to use Select, Where, OrderBy and the rest.

Does it work on my phone?

Yes. The editor, input and output work in a mobile browser, so you can practise C# without a laptop.