Types, LINQ, async/await, generics, records, nullability
Practise these as flashcards. One or two lines each; the ones you miss come back first.
Start freeA struct is a value type, meaning its data is stored directly and copied when assigned. A class is a reference type, so its data is stored on the heap and assignments copy the reference. Structs are best for small, immutable data, while classes are used for complex objects.
Boxing is the process of converting a value type, like an int, into a reference type, such as object. This happens when you assign a value type to a variable of type object or pass it as a parameter expecting an object.
You add a where clause to the method signature. For example, public void DoSomething<T>() where T : new() means T must have a parameterless constructor.
Deferred execution means that a LINQ query is not executed when it's defined, but only when you enumerate it, such as with a foreach loop or calling ToList().
IEnumerable<T> is used for in-memory collections and executes LINQ queries locally. IQueryable<T> is used for remote data sources like databases, and LINQ queries are translated, often to SQL, and executed remotely.
Async/await allows you to write asynchronous code in a way that looks synchronous, making it easier to read and maintain. It handles continuations automatically, reducing boilerplate and minimizing errors compared to manual callbacks.
Use ValueTask when the operation often completes synchronously, such as returning a cached value, to avoid allocating a new Task object. For most async APIs, Task is preferred for simplicity unless performance profiling shows a benefit.
ConfigureAwait(false) tells the awaiter not to capture the current synchronization context, which helps avoid deadlocks in UI or ASP.NET code and can improve performance in library code.
You declare an event using the event keyword and a delegate type. For example, public event Action OnChanged; and then you can subscribe with OnChanged += Handler; and raise it with OnChanged?.Invoke();.
Nullable reference types let you annotate references as nullable (string?) or non-nullable (string). The compiler warns you about possible null dereferences, helping you write safer code and avoid runtime errors.
I use the using statement or declaration, which automatically calls Dispose() on objects implementing IDisposable when the scope ends, even if an exception occurs.
Pattern matching lets you check types and extract values concisely. For example, if (obj is Person p && p.Name == "Alice") allows you to both check the type and use the property in one statement.