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 in two variants: Lambda and Query expression.

Continue reading C# – LINQ GroupBy Examples

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. All is encapsulated within a class, ready to reuse in your project. In this articles we’ll go through small code snippets and describe them in details, but full class can be also found at the end.

Continue reading C# – How to generate random password

C# – How to split a string

String can be split into collection of substrings based on a defined character (separator) using Split() function. Split is part of String class which exists in the System namespace.

Split function is overloaded and has various forms which allow to:

  • pass separator as a single character, array of characters, single string or array of strings;
  • define maximum number of substrings in output array;
  • define split options like removing empty elements from returned collection.
Continue reading C# – How to split a string

Mutation Testing with C# and .NET Core

Unit Testing is widely known method of validating results produced by a code in an automated manner. Good test coverage helps with maintaining a code as it is much easier and quicker to spot potential bugs after code changes. How to check if a test suite is effective though? One of the answers is to use Mutation Testing.

The idea is to create mutants which are modified versions of your code and run existing unit tests against these mutants. If a mutation caused test run failure it means a mutant is killed (which is good). If the tests passed then a mutant survived (which is not good). The measurement of the unit tests quality is percentage of the killed mutants where the higher figure is the better. In this approach we expect the test run to fail on mutated code otherwise either not all paths are covered by the unit tests or your tests miss some conditions.

Continue reading Mutation Testing with C# and .NET Core