Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. The Lounge
  3. do you use extension methods intensively in your projects?

do you use extension methods intensively in your projects?

Scheduled Pinned Locked Moved The Lounge
question
42 Posts 27 Posters 38 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • S Southmountain

    just curious. recently I start to use extension methods in my projects.

    diligent hands rule....

    G Offline
    G Offline
    Garth J Lancaster
    wrote on last edited by
    #5

    As per Super Lloyd - I use them, I don't know about 'extensively', but, 'when it seems to make sense'

    1 Reply Last reply
    0
    • S Southmountain

      just curious. recently I start to use extension methods in my projects.

      diligent hands rule....

      J Offline
      J Offline
      Jorgen Andersson
      wrote on last edited by
      #6

      Seconding the others.

      Wrong is evil and must be defeated. - Jeff Ello

      1 Reply Last reply
      0
      • S Southmountain

        just curious. recently I start to use extension methods in my projects.

        diligent hands rule....

        realJSOPR Offline
        realJSOPR Offline
        realJSOP
        wrote on last edited by
        #7

        I have an assembly full of them.

        ".45 ACP - because shooting twice is just silly" - JSOP, 2010
        -----
        You can never have too much ammo - unless you're swimming, or on fire. - JSOP, 2010
        -----
        When you pry the gun from my cold dead hands, be careful - the barrel will be very hot. - JSOP, 2013

        1 Reply Last reply
        0
        • S Southmountain

          just curious. recently I start to use extension methods in my projects.

          diligent hands rule....

          OriginalGriffO Offline
          OriginalGriffO Offline
          OriginalGriff
          wrote on last edited by
          #8

          Like all other language features, I use them when it's appropriate. That means sometimes I do, and sometimes I don't. It depends on what the method is doing and whether it makes more sense as an extension method or as a property / standard method. If you need to add functionality to a sealed class such as String then it makes a lot of sense. If the class is your own, then it's just silly to use extension methods ... :laugh:

          Bad command or file name. Bad, bad command! Sit! Stay! Staaaay...

          "I have no idea what I did, but I'm taking full credit for it." - ThisOldTony
          "Common sense is so rare these days, it should be classified as a super power" - Random T-shirt

          Sander RosselS 1 Reply Last reply
          0
          • S Southmountain

            just curious. recently I start to use extension methods in my projects.

            diligent hands rule....

            W Offline
            W Offline
            Wendelius
            wrote on last edited by
            #9

            As OG pointed out when adding functionality to sealed class then yes, but otherwise I consider between extension methods and inheritance. Mainly because I often need more functionality than just a method and perhaps even new instance variables. But yes I use them when I see fit.

            realJSOPR 1 Reply Last reply
            0
            • W Wendelius

              As OG pointed out when adding functionality to sealed class then yes, but otherwise I consider between extension methods and inheritance. Mainly because I often need more functionality than just a method and perhaps even new instance variables. But yes I use them when I see fit.

              realJSOPR Offline
              realJSOPR Offline
              realJSOP
              wrote on last edited by
              #10

              I'm sure the OP means extension methods for native .Net objects, such as string, Type, DateTime, etc.

              ".45 ACP - because shooting twice is just silly" - JSOP, 2010
              -----
              You can never have too much ammo - unless you're swimming, or on fire. - JSOP, 2010
              -----
              When you pry the gun from my cold dead hands, be careful - the barrel will be very hot. - JSOP, 2013

              W 1 Reply Last reply
              0
              • S Southmountain

                just curious. recently I start to use extension methods in my projects.

                diligent hands rule....

                D Offline
                D Offline
                Duncan Edwards Jones
                wrote on last edited by
                #11

                I use them a lot to encapsulate the business meaning of a specific IEnumerable Where clause. Makes code much more readable.

                realJSOPR 1 Reply Last reply
                0
                • realJSOPR realJSOP

                  I'm sure the OP means extension methods for native .Net objects, such as string, Type, DateTime, etc.

                  ".45 ACP - because shooting twice is just silly" - JSOP, 2010
                  -----
                  You can never have too much ammo - unless you're swimming, or on fire. - JSOP, 2010
                  -----
                  When you pry the gun from my cold dead hands, be careful - the barrel will be very hot. - JSOP, 2013

                  W Offline
                  W Offline
                  Wendelius
                  wrote on last edited by
                  #12

                  Yep, that's what I understood.

                  1 Reply Last reply
                  0
                  • D Duncan Edwards Jones

                    I use them a lot to encapsulate the business meaning of a specific IEnumerable Where clause. Makes code much more readable.

                    realJSOPR Offline
                    realJSOPR Offline
                    realJSOP
                    wrote on last edited by
                    #13

                    My most recent ones calculate business chart trend line values from either an IEnumerable<double> or a IEnumerable<T> using the named property.

                    ".45 ACP - because shooting twice is just silly" - JSOP, 2010
                    -----
                    You can never have too much ammo - unless you're swimming, or on fire. - JSOP, 2010
                    -----
                    When you pry the gun from my cold dead hands, be careful - the barrel will be very hot. - JSOP, 2013

                    S 1 Reply Last reply
                    0
                    • OriginalGriffO OriginalGriff

                      Like all other language features, I use them when it's appropriate. That means sometimes I do, and sometimes I don't. It depends on what the method is doing and whether it makes more sense as an extension method or as a property / standard method. If you need to add functionality to a sealed class such as String then it makes a lot of sense. If the class is your own, then it's just silly to use extension methods ... :laugh:

                      Bad command or file name. Bad, bad command! Sit! Stay! Staaaay...

                      Sander RosselS Offline
                      Sander RosselS Offline
                      Sander Rossel
                      wrote on last edited by
                      #14

                      OriginalGriff wrote:

                      If the class is your own, then it's just silly to use extension methods

                      No it's not! On a few occasions I've created an interface, ISomething, and before writing any implementation I wrote a few extension methods I knew I needed. And then I wrote the implementations (and got so much functionality out of the box!) :D Kind of like collections and LINQ, all M$ "own" classes, but they still have lots of extension methods.

                      Read my (free) ebook Object-Oriented Programming in C# Succinctly. Visit my blog at Sander's bits - Writing the code you need. Or read my articles here on CodeProject.

                      Simplicity is prerequisite for reliability. — Edsger W. Dijkstra

                      Regards, Sander

                      B F 2 Replies Last reply
                      0
                      • S Southmountain

                        just curious. recently I start to use extension methods in my projects.

                        diligent hands rule....

                        Sander RosselS Offline
                        Sander RosselS Offline
                        Sander Rossel
                        wrote on last edited by
                        #15

                        Yes I do. I use the ones MS created, I use them to enhance built-in types and I use them to enhance my own types. Only when it makes sense, of course :D

                        Read my (free) ebook Object-Oriented Programming in C# Succinctly. Visit my blog at Sander's bits - Writing the code you need. Or read my articles here on CodeProject.

                        Simplicity is prerequisite for reliability. — Edsger W. Dijkstra

                        Regards, Sander

                        1 Reply Last reply
                        0
                        • S Southmountain

                          just curious. recently I start to use extension methods in my projects.

                          diligent hands rule....

                          L Offline
                          L Offline
                          Lost User
                          wrote on last edited by
                          #16

                          I mostly use them for things that can't have methods defined on them directly, interfaces and enums.

                          C 1 Reply Last reply
                          0
                          • S Southmountain

                            just curious. recently I start to use extension methods in my projects.

                            diligent hands rule....

                            M Offline
                            M Offline
                            Marc Clifton
                            wrote on last edited by
                            #17

                            public static bool Yes(this T foo) {return true;}

                            Marc

                            Imperative to Functional Programming Succinctly Contributors Wanted for Higher Order Programming Project! Learning to code with python is like learning to swim with those little arm floaties. It gives you undeserved confidence and will eventually drown you. - DangerBunny

                            B S M 3 Replies Last reply
                            0
                            • S Southmountain

                              just curious. recently I start to use extension methods in my projects.

                              diligent hands rule....

                              P Offline
                              P Offline
                              pt1401
                              wrote on last edited by
                              #18

                              I use them whenever they make the code clearer & more concise, which is often. Intensively? Probably not. Extensively? Maybe.

                              1 Reply Last reply
                              0
                              • M Marc Clifton

                                public static bool Yes(this T foo) {return true;}

                                Marc

                                Imperative to Functional Programming Succinctly Contributors Wanted for Higher Order Programming Project! Learning to code with python is like learning to swim with those little arm floaties. It gives you undeserved confidence and will eventually drown you. - DangerBunny

                                B Offline
                                B Offline
                                BillWoodruff
                                wrote on last edited by
                                #19

                                Samhain (Halloween) is still fifteen days away, but you got my vote on this one, anyhow. cheers, Bill

                                «There is a spectrum, from "clearly desirable behaviour," to "possibly dodgy behavior that still makes some sense," to "clearly undesirable behavior." We try to make the latter into warnings or, better, errors. But stuff that is in the middle category you don’t want to restrict unless there is a clear way to work around it.» Eric Lippert, May 14, 2008

                                1 Reply Last reply
                                0
                                • Sander RosselS Sander Rossel

                                  OriginalGriff wrote:

                                  If the class is your own, then it's just silly to use extension methods

                                  No it's not! On a few occasions I've created an interface, ISomething, and before writing any implementation I wrote a few extension methods I knew I needed. And then I wrote the implementations (and got so much functionality out of the box!) :D Kind of like collections and LINQ, all M$ "own" classes, but they still have lots of extension methods.

                                  Read my (free) ebook Object-Oriented Programming in C# Succinctly. Visit my blog at Sander's bits - Writing the code you need. Or read my articles here on CodeProject.

                                  Simplicity is prerequisite for reliability. — Edsger W. Dijkstra

                                  Regards, Sander

                                  B Offline
                                  B Offline
                                  BillWoodruff
                                  wrote on last edited by
                                  #20

                                  Hi Sandor, I'd really appreciate seeing an example of Extension Methods for an Interface. cheers, Bill

                                  «There is a spectrum, from "clearly desirable behaviour," to "possibly dodgy behavior that still makes some sense," to "clearly undesirable behavior." We try to make the latter into warnings or, better, errors. But stuff that is in the middle category you don’t want to restrict unless there is a clear way to work around it.» Eric Lippert, May 14, 2008

                                  Sander RosselS 1 Reply Last reply
                                  0
                                  • B BillWoodruff

                                    Hi Sandor, I'd really appreciate seeing an example of Extension Methods for an Interface. cheers, Bill

                                    «There is a spectrum, from "clearly desirable behaviour," to "possibly dodgy behavior that still makes some sense," to "clearly undesirable behavior." We try to make the latter into warnings or, better, errors. But stuff that is in the middle category you don’t want to restrict unless there is a clear way to work around it.» Eric Lippert, May 14, 2008

                                    Sander RosselS Offline
                                    Sander RosselS Offline
                                    Sander Rossel
                                    wrote on last edited by
                                    #21

                                    The entire LINQ library depends upon it. For example the Count() extension method on collections may look as follows:

                                    public static class Extensions
                                    {
                                    public static int Count(this IEnumerable collection)
                                    {
                                    int count = 0;
                                    using (var enumerator = collection.GetEnumerator())
                                    {
                                    while (enumerator.MoveNext())
                                    {
                                    count += 1;
                                    }
                                    }
                                    return count;
                                    }
                                    }

                                    The actual extension method checks for null and tries to cast to ICollection<T> and ICollection for the Count property first, but it's an extension on an interface. There are LOTS of them...

                                    Read my (free) ebook Object-Oriented Programming in C# Succinctly. Visit my blog at Sander's bits - Writing the code you need. Or read my articles here on CodeProject.

                                    Simplicity is prerequisite for reliability. — Edsger W. Dijkstra

                                    Regards, Sander

                                    S 1 Reply Last reply
                                    0
                                    • M Marc Clifton

                                      public static bool Yes(this T foo) {return true;}

                                      Marc

                                      Imperative to Functional Programming Succinctly Contributors Wanted for Higher Order Programming Project! Learning to code with python is like learning to swim with those little arm floaties. It gives you undeserved confidence and will eventually drown you. - DangerBunny

                                      S Offline
                                      S Offline
                                      Southmountain
                                      wrote on last edited by
                                      #22

                                      very inspirational

                                      diligent hands rule....

                                      1 Reply Last reply
                                      0
                                      • realJSOPR realJSOP

                                        My most recent ones calculate business chart trend line values from either an IEnumerable<double> or a IEnumerable<T> using the named property.

                                        ".45 ACP - because shooting twice is just silly" - JSOP, 2010
                                        -----
                                        You can never have too much ammo - unless you're swimming, or on fire. - JSOP, 2010
                                        -----
                                        When you pry the gun from my cold dead hands, be careful - the barrel will be very hot. - JSOP, 2013

                                        S Offline
                                        S Offline
                                        Southmountain
                                        wrote on last edited by
                                        #23

                                        this is a good one..

                                        diligent hands rule....

                                        1 Reply Last reply
                                        0
                                        • Sander RosselS Sander Rossel

                                          The entire LINQ library depends upon it. For example the Count() extension method on collections may look as follows:

                                          public static class Extensions
                                          {
                                          public static int Count(this IEnumerable collection)
                                          {
                                          int count = 0;
                                          using (var enumerator = collection.GetEnumerator())
                                          {
                                          while (enumerator.MoveNext())
                                          {
                                          count += 1;
                                          }
                                          }
                                          return count;
                                          }
                                          }

                                          The actual extension method checks for null and tries to cast to ICollection<T> and ICollection for the Count property first, but it's an extension on an interface. There are LOTS of them...

                                          Read my (free) ebook Object-Oriented Programming in C# Succinctly. Visit my blog at Sander's bits - Writing the code you need. Or read my articles here on CodeProject.

                                          Simplicity is prerequisite for reliability. — Edsger W. Dijkstra

                                          Regards, Sander

                                          S Offline
                                          S Offline
                                          Southmountain
                                          wrote on last edited by
                                          #24

                                          thanks for this example!

                                          diligent hands rule....

                                          1 Reply Last reply
                                          0
                                          Reply
                                          • Reply as topic
                                          Log in to reply
                                          • Oldest to Newest
                                          • Newest to Oldest
                                          • Most Votes


                                          • Login

                                          • Don't have an account? Register

                                          • Login or register to search.
                                          • First post
                                            Last post
                                          0
                                          • Categories
                                          • Recent
                                          • Tags
                                          • Popular
                                          • World
                                          • Users
                                          • Groups