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 extension and let us call the external naming area a unique type name for the compiler.

What is the reason for the technical disapproval:

  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); }}}   

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:

  public class Foo {private bool _field; Public Static Class Extension {Public Stable Bull GetField (Foo Foo) {Return foo._field; }}}   

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:

  bool fieldValue = Foo.Extensions.GetField (new Foo ());   

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:

  public class Foo {private bool _field; Public Static Class Extension {Public Stable Bull GetField (* It * Foo Foo) // Not Permission, Error Compilation {Return foo._field; }}}   

You can then type the following method, from which the extension method behaves like a little more example example method:

  var Foo = New Foo (); Var iGotAPrivateField = foo.GetField ();    

Comments

Popular posts from this blog

java - NullPointerException for a 2d Array -

python - Assemble mpeg file unable to play in mediaplayer -

c# - NameSpace Manager or XsltContent to parse aspx page -