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

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

    Quote:

    but any calls to the method are also removed.

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

    D M M 3 Replies 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?

      D Offline
      D Offline
      den2k88
      wrote on last edited by
      #10

      CPallini wrote:

      if the method is intended to produce side-effects

      If the programmer is a donkey, giving him/her a compiler is disastrous.

      GCS d--(d+) s-/++ a C++++ U+++ P- L+@ E-- W++ N+ o+ K- w+++ O? M-- V? PS+ PE- Y+ PGP t+ 5? X R+++ tv-- b+(+++) DI+++ D++ G e++ h--- r+++ y+++*      Weapons extension: ma- k++ F+2 X

      C 1 Reply Last reply
      0
      • D den2k88

        CPallini wrote:

        if the method is intended to produce side-effects

        If the programmer is a donkey, giving him/her a compiler is disastrous.

        GCS d--(d+) s-/++ a C++++ U+++ P- L+@ E-- W++ N+ o+ K- w+++ O? M-- V? PS+ PE- Y+ PGP t+ 5? X R+++ tv-- b+(+++) DI+++ D++ G e++ h--- r+++ y+++*      Weapons extension: ma- k++ F+2 X

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

        Unless you are haskelling yourself too much, most methods do have side effects.

        D 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

          Kornfeld Eliyahu PeterK Offline
          Kornfeld Eliyahu PeterK Offline
          Kornfeld Eliyahu Peter
          wrote on last edited by
          #12

          I'm not sure such a code-remove by the compiler is 'cool'... While removing unused code while optimizing is good thing, removing unimplemented code just because it signed 'partial' definitely illogical... And to be honest - why would I define a private method and not implement it? It is definitely smells like a language feature added to support half-visual development with code generators... Outside of that context I see it very disturbing...

          "The greatest enemy of knowledge is not ignorance, it is the illusion of knowledge". Stephen Hawking, 1942- 2018

          "It never ceases to amaze me that a spacecraft launched in 1977 can be fixed remotely from Earth." ― Brian Cox

          C OriginalGriffO 2 Replies 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

            R Offline
            R Offline
            RugbyLeague
            wrote on last edited by
            #13

            I can't say I have ever found a use for it - although that might be because I didn't know it existed until your post :)

            1 Reply Last reply
            0
            • Kornfeld Eliyahu PeterK Kornfeld Eliyahu Peter

              I'm not sure such a code-remove by the compiler is 'cool'... While removing unused code while optimizing is good thing, removing unimplemented code just because it signed 'partial' definitely illogical... And to be honest - why would I define a private method and not implement it? It is definitely smells like a language feature added to support half-visual development with code generators... Outside of that context I see it very disturbing...

              "The greatest enemy of knowledge is not ignorance, it is the illusion of knowledge". Stephen Hawking, 1942- 2018

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

              Yes, code generators me think.

              1 Reply Last reply
              0
              • Kornfeld Eliyahu PeterK Kornfeld Eliyahu Peter

                I'm not sure such a code-remove by the compiler is 'cool'... While removing unused code while optimizing is good thing, removing unimplemented code just because it signed 'partial' definitely illogical... And to be honest - why would I define a private method and not implement it? It is definitely smells like a language feature added to support half-visual development with code generators... Outside of that context I see it very disturbing...

                "The greatest enemy of knowledge is not ignorance, it is the illusion of knowledge". Stephen Hawking, 1942- 2018

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

                The people defining and the people using may be different people - this is only applicable to partial classes, remember - so there is a very good chance that the partial definition and its implementation are in different files, just as InitializeComponent is defined in the designer.cs file of a WinForms app, and called from the .cs file. I've not used it, but I can see advantages in a "team written" class, in that I write a "frame" which calls partial methods that are implemented by other team members in different files. As has been mentioned, it could be very useful for automated designers, allowing the user to define methods only when he needs them without having to mess around with delegates. Or tracing information only available in debug builds, which isn't even called in release giving slightly higher performance.

                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!

                "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

                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
                  Johnny J
                  wrote on last edited by
                  #16

                  Dunno. If I had a method whereby I could do just a part of my work, that would definitely be desired... :rolleyes:

                  Anything that is unrelated to elephants is irrelephant
                  Anonymous
                  -----
                  The problem with quotes on the internet is that you can never tell if they're genuine
                  Winston Churchill, 1944
                  -----
                  Never argue with a fool. Onlookers may not be able to tell the difference.
                  Mark Twain

                  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

                    P Offline
                    P Offline
                    PIEBALDconsult
                    wrote on last edited by
                    #17

                    I agree.

                    1 Reply Last reply
                    0
                    • C CPallini

                      Unless you are haskelling yourself too much, most methods do have side effects.

                      D Offline
                      D Offline
                      den2k88
                      wrote on last edited by
                      #18

                      "Side" effects are really something that need to be reduced. Methods should have only intended effects and possibly no side effect. If a method is to be implemented by the user of a class then it is part of the 'contract' to be respected when writing the user component - meaning it's up to the end user (programmer) to implement all the required effects and side effects.

                      GCS d--(d+) s-/++ a C++++ U+++ P- L+@ E-- W++ N+ o+ K- w+++ O? M-- V? PS+ PE- Y+ PGP t+ 5? X R+++ tv-- b+(+++) DI+++ D++ G e++ h--- r+++ y+++*      Weapons extension: ma- k++ F+2 X

                      C 1 Reply Last reply
                      0
                      • D den2k88

                        "Side" effects are really something that need to be reduced. Methods should have only intended effects and possibly no side effect. If a method is to be implemented by the user of a class then it is part of the 'contract' to be respected when writing the user component - meaning it's up to the end user (programmer) to implement all the required effects and side effects.

                        GCS d--(d+) s-/++ a C++++ U+++ P- L+@ E-- W++ N+ o+ K- w+++ O? M-- V? PS+ PE- Y+ PGP t+ 5? X R+++ tv-- b+(+++) DI+++ D++ G e++ h--- r+++ y+++*      Weapons extension: ma- k++ F+2 X

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

                        Intended-side-effects were intended :-D. That is side-effects in the functional meaning (in constrast with 'pure' functions, Haskell was the hint). Unintended side effects are just evil.

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