Array is a fixed size collection of variables representing the same type. Array can store both built-in primitive types (like integer) and custom objects. In this article we’ll present different ways of initializing such arrays. Array of integers Let’s consider an array of integers. Below are presented five different methods of creating 3 elements array, … Continue reading C# – How to initialize an array
Author: csharp.academy
Favour Boolean assignment via expressions in C# rather than if/else blocks
Where applicable favour Boolean assignment via expressions rather than if/else blocks. The first example below is still so common but the 2nd and 3rd examples require much less code without sacrificing readability. Note… Visual Studio has a nice refactor to help converting relevant if/else blocks to an expression.
Use the digit separator character in C# to improve readability of large numbers
Using the digit separator character _ in C# to break up your large numbers is just another little thing you can do to help the next developer (and you later) read your code. It’s available from C#7…
Nested ternarys can be hard to read for other developers
Be careful with nested ternarys as they can impact code readability. For example I know what 👇🏻 … var comparison = (a > b) ? “a is greater than b” : (a < b) ? “b is greater than a” : “a is equal to b”; does but I have to ‘think’ about it. I … Continue reading Nested ternarys can be hard to read for other developers
How to get started with BenchmarkDotNet using a simple example
BenchmarkDotNet has become the de facto standard for performance benchmarking on .NET. Nearly every video or blog post which Microsoft developers do which has some performance benchmarking uses it. It’s really easy to get started… 1 – Install BenchmarkDotNet from NuGet2 – Create a class which will contain your methods3 – Use the [Benchmark] attribute … Continue reading How to get started with BenchmarkDotNet using a simple example
Use C# 10 Global Usings to make namespaces available to all files in a project
Global Usings in C# 10 allow us to declare a namespace as being available to all files in a project so we don’t need a using statement for it in each file which needs it. Ideally we’d put these global usings in a separate file. This will reduce so called ‘vertical waste’ which is nice. … Continue reading Use C# 10 Global Usings to make namespaces available to all files in a project
Use file-level namespaces in C# 10 to reduce vertical nesting
In C# 9 we saw how top-level statements can help us reduce ‘wasted’ vertical space… BUT… reducing ‘wasted’ horizontal space and in particular nesting levels is much more valuable IMHO and in C# 10 (released November 2021) we have file-level namespaces to help with this. File-level namespaces are definitely one of my favourite features from … Continue reading Use file-level namespaces in C# 10 to reduce vertical nesting
Consider using named arguments in C# to increase readability of method calls
By using named arguments in C# we don’t have to match the ordering of parameter lists of called methods. Instead the matching parameter for each argument can be specified by parameter name. I never use these to change the position of arguments passed in as I find that can confuse other developers but I often use … Continue reading Consider using named arguments in C# to increase readability of method calls
Microsoft drops parameter null checking operator from C#11
Looks like Microsoft have pulled the null check !! operator from C#11. It had been included in C# 11 early previews. I can’t say I’m particularly unhappy about it. It has been discussed for years. Time to focus on more needed features I think. What do you think?
C# – LINQ GroupBy Examples
GroupBy is a LINQ functionality which allows to group items from a collection based on a given key. It’s an equivalent to SQL’s GROUP BY clause. In this article we’ll show various examples of usage, starting from the simplest grouping by one key, through grouping by multiple keys, custom result selector and all of that … Continue reading C# – LINQ GroupBy Examples