C# for Newbies
Stuff I wish I knew when I started
General Syntax
Microsoft encourages the use of var. They’ve gone back and forth on this a few times, but I think they’ve settled on var for good now.
Know null conditional operators and the null coalescing operator too.
Classes are nullable by default; primitives are not. You can make primitives nullable using the ?.
bool? nullableBool;
If you don’t want your classes to be nullable by default, you can set that option in the .csproj file.
<Nullable>enable</Nullable>
You can set default parameters on methods by just using an equal sign:
void add(int i, int j = 1);
Don’t use + to combine strings; it’s gross. Don’t use composite strings either. Use interpolation. See examples here.
To convert from string to something, use .TryParse(). It returns a bool and has an out parameter that you must pass by reference for the parser to populate.
var a = "1";
if (int.TryParse(a, out int number))
return number;
The DateTime and TimeSpan classes are amazing.
Collections
List, Dictionary (C# version of hashmaps), and other collections implement IEnumberable. It allows for the use of foreach and other things. It does not have a .Count for tracking iteration however; it’s meant to be used declaratively. Use a for loop instead for that.
Unlike most languages, Dictionary throws an exception if the key isn’t found so be sure to check with .ContainsKey(key).
Classes
Know the difference between a field and a property. See examples here.
You can instantiate properties of a class at the same time you declare the class itself. You don’t even need the parenthesis:
var dude = new Person
{
Name = "Jake"
};
Concurrency
async, await, and Task are crucial to know and very easy to use.
Actions and Functions are formalized delegates. In other words, they are classes used to construct callbacks or promises. You can make your own delegate manually if you like but it’s a really deep and complicated topic. Don’t recommend; just stick with actions or even just lambdas.
LINQ
LINQ has two “formats”: one being literal psuedoSQL and the other using lambda expressions. Naturally, I like lambdas more.
Try to stick to the OrDefault() options. They almost always return null for default (in other words, couldn’t find the thing). The non-OrDefault() options throw an exception.
EF Core
TODO: this section is very dense and technical. Need to make it friendly
The main ORM used in the .NET ecosystem is Entity Framework Core. It replaces the old Entity Framework 6. Confusingly, both are often referred to just as Entity Framework (EF). When looking up documentation, be sure you are reading about EF Core, not EF 6.
Foreign keys in Entity Framework only require the actual ID property to work, but if you want to be able to access the linked entity’s properties, you need a navigation property which is just a “virtual” of the class with the same name minus “Id.” EF will populate it automatically and track all changes.
class student
{
Guid Id;
Guid ClassId;
virtual Class class;
}
If you don’t wanna track changes, such as when only reading data from the database, use AsNoTracking().
Serialization
Newtonsoft
Nearly all json serializing is done using Newtonsoft, also called Json.NET. For years Microsoft had not written their own and, surprisingly, a lot of their official code and documentation rely on it. They have created their own now, but adoption has been very slow. Newtonsoft is still the dominate option and I realistically don’t expect that to change.
Dependency Injection
A dependency injection tool only very recently was developed by MS and it’s my favorite. Before that, you used a 3rd party one like Ninject, Autofac, or Unity (those are the big ones).
The universally accepted but not-written-down-anywhere naming convention is to use an underscore at the beginning of injected services. See examples here.
ASP.NET
Do a tutorial on creating a web API with ASP.NET. It’ll get you familiar with how controllers are set up as well as the initialization.
There’s been a few variants to how the Program.cs and Startup.cs classes work over the years. Now, the default way is called a Minimal API. It is quite nice and simple and eliminates the Startup.cs class altogether. You can of course always revert to the more complete style.
Visual Studio
TODO: explain csproj file more
Use ctrl+. in Visual Studio to get recommendations on errors or the little gray dotted underlines. It will automatically fill out your class if you didn’t completely impliment your interface, for example. TODO: .editorconfig
Advanced Topics
Reflection
Look at Type.GetProperties and GetType.