Skip to content
← Back to Community
C# Tutorial
Profile icon
has Hacker Plan

Learn C# Fast!


Have you wanted to learn a modern, fast, compiled language that is general-purpose and is used almost everywhere? That's C# for you! It's used in Unity, the web, webassembly, desktop applications, and plays well with C++ and C. There is no reason something can't be accomplished with C#!

After this tutorial, you should have enough C# knowledge to do most things.


C# is a multi-paradigm language, it has object-oriented, and functional features. We will be focusing on the object-oriented part, because functions are math, and I didn't like that. Each C# file gets compiled. They either first get linked with other files, or they are compiled on the standalone, like this:

main.cs decl.cs --> main.exe

The compiler is usually called roslyn, or .NET Complier Platform. Replit uses a compiler similar to it, but it targets mono, which is one way to run .exe files on linux systems.


All classes should LookLikeThis. Notice the PascalCase.
All methods should AlsoLookLikeThis, and this is the same for AllProperties (more on these things later). All local variables should lookLikeThis. Read more here.

Starting Program

Here is a starting boilerplate.

class MainClass { public static void Main(string[] args) { // code } }

// code is a comment. Another type of comment is the multiline comment:

/* multi lines */

The entry point is in Main, and this method must be public and static.


If you know some C++, C# is super similar to it. Each program starts with a class. Classes are defined like this:

class Name { // ... more code ... }

Inside the brackets, you can add methods or properties. Methods and properties can either be static or instance methods. Another key feature is accessibility, which we will talk about later.

Methods look like this:

void Method() { // code }

The void can also be replaced with other types as well. Types will be talked about later.

Instance methods can be used after creating a class, while static classes don't. Here is a short demo:

MyClass instance = new MyClass(); instance.InstanceMethod(); MyClass.StaticMethod();

Methods can be set static using the static keyword.

Variables can be set using two ways. The first, you specify the type (the class name), and then you set the value. If you don't set a value, you'll get a CS0165, where you are trying to use an unassigned local variable. Here are all the native types (they are represented by the computer and are not provided as abstractions by C#):

intAn integer
uintAn unsigned integer (always positive)
longAn integer but its BIG (long)
ulongA BIG integer, it can be as big as 18,446,744,073,709,551,615
floatRepresents a decimal, but only to 6-9 digits.
doubleRepresents a decimal, but only to 15-17 digits.
decimalRepresents a decimal, but only to 28-29 digits.
charA character, only one letter.
stringA string represents some text, it contains multiple chars.
boolA boolean is either true or false

You can also define your own custom types, through classes!

int a; a = 5; // good! int b; // bad! int c = 5; // good! var d = 5; // good! var e; // bad! All `var`-created variables must be immediately followed by a value.

You can also make variables constants. They are faster because they don't change, but clearly sometimes change is needed. All things should be set to constant by default, that is good practice.

const int a = 5;

Properties and Methods

Classes have properties and methods. These methods can be public and private. Public properties can be accessed by outside sources, while private properties can only be accessed from inside the class. Here is how you can tell what can access a private property.

class MyClass { private int a = 5; // accessible! } // inaccessible!

All properties and methods are private by default, but its best to be explicit about this.

Remember the static from before? Now we can combine them!

class MyClass { private static void Method() {} }

The order is like this <private/public> <static> <returntype> <name>. The accessor must come first.

Methods can also take in some input, like this:

class MyClass { private static void Method(int arg) {} }

They are called arguments.

Now, when you call functions, you call them like this:

Add(1, 2);

More on classes

We have talked syntax, but not how to use classes yet! Classes can be made an instance using something called the constructor.

class MyClass { public int MyProp; public MyClass() { MyProp = 5; } }

don't forget to use the public keyword, or else it won't be able to be initialized!

As you can see, all the variables by default are exposed to the method scope, but sometimes there are clashes, and they are resolved with the this keyword:

class MyClass { public int Prop; public MyClass(int Prop) { this.Prop = Prop; } }

The this keyword refers to this instance.


Many functions, and many standard library functions are put inside namespaces. in replit, there is only one, it's called System. Namespaces can include namespaces, and they can nest forever.

namespace MyNamespace { class MyClass { public static int a = 5; } } MyNamespace.MyClass.a; // 5

Do note that namespaces must only contain classes, you cannot treat it like a block.

You can also bring namespaces out. Something like this:

namespace A { namespace B { class C {} } }

You can do:

using A.B; C

Now C is brought out to the global scope!


An easy way to represent things that can only take certain forms are enumerables. They are represented with the keyword enum:

enum Users { Coder100, Coderbot100, RayhanADev, Others }

They can also have custom integer types, and implicitly they look like this:

enum Users { Coder100 = 0, Coderbot100 = 1, RayhanADev = 2, Others = 3 }

Do note you can not just interchangeably use this with the int type! You will have to cast it.

Type Conversions

Types can change from one type to another, using two methods, either through methods and functions, or casting. Casting is more interesting and more common than through methods.

They look like this:


Remember enums from last time? You can cast them into ints, like this:

enum A { B, C } int val = (int)A.B;


Sometimes you don't want to use classes, and you want to use sturcts. This is mostly when you just want a data type that stores things. (In js, this is called and object) You can use a struct for this.

struct MyStruct { public int A = 5; public int B; } MyStruct s = new MyStruct(); s.B = 3; // s.A = 5, s.B = 3

Confused what the difference is between structs and classes? Read this.


The standard library is very very very large. The most commonly used is System.Console class.

using System; class MainClass { public static void Main(string[] args) { Console.WriteLine("Hi with a newline!"); Console.Write("Hi without a newline!"); } }

More methods

You can view the whole System documentation here


That's it! C# is a really really big language, and these are the basics to help you get started with coding in C#! Hopefully it was helpful :)

Profile icon
Profile icon
Profile icon
Profile icon
Profile icon
Profile icon

I'm a bit confused about how to run programs from separate files. How do I do this?

Profile icon

you can export some specific static method that you later call @MrPynap

Profile icon

I dont get it

Profile icon


hi im just curious on how to make a chart. so can you teach me?

Profile icon


Profile icon

hehe hi twin

Profile icon


Profile icon


Profile icon

There is no reason something can't be accomplished with C#!

can you make a web server

Profile icon

yeah ofc @ch1ck3n

Profile icon

@Coder100 ok show me

Profile icon
Profile icon

or @ch1ck3n

Profile icon

@Coder100 I'm sold. c# is the best!

but dogescript is better

Profile icon

but what's dogescript?

Profile icon

@Coder100 javascript for the moon 🚀
a glorified version of javascript

Profile icon
Profile icon

thanks so much

Profile icon
Profile icon

@Coder100 I have one question, I have seen and forked repls with case (ex case "1") and stuff like that, but whenever I try it, it doesn't work. Can you please help

Profile icon

sure, send repl @Bunnytoes

Profile icon

@Coder100 this is the forked version(no changes and I don't know who made it)

Profile icon

@Coder100 repl I sent was made by @dungeonmaster and I forked it