Category: C#

How to make a Type Sortable in C# using IComparable interface?

Assume that you have a type which would be stored in a List. You would want to use List.Sort method to sort your data in the List. For example , assume that you have to store objects of type Employee in List<Employee> and sort the List by Age using the Sort method. To do this , we can use he IComparable<T> interface. How to make…

C# Compiler Error – CS0191 a readonly field cannot be assigned to (

In this blog post, you’ll learn more about the C# Compiler Error – CS0191 and the related message description C# Compiler Error Code CS0191 C# Compiler Description for the Code :CS0191 A readonly field cannot be assigned to (except in a constructor or init-only setter of the type in which the field is defined or a variable initializer)

Example of Delegates in C#

Delegates in C# is simply a reference/pointer to a function . One of the typical usage of the delegates is the event handlers in .NET. Example of Delegates in C# Below is a sample code snippet demonstrating the usage of delegates to add and subtract 2 numbers.