I will demonstrate how to create an extension method in C# for the List type. I will explain how we can extend the functionality of List without a new derived class. I will create extension methods for list types to help us use them across projects without creating a new class.
Let's create an extension method for the list that will help us use it in the project for any list object. Here, I have created five extension methods with examples. I will explain each with examples of how we can use them in our real-time project.
Extension Method Example
Let's create a class and a few records for that class so we can understand how we can use the above methods for our project.
Now create a list with the null assignment and try to use Any() on it.
It will throw an error. "System.ArgumentNullException: 'Value cannot be null. (Parameter'source')'" as below,
![Will show the following Error]()
Extension Method. SafeAny()
How to use it in a project?
![SafeAny method]()
Impact of this method on code
If we use this extension method in a project, then we will make sure it will not throw an error even if the developer creates a list and initializes it with a null value.
Now we can use SafeAny() to check the null check as well. If the developer assigns a list with null values, then it will not throw any errors, but it will handle null and return false. It will handle unintentional errors in the application.
![Unintentional errors]()
Extension method. DistinctByProperty()
This method will do distinct work on multiple fields; here, I have used the age and experience fields to do distinct work based on two fields.
How to use it in a project?
Impact of this method on code
By default, lists do not support distinct values for multiple columns before .NET 6. If you are using older than.NET 6, then you can create an extension method that you can use in the entire project with a simplified version like above.It makes the code cleaner and easier to use when we want to create a distinct list based on multiple columns.
Extension method. HasOneItem()
This method will return true or false. If the list contains only a single value then will return true else false.
How to use it in a project?
Impact of this method on code
This method is very useful for checking the count of a list. Using this, you don't need to repeat count() > 0 code everywhere in your project; you can use the HasOneItem() method in the entire project if you create an extension method, which will simplify your code and make it more readable.
Extension method. HasAny(YourCondition)
HasAny method. we can pass a predicate, and it will return true if the specified condition is satisfied.
How to use it in a project?
Conclusion
In this article, we have learned an extension method for a list object that will help us extend an existing list type, write less code, and make it more readable. In the above, we have created five extension methods for list object types that make our code more robust and maintainable. Extension methods are widely used in practice to promote clean and modular code design.