Posted on February 23, 2025
| 2 minutes
| 405 words
| Ravindra Devrani
Higher-order functions (HOF) are functions that can take a function as an argument or return a function or both. In C#, higher-order functions are achieved using delegates, lambda expressions and expression trees.
Example 1: Using a Delegate
// Declare a delegate that takes an int and returns an intpublicdelegateint IntOperation(int x);
publicclassProgram{
// Higher-order function that takes a delegate as an argumentpublicstaticint PerformOperation(intvalue, IntOperation operation)
{
return operation(value);
}
publicstaticvoid Main()
{
// Create a delegate instance that points to the Square method IntOperation square = x => x \* x;
// Pass the delegate to the higher-order functionint result = PerformOperation(5, square);
Console.WriteLine(result); // Output: 25 }
}
Example 2 : Using lambda expression
// Higher-order function that takes a lambda expression as an argumentpublicstaticint PerformOperation(intvalue, Func<int, int\> operation)
{
return operation(value);
}
// main method// Define a lambda expression that squares a numberFunc<int, int> square = x => x * x;
// Pass the lambda expression to the higher-order functionint result = PerformOperation(5, square);
Example 3: Function as a Return Value
publicclassProgram{
// Higher-order function that returns a functionpublicstatic Func<int, int\> GetOperation(bool isSquare)
{
if (isSquare)
{
return x => x \* x;
}
else {
return x => x + x;
}
}
publicstaticvoid Main()
{
// Get a function that squares a number Func<int, int> operation = GetOperation(true);
// Use the returned functionint result = operation(5);
Console.WriteLine(result); // Output: 25 }
}
In c#, higher order functions are everywhere. If one have used LINQ, must have used HO functions. Collection’s Where() is the good example.
Posted on February 23, 2025
| 6 minutes
| 1236 words
| Ravindra Devrani
I assume you have some knowledge of C# properties. If not, here’s a quick definition: A property is a class member that provides a flexible way to read, write, or compute the value of a private field. Don’t worry if you’re new to properties; we’ll be using them in this blog, and I’ll explain them as we go along.
1. Properties with getter and setter
publicclassPerson{
publicstring Name { get;set; }
publicint Age { get; set;}
}
Generally, we define properties like this. The Person class has two properties Name and Age, each with getters and setters. This enables you to set and read their values.
Posted on February 21, 2025
| 2 minutes
| 310 words
| Ravindra Devrani
In C#, a private constructor is a constructor method declared with the private access modifier (or without mentioning any modifier). If a class have only a private constructor and does not have any public constructor, then you are not able to create the instance of a class. Example 👇
publicclassStudent{
Student() { } // private constructor}
publicclassProgram{
staticvoid Main()
{
Student stu = new(); //Error CS0122 'Student.Student()'// is inaccessible due to its protection level }
}
When you try to create the instance of stu, you will the following error.
Posted on February 21, 2025
| 2 minutes
| 338 words
| Ravindra Devrani
In c#, Abstract class is a class that can not be instantiated on its own
In c#, Abstract class is a class that can not be instantiated on its own
It is typically used as a base class for other class.
Abstract class provides a way to achieve abstraction, because there you can just declare the methods (abstract methods) and implement them later.
It can contain both abstract methods (methods without implementation details) and non-abstract methods (method with implementation details).
Similar goal can be achieved with interface, but in abstract class you can also define non-abstract method. These methods are needed when you need to share some common functionality.
publicabstractclassShape{
// it is the basic syntax of abstract class}
Posted on February 20, 2025
| 4 minutes
| 713 words
| Ravindra Devrani
In this tutorial we will se how to read and write data to csv file. It is pretty much easy if you have some external library for that. We are definitely going to use a library and that will be CsvHelper.
You can also check the video version of this tutorial.
First and foremost, create a c# console application in .net core. After that we need to install a nuget package, which is**CsvHelper**