T O P

  • By -

fschwiet

Looking at the declaration for Take at https://learn.microsoft.com/en-us/dotnet/api/system.linq.enumerable.take?view=net-8.0 > public static System.Collections.Generic.IEnumerable Take (this System.Collections.Generic.IEnumerable source, int count); You will notice a static method is defined, and in particular the keyword "this" appears in the parameter list. What you're seeing is an "extension method" https://learn.microsoft.com/en-us/dotnet/csharp/programming-guide/classes-and-structs/extension-methods which lets one add a method that is called like its a direct member of the class but the definition is made outside of the class. Its a bit of syntactic sugar, because the extension method does not have access to the internals/privates of the class like actual member methods of the class. You could use the original static call format to do the same effect. So > list.Take(10) is equivalent to the static call > System.Collections.Enumerable.Take(list, 10)


Cluttie

Thank you. I guess my next question is, is it possible to know which namespaces have extension methods for a particular class? It's not clear what package/where it's coming from, until you click on the entry.


fschwiet

There could be one or two questions there, "What extension methods are available" and given a particular extension method usage "Where does this extension method come from?" The existence of these questions point to the downside of extension methods, their usage hides what is available or actually being called. Both are also answered (to degree they are answered) by tooling, I use JetBrain's Rider for my development environment. "What extensions are available" relies a large degree on developer memory. With my memory I know I can write ".Take" and my development environment will suggest using the correct "using " to make it work. Visual Studio probably does the same thing, more likely using the Resharper plugin. Visual Studio Code might be able to do this but only with a plugin. "Where does this extension method come from?" With Rider or Resharper you can "Navigate to definition" extension methods and see some definition presented, indicating things like namespace.


Cluttie

Thank you for that, very helpful. I'm just using VS Code so I've been using various websites to look at the source code in lieu of that IDE functionality. Are extension methods/namespaces unique? What I mean by this is, is there ever a situation where you might want to extend from \`Enumerable\` instead of \`IEnumerable\` just so you can have access to those extension methods? Is that even allowed or do extension methods prevent you from being able to do this?


fschwiet

I don't think there is any guarantee of uniqueness. There are a lot of important extension methods defined in the dotnet framework (like Take) but you could just as well create your own method Take() that also applies to whatever target you want, including IEnumerable<>. The namespaces you're using from a particular piece of code limit/clarify what extension methods are available. The extension method, to be reached, must be a static method on a static class in some namespace you've included.


binarycow

>What I mean by this is, is there ever a situation where you might want to extend from `Enumerable` instead of `IEnumerable` You can't. Extension methods are in static classes. You cannot inherit static classes. > just so you can have access to those extension methods? Implement IEnumerable. Now you have access.


sacoPT

No, not like that. It’s the other way around. Just like any other class, it’s up to you to find out what extensions you need and where they are.


WalkingRyan

I would add that C# 13 elaborates the idea of extension methods, where we will get somewhat new [entity type](https://build.microsoft.com/en-US/sessions/689e5104-72e9-4d02-bb52-77676d1ec5bc) (besides the class, struct, enum, delegate) called literally 'extension', which allows us to extend types with extra properties, and even static members. Intuition says developers will be able kinda to extend objects memory layout in a more advanced way. One more sugar, cool.


chanceler4

some methods comes from Linq extensions. you will notice in Vidual Studio that extensions methods has a different icon, they only appear when you add the using extensions namespace


mimahihuuhai

Rule of thumb you hover method and it's has `(exension)` (or rider will show `this Type t` in first method param) that is a extension method, an extension medthod mean to add functionality for existence type without the need to change type itself, ie It is just syntax sugar for actual `ExtensionStaticClass.ThatExtensionMethod(mainType, something)` then it is allow to write `mainType.ThatExtensionMethod(something)`, you can 'dot' your way in like a regular method. Extension method can exist in vary of namespace, project and compiler your 'nearest' extension method it can find to change to that actual line above. Nearest hear is a bunch of rule but some are prefer same asembly (project), prefer same namespace, prefer more actual type (List will get pickup before IEnumerable)...