0

In Visual Studio 2012 I'd like to navigate to the MapHttpRoute method from the HttpRouteCollection class.

So for example if I go to the definition of Routes of Routes.MapHttpRoute(), I see Routes is an instance of HttpRouteCollection class. However if I check the definition of HttpRouteCollection I can't find the MapHttpRoute method. Are extension methods hidden?

Angel Cloudwalker
  • 2,015
  • 5
  • 32
  • 54

1 Answers1

1

No, they aren't hidden. They are extension methods that reside in the HttpRouteCollectionExtensions static class, and since you're using an instance of HttpRouteCollection, you're seeing them as an available API.

enter image description here

Another way you can tell, should this scenario come up in the future, is to look at the documentation for the method in question.

Mark C.
  • 6,332
  • 4
  • 35
  • 71
  • I can get to this if I right click on MapHttpRoute() to see it's definition. My question is from the HttpRouteCollection class, how would I know these extension methods exist for that class? – Angel Cloudwalker Jan 11 '17 at 17:21
  • You wouldn't, simply from looking at the `HttpRouteCollection` class, since it is not static. If the `HttpRouteCollection` class was static, I'm sure these methods would reside in that class. There are existings [answers](http://stackoverflow.com/questions/857489/how-i-can-find-all-extension-methods-in-solution) on how to find all extension methods, which may help answer your question – Mark C. Jan 11 '17 at 17:28
  • So how does HttpRouteCollectionExtensions extend to HttpRouteCollection? From your picture I only see that HttpRouteCollectionExtensions implements IHttpRoute. – Angel Cloudwalker Jan 11 '17 at 17:36
  • 1
    Look at the first parameter in the methods, an instance of `HttpRouteCollection`, using the `this` keyword makes them "extension methods." You couldn't write `var s = "hello"; s.MapHttpRoute()` because `s` is a string, not an instance of `HttpRouteCollection`. If you are looking for some white paper, you should google extension methods in C#. – Mark C. Jan 11 '17 at 17:40
  • your explanation of this keyword was very helpful, thanks for this! – Angel Cloudwalker Jan 11 '17 at 17:43
  • @MilesMorales no problem - glad I could help. – Mark C. Jan 11 '17 at 17:44