what is ention method in c# with example
Loading
what is ention method in c# with example
Know the answer? Post it — somebody with the same question will find it here.
Sign in to answer this question
It is the same account you read, post and publish with — and you will come straight back to this page.
Subarta RayPosted Dec 26, 2023, 6:02 AM
Hi Vikas ,
public static class StringExtensions
{
public static string CustomMethod(this string input)
{
// Your custom logic here
return "Modified " + input;
}
}
// Usage
string result = "Hello".CustomMethod(); // Returns "Modified Hello"
Anandu G NathPosted Dec 20, 2023, 5:24 AM
An extension method in C# allows you to add new methods to existing classes or interfaces without modifying the original code, providing a way to extend their functionality. These methods appear as if they are part of the original class or interface. They are especially useful when you cannot modify the source code of a class, yet you want to add functionality to it.
Anupam MaitiPosted Feb 2, 2023, 6:09 PM
An extension method is a special type of static method in C# that allows you to add methods to existing types without modifying the original type.
thiskeyword in front of the first parameter of the extension method indicates that this method is an extension methodFor Example :
Now you can use the
IsEvenmethod on anyintobject, as if it were an instance method:Naimish MakwanaPosted Feb 2, 2023, 4:21 PM
Hello Vikas,
Please refer below link.
https://learn.microsoft.com/en-us/dotnet/csharp/programming-guide/classes-and-structs/extension-methods
Thanks
Naimish
Jignesh KumarPosted Feb 2, 2023, 1:24 PM
Hello,
Please refer this thread which will help you out to understand step by step,
https://www.c-sharpcorner.com/article/real-time-example-of-extension-method-in-c-sharp2/
Vishal YelvePosted Feb 2, 2023, 1:18 PM
https://youtu.be/Iu7OrL6vCOw
Refere above shivprasad koirala sir video on Extension method
Sachin SinghPosted Feb 2, 2023, 1:10 PM
Please follow my article on this, for any doubt feel free to ask
https://www.sharpencode.com/article/Linq/an-overview-of-linq/extension-methods