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.
Answer a question in the forums and earn +20 SHARP for every answer.
See unanswered questions
DevSocialUpcoming · 3rd editionAI Agents ConferenceDec 1–3, 2026Live onlineFree to attend5days leftRegister
Sachin SinghRank #66 · 26.2K reputationPosted Feb 2, 2023, 8:10 AM EST
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
Vishal YelveRank #178 · 13.6K reputationPosted Feb 2, 2023, 8:18 AM EST
https://youtu.be/Iu7OrL6vCOw
Refere above shivprasad koirala sir video on Extension method
Anupam MaitiRank #295 · 8.5K reputationPosted Feb 2, 2023, 1:09 PM EST
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 :
using System; namespace ExtensionMethods { public static class IntExtension { public static bool IsEven(this int number) { return number % 2 == 0; } } }Now you can use the
IsEvenmethod on anyintobject, as if it were an instance method:int number = 10; Console.WriteLine(number.IsEven()); // Output: True int anotherNumber = 11; Console.WriteLine(anotherNumber.IsEven()); // Output: FalseJignesh KumarRank #91 · 22.9K reputationPosted Feb 2, 2023, 8:24 AM EST
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/
Naimish MakwanaRank #324 · 7.7K reputationPosted Feb 2, 2023, 11:21 AM EST
Hello Vikas,
Please refer below link.
https://learn.microsoft.com/en-us/dotnet/csharp/programming-guide/classes-and-structs/extension-methods
Thanks
Naimish
Subarta RayRank #336 · 7.4K reputationPosted Dec 26, 2023, 1:02 AM EST
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 NathRank #681 · 3.5K reputationPosted Dec 20, 2023, 12:24 AM EST
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.
public static class StringExtensions { // Extension method to count the number of words in a string public static int WordCount(this string str) { // Split the string by spaces and count the resulting array's length string[] words = str.Split(new char[] { ' ' }, StringSplitOptions.RemoveEmptyEntries); return words.Length; } } class Program { static void Main() { string sentence = "This is an example sentence."; // Call the extension method on a string object int count = sentence.WordCount(); Console.WriteLine($"The number of words in the sentence is: {count}"); } }