Have you ever used private extension methods?
Is there any advantage of having the option of using private extension methods? I haven't found any use for them whatsoever. Wou开发者_JS百科ldn't it be better if C# didn't allow them at all?
This is helpful for improving the syntax of code within a method/class, but you don't want to expose the functionality offered by that extension method to other areas of the codebase. In other words as an alternative for regular private static helper methods
Consider the following:
public class MyClass
{
public void PerformAction(int i) { }
}
public static class MyExtensions
{
public static void DoItWith10(this MyClass myClass)
{
myClass.DoIt(10);
}
public static void DoItWith20(this MyClass myClass)
{
myClass.DoIt(20);
}
private static void DoIt(this MyClass myClass, int i)
{
myClass.PerformAction(i);
}
}
I realize that the example does not make much sense in its current form, but I'm sure you can appreciate the possibilities that private extension methods provide, namely the ability to have public extension methods that use the private extension for encapsulation or composition.
I just Googled to investigate as I was doubtful that there was much use for them. This however is an excellent illustrative application for them:
http://odetocode.com/blogs/scott/archive/2009/10/05/private-extension-methods.aspx
精彩评论