Sorting arrays is a common task in programming, and in C#, you can sort arrays without relying on LINQ. This article explores different techniques for sorting arrays in C#, focusing on built-in methods and manual implementations of sorting algorithms. Why Sort Arrays Without LINQ? Although LINQ provides easy sorting with methods like OrderBy, it’s useful … Continue reading How to Sort Arrays in C# Without LINQ
How to Compare Two Dictionaries in C#
Comparing two dictionaries in C# can be essential in various scenarios, such as ensuring data consistency, validating configurations, or debugging. Since dictionaries are key-value pair collections, comparing them requires both key and value checks. In this article, we’ll discuss different methods for comparing two dictionaries in C# based on their keys, values, or both. Why … Continue reading How to Compare Two Dictionaries in C#
How to Handle Null Values in Lists in C#
In C#, lists can sometimes contain null values, especially when working with complex data or handling data from external sources. Properly handling these null values is crucial to ensure that your code runs smoothly without unexpected NullReferenceException errors. This article will guide you through various methods for handling null values in lists in C#. Why … Continue reading How to Handle Null Values in Lists in C#
How to Convert an Integer to Binary in C#
In C#, converting an integer to its binary representation can be useful in many situations, such as low-level programming, bit manipulation, or debugging. The binary system uses only two digits, 0 and 1, making it a fundamental aspect of computing. In this article, we’ll explore multiple ways to convert an integer to binary in C#. … Continue reading How to Convert an Integer to Binary in C#
How to Cast Object to a Specific Type in C#
In C#, casting refers to converting one type of object to another, typically when dealing with object types or derived classes. Often, you’ll find yourself working with object, the base class for all types in C#. Since object is a generic type, casting is necessary when you want to work with the specific properties or … Continue reading How to Cast Object to a Specific Type in C#
Difference between const and readonly in C#
The const and readonly keywords in C# are used to declare variables that cannot be changed after they are initialized. Const variables are initialized at compile time and their value can never change: … while readonly variables can be initialized at compile time or runtime and their value can be updated but only in a constructor … Continue reading Difference between const and readonly in C#
Use Target Typed New Expressions to infer type on the right in C# 9 +
In C# 9 Target Typed New Expressions were introduced. They allow us to infer type on the right, whereas var allows us to infer type on left. The simple example below shows how we can omit the type when using ‘new()’ so the code becomes a little cleaner. Click on the image for a larger … Continue reading Use Target Typed New Expressions to infer type on the right in C# 9 +
Use params keyword in C# to accept a variable number of arguments of the same type
Do you know about the PARAMS keyword in C# ? The params keyword allows us to create methods that accept a variable number of arguments of the same type. The params parameter is always an array, can only be used once and it must be the last parameter in the list of parameters. Here’s a … Continue reading Use params keyword in C# to accept a variable number of arguments of the same type
C# 11 list pattern examples
Beginning with C# 11, we can use list patterns to match elements of a list or array. Some examples of different types of patterns we can use include … Constant and Relational Patterns Discard Patterns The Discard pattern assumes that you know the length of the sequence and match one or more elements from the sequence. Range … Continue reading C# 11 list pattern examples
String.Equals with OrdinalIgnoreCase compared to ToLower / ToUpper for string insensitive comparisons in C#
In C#, when checking if strings are equal in a case insensitive way prefer string.Equals with OrdinalIgnoreCase over using ToLower() / ToUpper(). Why? Correctness Using ToUpper or ToLower can lead to incorrect results in certain cultures and languages. Not all languages follow the simple upper-to-lowercase mapping that English does. For example, in Turkish, the lowercase … Continue reading String.Equals with OrdinalIgnoreCase compared to ToLower / ToUpper for string insensitive comparisons in C#