code_junkie

Usage of Extension Methods

Nov 13th, 2011
84
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.98 KB | None | 0 0
  1. instance.SomeExtensionMethod()
  2.  
  3. StaticExtensionMethodClass.SomeExtensionMethod(instance);
  4.  
  5. public class extended {
  6. public int sum() {
  7. return 7+3+2;
  8. }
  9. }
  10.  
  11. public static class extending {
  12. public static float average(this extended extnd) {
  13. return extnd.sum() / 3;
  14. }
  15. }
  16.  
  17. extended ex = new extended();
  18.  
  19. Console.WriteLine(ex.average());
  20.  
  21. public static void ThrowIfNull<T>(this T obj, string name) where T : class
  22. {
  23. if(obj == null) throw new ArgumentNullException(name);
  24. }
  25.  
  26. var foo = source.Where(predicate).OrderBy(selector);
  27.  
  28. var foo = Enumerable.OrderBy(Enumerable.Where(source,predicate),selector);
  29.  
  30. ExtendUnlimitedCredit(AddVIP(tblCustomer, "Bill Gates"))
  31.  
  32. tblCustomer.AddVIP("Bill Gates").ExtendUnlimitedCredit()
  33.  
  34. public class Stock {
  35. public Code { get; private set; }
  36. public Name { get; private set; }
  37. }
  38.  
  39. public static class StockExtender {
  40. public static List <Quote> GetQuotesByDate(this Stock s, DateTime date)
  41. {...}
  42. }
Add Comment
Please, Sign In to add comment