1 extension methods for the class:2 3 1. Requirements are static classes4 2the method required to write is a static method. 5 3to use the This keyword, the object that is passed in represents the current object. 6 7 Example: The Where method of the extended list collection compares the size of the string. 8 1. Create a class that is named ListExt.cs9 Ten //1> Creating a static class One Public StaticClass ListExt A { - //Create a static method: Note Use the This keyword. - Public Staticlist<string> Mywhere ( Thislist<string> list,func<string,BOOL>mylistext) the { - //iterates through the current list combination, invokes the delegate mylistext to pass in a string, returns a bool type -list<string> strlist=Newlist<string>(); - foreach(varIteminchlist) + { - if(Mylistext (item)) + { A Strlist.add (item); at } - } - returnstrlist; - } - } - in Call the method: -list<string> newlist=Newlist<string>(); tolist<string> Result=newlist.mywhere (A=>a.compareto ("6") >0);
---method of class extension: