In this code snippet, we’ll see how to use GPU acceleration in C# using the ILGPU library. ILGPU provides you with a fairly simple interface to run code on your GPU from C#. For more information, you can check out the official documentation here. In this post, I will show you how to do some image processing by utilizing GPU acceleration if you want to see more examples(simpler or more complex ones) you can find them here.
In this code snippet, we will take a look at COM objects, interop using PInvoke and type marshalling in C#.
In this code snippet, we will take a look at lambda expressions, Func
In this code snippet, we will take a look at unsafe code, pointers, stack allocation and spans in C#. C# has managed memory unlike for example C/C++. This means that you don’t have to allocate or clear memory by yourself and in the case of .NET the CLR(common language runtime) takes care of memory allocation and garbage collection. This also means that in C# you can’t work with a pointer or allocate your variables on the stack which is common practice in C/C++.
In this code snippet, we will take a look at async and await in C#.
The aysnc keyword enables a method to use the await keyword. These two keywords together enable us to execute code asynchronously. When called an async method will return a Task.
In this code snippet, we will learn how to do some basic queries using LINQ(Language Integrated Query) in C#.LINQ is used to work with data within C# and can be used on enumerable types(IEnumerable
In this code snippet, we will take a look at bitwise operators in C#. Bitwise operators can be used to manipulate individual bits of a variable. These operations can be quite cryptic and hard to understand compared to regular code. So only use it when it makes sense(for example bit masking) and don’t prematurely optimize your code. In C# we can use enums in conjunction with the Flags attribute to do bitmasking. This gives us cleaner and more readable code.
In this code snippet, we will take a look at pattern matching in C#. Pattern matching is simply testing if an expression matches certain criteria. For example, Regex often times gets used for more complex pattern matching of strings.
In this code snippet, we will take a look at the nullable types, null coalescing and checking operators in C#. Reference types are nullable by default, value types like int for example are not. You can make them nullable by appending ? after the data type declaration like so: int?
In this code snippet, we will take a look at records in C#. Records can be used instead of classes and structs. They reduce the amount of boilerplate code required to create objects whose purpose is to simply hold data. By default, records are reference types just like classes but they will act like value types when compared for equality(using == operator) meaning that two different records will be evaluated as the same if all their values are the same.