C# is an object-oriented programming language created by Microsoft for their .NET framework. It was created as their answer to C++ (the # represents four +s in a 2x2 grid and sharp meaning higher in pitch) but it is often compared to Java since they both have similar syntax and run on virtual machines. In recent years, C# has become a very common language for gamedev in both indie and AAA video games.
Pros and Cons
Pros
- Widely used in the game industry
- Easier to learn and use compared to C++
- Compatible with C/C++ via DLLs
- Wide amount of frameworks and engines
- Limited functional-programming support
- Portable - Runs on any platform that .NET/Mono supports
- Supports GOTO control flow unlike Java
- Supports multi-dimensional arrays unlike Java
- Supports structs in addition to classes
- Supports pointers unlike Java
- Supports multiple classes in the same source file
- Supports in-line assembly
- Built in XML parser
Cons
- Practically forces you to use an IDE for large projects due to lack of import/include statements
- No multiple inheritance for classes
- Being a virtual machine based language means that programs will run slower than if they were native applications
- No way to compile to native applications
- Despite there being open source implementations of C# like Mono and open source frameworks like MonoGame. C# is pretty much frowned upon in the FLOSS community due to concerns with patents from Microsoft.
.NET vs Mono
C# programs run on a virtual machine. The virtual machine that C# was intended for was the .NET runtime by Microsoft. Thus it is only targeted at Microsoft platforms (except for .NET Core which is licensed under MIT). However, an open source implementation of .NET exists called Mono. Mono tries to replicate all the functionalities of the .NET framework without patent infringement and is cross-platform. For the most part, .NET and Mono applications are compatible with each other, so you never really need to worry about it, but this is still something worth pointing out.
Supported tools
Frameworks
- MonoGame
- SDL.NET - C# wrapper for SDL
- SFML.NET Official C# binding for SFML
- OGRE3D - w/a C# wrapper formerly known as MOGRE
Engines
- Unity
- Xenko
- Godot - C# support added in v3
- Atomic Game Engine
Resources
Docs
- MSDN C# Docs - Official MS C# docs. Reference page.
- C# Docs - Robust, and to the point docs with lots of information. Reference page.
- Dot Net Perls - Good C# docs with things like example implementations, performance benchmarks, and usage cases.
Tutorials
- TutorialsPoint
- RB Whitaker's C# Tutorials
- LearnCS.org - Has an online compiler
- C# Tutorial - Tutlane
IDEs
- MonoDevelop
- SharpDevelop
- VisualStudio
- VisualStudio Code
- OmniSharp - C# IDE plugin for Vim
- Jetbrains Rider - PAID - Free for 30day trial period, trial resets per update, they put out updates every month (infinite trial). FREE for students (uni/college/etc).
Compilers
Online C# Coding/Compile
- C# Pad - Fast, simple format, and has autocomplete.
- .Net Fiddle - Fast, full format, has autocomplete, and can import nuget packages.
Libraries/NuGet Packages
- JSON.NET - JSON parser for C#
- Curses Sharp - C# wrapper for the curses library
- Nlua - C# library that allows you to run Lua scripts in your programs
Sample code
Hello World
using System; // Namespace for essential datatypes
namespace Program {
class MainClass {
public static void Main(string[] args) {
Console.WriteLine("Hello, world!");
}
}
}
Fizzbuzz
using System;
namespace Program {
class MainClass {
public static void Main(string[] args) {
for (int i = 0; i <= 100; i++) {
if ((i % 3 == 0) && (i % 5 == 0)) {
Console.WriteLine("fizzbuzz");
}
else if (i % 3 == 0) {
Console.WriteLine("fizz");
}
else if (i % 5 == 0) {
Console.WriteLine("buzz");
}
else {
Console.WriteLine(i);
}
}
}
}
}