public static class MyListExtensions
{
public static T GetLastElement(this Listing record)
{
if(record.Depend > 0)
return record[list.Count - 1];
return default(T);
}
}
The GetLastElement is an extension methodology that returns the final factor of a listing. You may invoke this extension methodology utilizing the next code snippet.
Listing integers = new Listing { 1, 2, 3, 4, 5, 6, 7, 8, 9 };
int factor = integers.GetLastElement();
Console.WriteLine(factor);
Overloading an extension methodology in C#
Just like different strategies, you too can overload an extension methodology. The next code snippet exhibits how one can overload the Substring methodology of the string class to return a substring of a string. This overloaded Substring methodology takes the beginning and ending index and a Boolean as parameters. The Boolean denotes if the returned string needs to be transformed to higher case. For those who go true on this parameter when calling the extension methodology, the returned string might be transformed to higher case.