C# from Basics to Mastery — Part 1
Starting C# with classes, properties, and collections
The articles written on this blog were created with the goal of teaching. Also, the information in the article may be incorrect, so please be careful.
C# is a general-purpose programming language that runs in the .NET environment. The syntax is similar to the C series, but the standard library and runtime provide a lot of help with functions such as memory management, strings, and collections.
Express types and variables clearly
int level = 3;
float speed = 5.5f;
string playerName = "Rose";
bool isAlive = true;
C# is a statically typed language, so the types of variables and values are checked at compile time. When you use var, the type is inferred from the value on the right, but the variable's type does not change during execution.
var scores = new List<int> { 10, 20, 30 };
// scores is of type List<int>.
Classes bind data and behavior
public class Player
{
public string Name { get; }
public int Health { get; private set; } = 100;
public Player(string name)
{
Name = name;
}
public void TakeDamage(int amount)
{
Health = Math.Max(0, Health - amount);
}
}
Properties are read and written like fields, but you can specify access levels and validation rules. In the code above, outside code can read Health, but it cannot change the value directly.
Collection and Repetition
List<T> stores multiple ordered values, while Dictionary<TKey, TValue> finds a value by its key.
var inventory = new Dictionary<string, int>
{
["Potion"] = 3,
["Key"] = 1
};
foreach (var item in inventory)
{
Console.WriteLine($"{item.Key}: {item.Value}");
}
For reference-type objects in C#, the garbage collector cleans up unused memory. However, the lifetime of external resources such as files and network connections must be managed explicitly with using or Dispose.
When first learning C#, it is better to connect types, conditionals, loops, classes, and collections with a small console program rather than memorizing the entire syntax. Unity is easy to understand if you think of it as adding the game engine life cycle and component structure on top of it.