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. Pointless .Net Feature of the Day

Pointless .Net Feature of the Day

Scheduled Pinned Locked Moved The Lounge
csharp
45 Posts 24 Posters 57 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.
  • D Dominic Burford

    I thought this too. What the hell use are partial methods. The only reasonable use I have found for them is when using Xamarin to build an app. You may have a class defined in both the iOS and Android projects and defining a partial class makes sense.

    "There are two ways of constructing a software design: One way is to make it so simple that there are obviously no deficiencies, and the other way is to make it so complicated that there are no obvious deficiencies. The first method is far more difficult." - C.A.R. Hoare Home | LinkedIn | Google+ | Twitter

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

    I'm not saying partial classes aren't reasonable (hell, I use partial classes), I'm saying partial METHODS aren't.

    ".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

    D 1 Reply Last reply
    0
    • realJSOPR realJSOP

      I'm not saying partial classes aren't reasonable (hell, I use partial classes), I'm saying partial METHODS aren't.

      ".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

      D Offline
      D Offline
      Dominic Burford
      wrote on last edited by
      #37

      I can't think of a single use for a partial method either.

      "There are two ways of constructing a software design: One way is to make it so simple that there are obviously no deficiencies, and the other way is to make it so complicated that there are no obvious deficiencies. The first method is far more difficult." - C.A.R. Hoare Home | LinkedIn | Google+ | Twitter

      1 Reply Last reply
      0
      • realJSOPR realJSOP

        Partial methods. I'll let you do your own research, but I cannot identify a single case where the use of a partial method would be beneficial, desired, or warranted.

        ".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

        O Offline
        O Offline
        Olfello
        wrote on last edited by
        #38

        I do not agree. We have a number of WCF/SOAP projects where we generate code from wsdl and same thing with XML generating code from XSD. I both cases it is beneficial to have additional methods or properties in a separate file. When XSD or WSDL is updated you code is not overwritten during regeneration.

        realJSOPR 1 Reply Last reply
        0
        • M Marc Clifton

          Sander Rossel wrote:

          It was heavily used in Entity Framework DB First.

          Ah. That explains it. They made a language enhancement to support a bad design to begin with! ;)

          Latest Article - A Concise Overview of Threads 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 Artificial intelligence is the only remedy for natural stupidity. - CDP1802

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

          Marc Clifton wrote:

          They made a language enhancement to support a bad design to begin with!

          That's truth in advertising right there... :)

          ".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
          • O Olfello

            I do not agree. We have a number of WCF/SOAP projects where we generate code from wsdl and same thing with XML generating code from XSD. I both cases it is beneficial to have additional methods or properties in a separate file. When XSD or WSDL is updated you code is not overwritten during regeneration.

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

            Once again, this discussion isn't about partial classes - it's about partial METHODS.

            ".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
            • realJSOPR realJSOP

              Partial methods. I'll let you do your own research, but I cannot identify a single case where the use of a partial method would be beneficial, desired, or warranted.

              ".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

              J Offline
              J Offline
              johannesnestler
              wrote on last edited by
              #41

              No Research needed. It's for extending generated Code that may will be re-generated later (so you can not change it because your changes would be overriden). E.g. I use it to extend EF generated Contexts. It's a very nice feature and .NET has many not everyone will use, so don't. But if you do Frameworks or template-code or big systems (or WindowsForms UI) it's very handy…

              O 1 Reply Last reply
              0
              • G GKP1992

                Where is YAGNI when you need it. Google trends[^] results sort of agree with your feelings, as do I.

                M Offline
                M Offline
                M chael Luna
                wrote on last edited by
                #42

                Definitely YAGNI. Will definitely be asked about it in a job interview.

                1 Reply Last reply
                0
                • M Marc Clifton

                  The cool thing in the code below is that if you don't define LOGGING, not only does the partial method go away, but any calls to the method are also removed.

                  #define LOGGING

                  using System;

                  namespace PartialMethodTest
                  {
                  partial class Foo
                  {
                  partial void Log(string msg);

                      public void DoSomething()
                      {
                          Log("Fizbin");
                      }
                  }
                  

                  #if LOGGING
                  partial class Foo
                  {
                  partial void Log(string msg)
                  {
                  Console.WriteLine(msg);
                  }
                  }
                  #endif

                  class Program
                  {
                      static void Main(string\[\] args)
                      {
                          new Foo().DoSomething();
                      }
                  }
                  

                  }

                  Latest Article - A Concise Overview of Threads 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 Artificial intelligence is the only remedy for natural stupidity. - CDP1802

                  A Offline
                  A Offline
                  agolddog
                  wrote on last edited by
                  #43

                  C'mon Marc, that's not a valid example. Everybody of our age range knows fizzbin has two z's. ;)

                  1 Reply Last reply
                  0
                  • J johannesnestler

                    No Research needed. It's for extending generated Code that may will be re-generated later (so you can not change it because your changes would be overriden). E.g. I use it to extend EF generated Contexts. It's a very nice feature and .NET has many not everyone will use, so don't. But if you do Frameworks or template-code or big systems (or WindowsForms UI) it's very handy…

                    O Offline
                    O Offline
                    obermd
                    wrote on last edited by
                    #44

                    Finally, a good answer to what Partial Methods are good for.

                    1 Reply Last reply
                    0
                    • OriginalGriffO OriginalGriff

                      It's for use in a partial class, so you could declare the partial method in the designer code for a WinForms app for example, and implement it in the user defined code for the same class. The difference is that the method can be called from the designer code because it will compile cleanly. This is still the case if the implementation of the partial method is never provided: the compiler will remove the partial method and the call to it if the concrete version is not written, and you still won't get a compiler error. Think of it as a sort-of optional private abstract method and you're about there.

                      Sent from my Amstrad PC 1640 Never throw anything away, Griff Bad command or file name. Bad, bad command! Sit! Stay! Staaaay... AntiTwitter: @DalekDave is now a follower!

                      J Offline
                      J Offline
                      Jalapeno Bob
                      wrote on last edited by
                      #45

                      I use it frequently. I find it simpler to use than Interface.

                      __________________ Lord, grant me the serenity to accept that there are some things I just can’t keep up with, the determination to keep up with the things I must keep up with, and the wisdom to find a good RSS feed from someone who keeps up with what I’d like to, but just don’t have the damn bandwidth to handle right now. © 2009, Rex Hammock

                      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