Join is a LINQ functionality to combine two collections and produce a single result set. Connection happens by comparing items from both series. When there is a match then such pair is one of the output elements. Let’s consider following example. We’ve got two separate collections: countries and cities, which store objects of Country and … Continue reading C# – LINQ Join Examples
Author: csharp.academy
C# – How to generate random password
This article delivers proposal of algorithm to generate random passwords or actually random strings which are generated in safe manner and can be used as passwords, discount codes etc. Also, it is configurable, so that you can easily define expected length of string, pool of available characters or minimal number of occurrences of particular elements. … Continue reading C# – How to generate random password
C# – How to generate random string
There are situations where you need a random string which is built with specific characters. In this article you’ll see how to generate such string where both length and elements are totally up to you. Algorithm is very simple. It uses for loop, so that in every iteration it extends an output string with randomly … Continue reading C# – How to generate random string
C# – How to generate random number
Generating random numbers in C# is quick and easy using Random class. It’s built-in functionality which allows to produce integers, doubles and bytes. In this article you can find examples how to use it. Random integers The most basic usage is calling Next function without any parameters against an object of Random class. It returns … Continue reading C# – How to generate random number
C# – How to get unique items from list
The easiest way of getting unique items from list is LINQ’s Distinct() method. In this article you’ll see how to use it with both built-in types (like collection of integers) and custom types (like collection of complex type objects). Get unique values from collection of integers Let’s start with creating example list of items where … Continue reading C# – How to get unique items from list
How to set the Ratio column style in BenchmarkDotNet results
By default when you output the Ratio column in BenchmarkDotNet using the Baseline = true attribute it is output using the Value approach below. This means that the baseline method will be shown as 1.00 and that other methods will be shown as a relative value to this. If we wish we can also output … Continue reading How to set the Ratio column style in BenchmarkDotNet results
Using declarations in C#8+ can allow us to dispose of resources correctly without increasing nesting levels
Since C#8 we can use single line using declarations which will allow us to dispose of resources at the end of the enclosing scope. This helps us reduce nesting in our code BUT… … while the using {} approach allows us to explicitly tell the compiler when to dispose of a resource, the C#8+ using declaration … Continue reading Using declarations in C#8+ can allow us to dispose of resources correctly without increasing nesting levels
How to output the Ratio column when using BenchmarkDotNet
If you’re using BenchmarkDotNet and want to see how other methods compare against a baseline method, set the Baseline attribute on your baseline method. Doing this will result in the Ratio column being outputted as shown below… More info from the Benchmark and Job Baselines page on BenchmarkDotNet.org.
Omit type name when calling static methods with the using static directive in C#
The using static directive was introduced in C# 6 and allows us to omit the type name when calling static methods… cool right? … but I wouldn’t use this when omitting the type name would result in ambiguity or confusion for the next developer about what class a static method is in. The examples below … Continue reading Omit type name when calling static methods with the using static directive in C#
Using aliases in C# to provide shorthand references to types or namespaces
In C# we can use aliases to provide shorthand references to types or namespaces. Aliases can be useful to tidy up our code for example when we might be repeatability referencing generic types. They also help to remove ambiguity when we have conflicting type names in difference namespaces. In this case without aliases we would … Continue reading Using aliases in C# to provide shorthand references to types or namespaces