Pointless .Net Feature of the Day
-
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);
}
}
#endifclass 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
-
Quote:
but any calls to the method are also removed.
Wouldn't that be disastrous if the method is intended to produce side-effects?
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
-
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
-
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);
}
}
#endifclass 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
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'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, 2013I 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 :)
-
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
-
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
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!
-
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, 2013Dunno. 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 -
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, 2013I agree.
-
"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
-
"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
-
Quote:
but any calls to the method are also removed.
Wouldn't that be disastrous if the method is intended to produce side-effects?
Judging by the restrictions imposed on partial methods: they cannot have return value (only
void
) orout
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. -
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, 2013It 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
-
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!
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.
-
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, 2013It is most certainly NOT pointless. You just haven't found a use case for it yet. :)
-
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);
}
}
#endifclass 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
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 -
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
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 -
Judging by the restrictions imposed on partial methods: they cannot have return value (only
void
) orout
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. -
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);
}
}
#endifclass 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
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
-
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