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 83 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.
  • C CPallini

    Quote:

    but any calls to the method are also removed.

    Wouldn't that be disastrous if the method is intended to produce side-effects?

    M Offline
    M Offline
    Mladen Jankovic
    wrote on last edited by
    #20

    Judging by the restrictions imposed on partial methods: they cannot have return value (only void) or out parameters, they are intended to have side effects. Whether or not it is disastrous to skip side effect is another question. In original example (logging) it is not.

    C 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

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

      It was heavily used in Entity Framework DB First. I've used them a lot to extend generated classes with my own code. I've never used it for my own classes or outside of generated code.

      Best, Sander sanderrossel.com Continuous Integration, Delivery, and Deployment arrgh.js - Bringing LINQ to JavaScript Object-Oriented Programming in C# Succinctly

      realJSOPR M 2 Replies 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!

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

        Right. We use partial classes/methods all the time with EF. You extend and EF entity with extra properties and methods if needed. The entity can change in EF, but the partial class/method remains the same. Very useful. use it all the time.

        1 Reply Last reply
        0
        • realJSOPR realJSOP

          It's still pointless.

          ".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
          Slacker007
          wrote on last edited by
          #23

          It is most certainly NOT pointless. You just haven't found a use case for it yet. :)

          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

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

            That's not cool - it's obfuscatory.

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

            R 1 Reply Last reply
            0
            • Sander RosselS Sander Rossel

              It was heavily used in Entity Framework DB First. I've used them a lot to extend generated classes with my own code. I've never used it for my own classes or outside of generated code.

              Best, Sander sanderrossel.com Continuous Integration, Delivery, and Deployment arrgh.js - Bringing LINQ to JavaScript Object-Oriented Programming in C# Succinctly

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

              Sander Rossel wrote:

              It was heavily used in Entity Framework DB First.

              Well, there's your problem - you're using Entity Framework...

              Sander Rossel wrote:

              I've used them a lot to extend generated classes with my own code.

              I prefer to generate my own code.

              ".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
              • M Mladen Jankovic

                Judging by the restrictions imposed on partial methods: they cannot have return value (only void) or out parameters, they are intended to have side effects. Whether or not it is disastrous to skip side effect is another question. In original example (logging) it is not.

                C Offline
                C Offline
                CPallini
                wrote on last edited by
                #26

                Yes, indeed. The original example showed a safe usage.

                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

                  Richard DeemingR Offline
                  Richard DeemingR Offline
                  Richard Deeming
                  wrote on last edited by
                  #27

                  Of course, you could always simplify that to:

                  using System;
                  using System.Diagnostics;

                  namespace ConditionalMethodTest
                  {
                  class Foo
                  {
                  [Conditional("LOGGING")]
                  private void Log(string msg)
                  {
                  Console.WriteLine(msg);
                  }

                      public void DoSomething()
                      {
                          Log("Fizbin");
                      }
                  }
                  
                  static class Program
                  {
                      static void Main()
                      {
                          new Foo().DoSomething();
                      }
                  }
                  

                  }

                  for exactly the same effect. :) ConditionalAttribute Class (System.Diagnostics) | Microsoft Docs[^]


                  "These people looked deep within my soul and assigned me a number based on the order in which I joined." - Homer

                  "These people looked deep within my soul and assigned me a number based on the order in which I joined" - Homer

                  M 1 Reply Last reply
                  0
                  • realJSOPR realJSOP

                    That's not cool - it's obfuscatory.

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

                    R Offline
                    R Offline
                    Rick York
                    wrote on last edited by
                    #28

                    Exactly what I was going to write.

                    "They have a consciousness, they have a life, they have a soul! Damn you! Let the rabbits wear glasses! Save our brothers! Can I get an amen?"

                    1 Reply Last reply
                    0
                    • C CPallini

                      Quote:

                      but any calls to the method are also removed.

                      Wouldn't that be disastrous if the method is intended to produce side-effects?

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

                      CPallini wrote:

                      Wouldn't that be disastrous if the method is intended to produce side-effects?

                      Where I used to work, that was an every day occurrence, both in the code and in the meetings with the managers.

                      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

                      C 1 Reply Last reply
                      0
                      • Sander RosselS Sander Rossel

                        It was heavily used in Entity Framework DB First. I've used them a lot to extend generated classes with my own code. I've never used it for my own classes or outside of generated code.

                        Best, Sander sanderrossel.com Continuous Integration, Delivery, and Deployment arrgh.js - Bringing LINQ to JavaScript Object-Oriented Programming in C# Succinctly

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

                        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 1 Reply Last reply
                        0
                        • M Marc Clifton

                          CPallini wrote:

                          Wouldn't that be disastrous if the method is intended to produce side-effects?

                          Where I used to work, that was an every day occurrence, both in the code and in the meetings with the managers.

                          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

                          C Offline
                          C Offline
                          CPallini
                          wrote on last edited by
                          #31

                          :-D :thumbsup:

                          1 Reply Last reply
                          0
                          • Richard DeemingR Richard Deeming

                            Of course, you could always simplify that to:

                            using System;
                            using System.Diagnostics;

                            namespace ConditionalMethodTest
                            {
                            class Foo
                            {
                            [Conditional("LOGGING")]
                            private void Log(string msg)
                            {
                            Console.WriteLine(msg);
                            }

                                public void DoSomething()
                                {
                                    Log("Fizbin");
                                }
                            }
                            
                            static class Program
                            {
                                static void Main()
                                {
                                    new Foo().DoSomething();
                                }
                            }
                            

                            }

                            for exactly the same effect. :) ConditionalAttribute Class (System.Diagnostics) | Microsoft Docs[^]


                            "These people looked deep within my soul and assigned me a number based on the order in which I joined." - Homer

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

                            Richard Deeming wrote:

                            for exactly the same effect.

                            Yes, but that's not nearly as confusing!

                            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

                            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

                              S Offline
                              S Offline
                              Super Lloyd
                              wrote on last edited by
                              #33

                              Remove partial method, you just broke all WPF user controls! :((

                              A new .NET Serializer All in one Menu-Ribbon Bar Taking over the world since 1371!

                              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

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

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

                                  K Offline
                                  K Offline
                                  kalberts
                                  wrote on last edited by
                                  #35

                                  When I learned about partial functions, I immediately embraced it. My project at that time was a protocol stack, with data transport, control and management planes. Each plane is a set of functions completely separated from the other two, but operating on the same protocol layer entity. Partial fuctions allowed me to keep the implementation of the planes completely independent of each other. Obviously, very plane specific functions didn't make use of partial functions, but for e.g. initialization and cleanup it was great. Once you get going with partial functions, you add e.g. logging functions without cluttering up the plain code. When you write test functions, you write a partial function to verify entry and/or exit conditions. In the development phase, you add extensive parameter validation that would be too expensive to run in production code, or you add code for performance measurenment and statitics to detect bottlenecks and excessive resource reuqirements, so they can be fixed before the product is released. Yes, you can put it all into one huge, messy code file with a lot of #ifdef to disable parts of the code. The programmer responsisible for one of the planes will have to wade through lots of code that is ireelevant to his tasks. An update in any of the planes, the log functions, the performance measurement functions, the test and validation functions,... invalidates the source file of every other programmer on the project. Obviously it can be done that way. But I love the partial functions, considering that to be a much better way to do it.

                                  1 Reply Last reply
                                  0
                                  • 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
                                          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