Wednesday, April 3, 2013

C#/.NET Little Wonders: Extension Methods Demystified

C#/.NET Little Wonders: Extension Methods Demystified:

'via Blog this'

I love extensions. Handy candy, though! Check their page out:
1: public static class IntExtensions
   2: {
   3:     public static int Half(this int source)
   4:     {
   5:         return source / 2;
   6:     }
   7:  
   8:     public static int Cube(this int source)
   9:     {
  10:         return (int)Math.Pow(source, 3);
  11:     }
  12:  
  13:     public static int Square(this int source)
  14:     {
  15:         return (int)Math.Pow(source, 2);
  16:     }
  17: }
And can be easily used like this:
2: var ans = 13.Cube().Half().Square(); 

No comments: