Your full service technology partner! 
-Collapse +Expand
Search C# Group:

-Collapse +Expand C# To/From
-Collapse +Expand C# Study Test
-Collapse +Expand C# Store

Prestwood eMagazine

April Edition
Subscribe now! It's Free!
Enter your email:

   ► KBProgrammingC#OOP   Print This     
Extension Methods in C#
Posted 11 years ago on 5/11/2009 and updated 2/10/2020
Take Away:

Add new methods to predefined types and objects with extension methods (no need to recompile the base code).  To use this extension, all i have to do is include the  namespace ExtensionExample.

 A blog topic from khemebuen's Blog


With Extension methods you are able to add new methods to predefined types and objects without recompiling the base type or type.

As an example, I will extend the DateTime object and add two more methods to it:GetFirstOfMonth, and GetEndOfMonth.

The snippet below is a static class which extends two more methods of the DateTime class

namespace ExtensionExample
    public static class DateTimeExtensions 
        public static DateTime GetFirstOfMonth(this DateTime Value)
            return new DateTime(Value.Year,Value.Month,1);
        public static DateTime GetEndOfMonth(this DateTime Value)
            return new DateTime(Value.Year, Value.Month, DateTime.DaysInMonth(Value.Year, Value.Month));

to use this extension, all i have to do is include the  namespace ExtensionExample.

then when a DateTime variable is declared, it will have the two methods i just extended

            DateTime t = DateTime.Today;

Blog Entries!

 Contribute to this open topic blog!
Comment 1 of 1

Hey everyone! I think, there is very interesting and useful post! I am a student of the DHG university and for me the greatest way to write my essays is to use the coolest custom writing service that I have found a few months ago via web searching. I no longer strain when I get a writing task in the form of essays and instantly go for help to professional typers to make an order. I can say, this greatly facilitates my student life. I can advise this awesome writing company for every student who needs help, just like me!!!

Posted 55 days ago
Comment on this blog topic...
Sign in...

If you are a member, Sign In. Or, you can Create a Free account now.

Anonymous Post (text-only, no HTML):

Enter your name and security key.

Your Name:
Security key = P151A1
Enter key:
Blog Contributed By khemebuen:

Roland Chie

Visit Profile

 KB Article #101992 Counter
Since 5/11/2009
Go ahead!   Use Us! Call: 916-726-5675  Or visit our new sales site:

©1995-2020 Prestwood IT Solutions.   [Security & Privacy]