Do extension methods help in a practical way not to be part of a static class or [theoretically] part of a namespace?

When it comes to extension methods, class names don't seem to do anything, but provide a grouping, which is what namespaces do. As soon as I turn on the namespace, I get all the extension methods in the namespace. So my question boils down to the following: is there any value that I can get from extension methods found in a static class?

I understand that this is a compiler requirement for them to be placed in a static class, but it seems organizationally reasonable for it to be legal to allow extension methods to define in namespaces without classes surrounding them. To paraphrase the above question in another way: is there any practical benefit or help in any scenario that I get from the developer, if you have extension methods attached to the class or attached to the namespace?

Basically, I just want to get some kind of intuition, confirmation or understanding. I suspect that it was easiest to implement extension methods this way and did not cost time to allow extension methods to exist independently in namespaces.

+6
source share
4 answers

You may find a satisfactory answer on Eric Lippert's blog. Why does C # not implement top-level methods? (in turn caused by the SO question Why C # does not allow functions other than members, such as C ++ ), where (my emphasis):

They ask me: "Why does C # not implement the function X?" all the time. the answer is always the same: because no one has ever designed, specified, implemented, tested, documented and shipped. All six of these things are needed to make a function. All of them cost a huge amount of time, effort and money. The features are not cheap, and we try very hard to make sure that we only ship these features that give the best benefits to our users, given our limited time, effort and money budgets.

I understand that such a general answer probably does not address a specific question.

In this particular case, the apparent benefit to users in the past was not large enough to justify the complications of the language that is coming. By restricting how different language objects nest inside each other, we (1) restrict legal programs to be generally easy to understand and (2) define a “lookup identifier” that is understandable, understandable, implementable, verifiable and documentary.

By restricting the method bodies to always be inside a structure or class, we make it easier to talk about the meaning of an unqualified identifier used in the context of a call; such a thing is always an invocable member of the current type (or base type).

+3
source

I put them in a class all about grouping related functions inside a class. You can have multiple extension methods in the same namespace. If I wanted to write some extension methods for the DirectoryInfo and FileInfo classes, I would create two classes in the IO namespace called DirectoryInfoExtensions and FileInfoExtensions.

You can still call extension methods, like any other static method. I don’t know how the compiler works, but maybe the output assembly, if compiled for .net 2, can still be used by legacy .net structures. It also means that the existing reflection library can work and be used to run extension methods without any changes. Again, I'm not an expert on the compiler, but I think the keyword “this” in the context of the extension method is to allow syntactic sugar, which allows us to use the methods as if they belonged to an object.

+1
source

The .NET Framework requires that each method exist in a class that is inside the assembly. The language can allow methods or fields to be declared without an explicitly defined class, put all such methods in the Fnord assembly into a class named Fnord_TopLevelDefault , and then look for the Fnord_TopLevelDefault class of all assemblies when performing a method search; The CLS specification needs to be expanded to make this feature work seamlessly for mixed language projects. As with extension methods, this behavior can be compatible with CLS if CLS did not confirm it, because code in a language that did not use such a function could use the Foo free-floating method in the Fnord assembly by writing it Fnord_TopLevelDefault.Foo , but that would be a little ugly.

A more interesting question is to what extent the acceptability of calling the Foo extension method from an arbitrary class that does not require a clearly visible reference to this class is less evil than allowing static methods without extension to be called as well. I do not think that Math.Sqrt(x) really more readable than Sqrt ; even if you do not want to import Math everywhere, the ability to do this, at least locally, can in some cases significantly improve the clarity of the code.

+1
source

They can refer to other static members of the class internally.

You must not only take into account the consumer aspect, but also the code maintenance aspect.

Despite the fact that intellisense is not different with respect to the owner class, information still exists with the help of tips and any productivity tools that you have added to your development environment. This can easily be used to provide some context for the method in what would otherwise be a flat (and sometimes very long) list.

Consumer mind, the bottom line, I do not think it matters a lot.

0
source

Source: https://habr.com/ru/post/914267/


All Articles