C# 9 shipped with .NET 5 in November. One change included is init only properties which allow us to have immutable (non-changeable) classes without some of the downsides associated with pre C# 9 approaches. As can be seen below in C# 8 we’d typically have getters with no setters which gives us immutability…great.. but it … Continue reading Easier immutability with Init only properties in C# 9
Author: csharp.academy
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; … Continue reading C# – How to split a string
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. Parse method Basic form of Parse method receives string as an input parameter and returns converted value as an … Continue reading C# – How to convert string to int
C# – 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. Custom function Let’s say … Continue reading C# – How to check if string contains only digits
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 … Continue reading Mutation Testing with C# and .NET Core
C# – LINQ Any Examples
Any is LINQ functionality to validate whether collection contains at least one element which meets given criteria. Let’s start with example collection of integers: var integers = new List<int>() { 36, 10, 4, 23, 1 }; 1 var integers = new List<int>() { 36, 10, 4, 23, 1 }; First call the Any function without any … Continue reading C# – LINQ Any Examples
C# – LINQ First Examples
First is LINQ functionality to return first item of the collection or throw exception if such item does not exist. First is overloaded method which can be used with either zero or one parameter. The first option just returns first element and the second one allows to define condition which needs to be met. 1. Collection … Continue reading C# – LINQ First Examples
C# – How to generate Guid
.NET Framework provides built-in Guid structure which allows to generate unique identifier. The usage is very simple and requires to call NewGuid function on Guid structure. Guid guid = Guid.NewGuid(); 1 Guid guid = Guid.NewGuid(); Returned object type is Guid however we can easily convert it to string. var guidString = guid.ToString(); 1 var guidString = … Continue reading C# – How to generate Guid
C# – Object to JSON Examples
In this article you can find how to convert C# object into JSON using Json.NET (Newtonsoft.Json) library. It is available as a NuGet package for free, so you can easily install it from nuget.org repository. Let’s create an example class which object will be converted into JSON. public class Place { public string Name { get; … Continue reading C# – Object to JSON Examples
C# – JSON to Object Examples
In this article you can find examples how to convert JSON into C# object using Json.NET (Newtonsoft.Json) library. It is available as a NuGet package for free, so you can easily install it from nuget.org repository. Let’s create a JSON which will be used in below examples. { Forename: “John”, Surname: “Smith”, Age: 40, IsMarried: … Continue reading C# – JSON to Object Examples