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

Advanced
-Collapse +Expand C# To/From
To/FromCODEGuides
-Collapse +Expand C# Study Test
PRESTWOODCERTIFIED
-Collapse +Expand C# Store
PRESTWOODSTORE

Prestwood eMagazine

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

   ► KBProgrammingC#OOP   Print This     
 
C# OOP:
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

KB101992

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;
            Consolole.WriteLine(t.GetFirstOfMonth().ToString);
            Consolole.WriteLine(t.GetEndOfMonth().ToString);

Blog Entries!

1 Comments.
 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
7076
Since 5/11/2009
Go ahead!   Use Us! Call: 916-726-5675  Or visit our new sales site: 
www.prestwood.com


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