Learn to Code via Tutorials on Repl.it!

← Back to all posts
C# Tutorial
h
Coder100 (18146)

Learn C# Fast!

@Bunnytoes

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.

Introduction

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.

Conventions

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.

Syntax

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#):

TypeDesc
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.

Namespaces

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!

Enumerables

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:

(type)(expression)

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

enum A { B, C }

int val = (int)A.B;

Structs

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.

Stdlib

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

Closing

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 :)

Comments
hotnewtop
[deleted]
<h1> I dont get it </h1>
[deleted]

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

[deleted]

hi

[deleted]

hehe hi twin

[deleted]

||wer

ch1ck3n (2055)

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

can you make a web server

ch1ck3n (2055)

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

but dogescript is better

Coder100 (18146)

ayyy
but what's dogescript?
@ch1ck3n

ch1ck3n (2055)

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

Bunnytoes (142)

@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

Bunnytoes (142)

@Coder100 this is the forked version(no changes and I don't know who made it) https://replit.com/@Bunnytoes/calculator#main.cs

Bunnytoes (142)

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