c# - Why not allow Extension method definition in nested class? -
I will be convenient / rational to write my expansions for a class in the nested class, the main reason is that I just Let me name the class What is the reason for the technical disapproval: While now I have to create a separate free class. Update / Note: I was not thinking this was the fact that an internal class will affect the scope of the availability of the extension method. I just wanted to address the practical coding issue with a different class with a different name. The main point here The following code works: Here you are clearly going through an example of a square, and a steady method is allowed to reach a private area ... it seems appropriate: However, although extension methods are only an alternative syntax for static methods, they are implemented just like non-static example methods. Now if extended methods were allowed in nested classes, they could actually use private areas, and they are very close to example methods. This may have some unexpected results. / P> In short, if this permission was: You can then type the following method, from which the extension method behaves like a little more example example method: extension and let us call the external naming area a unique type name for the compiler.
Public Sector Fu {ObjectSet & lt; Bars & gt; Bar {receive; Set; } Public Static Class Extensions {Public Static Bar Bye Names (This Object Siteme & lt; Bar & gt; Bar, String Name) {return bars.FirstOrDefault (c => c.Name == Name); }}}
public class Foo {private bool _field; Public Static Class Extension {Public Stable Bull GetField (Foo Foo) {Return foo._field; }}}
bool fieldValue = Foo.Extensions.GetField (new Foo ());
public class Foo {private bool _field; Public Static Class Extension {Public Stable Bull GetField (* It * Foo Foo) // Not Permission, Error Compilation {Return foo._field; }}}
var Foo = New Foo (); Var iGotAPrivateField = foo.GetField ();
Comments
Post a Comment