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 ExamplesC# – LINQ Join Examples
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.
Continue reading C# – LINQ Join ExamplesC# – 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 passwordC# – 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.
Continue reading C# – How to generate random stringC# – 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.
Continue reading C# – How to generate random numberC# – 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).
Continue reading C# – How to get unique items from listC# – 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.
C# – How to convert string to int
There are three easiest ways which allow you to convert string data type to int:
- Parse method
- TryParse method
- Convert class
In the following article I’ll explain each one and show the example of its usage.
Continue reading C# – How to convert string to intC# – How to check if string contains only digits
There are plenty of methods to achieve this but even though it’s simple task you need to specify requirements like:
- are white spaces allowed?
- how to treat empty string?
- what’s the maximum length of the string?
In this article I want to describe 3 different ideas how to solve this problem.
Continue reading C# – How to check if string contains only digitsMutation 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