Skip to main content

Posts

Showing posts from February, 2021

C# Extension Methods

 C# Extension Methods Extension methods      public   static   class   ExtensionMethods     {          public   static   Stopwatch   Measure ( this   Func < int >  f )         {              var   sw  =  new   Stopwatch ();              sw . Start ();              f ();              sw . Stop ();              return   sw ;         }          public   static   void   Save ( this   ISerializable   serializable )         {              //         }          public   static   void   DeepCopy < T >( this   T   o )  where   T  :  ISerializable ,  new ()         {               //         }     }      class   Program     {          static   void   Main ( string []  args )         {              Func < int >  calculate  =  delegate             {                  Thread . Sleep ( 1000 );                  return   42 ;             };              var   sw  =  calculate . Measure ();              Console . WriteLine ( $"Took { sw . ElapsedMilliseconds }ms" );         }