How do I extend a class with c# extension methods?

后端 未结 9 1390
情书的邮戳
情书的邮戳 2020-12-04 10:48

Can extension methods be applied to the class?

For example, extend DateTime to include a Tomorrow() method that could be invoked like:

DateTime.Tomor         


        
9条回答
  •  青春惊慌失措
    2020-12-04 11:14

    I would do the same as Kumu

    namespace ExtensionMethods
    {
        public static class MyExtensionMethods
        {
            public static DateTime Tomorrow(this DateTime date)
            {
               return date.AddDays(1);
            }    
        }
    }
    

    but call it like this new DateTime().Tomorrow();

    Think it makes more seens than DateTime.Now.Tomorrow();

提交回复
热议问题