none
Extension methods with no implementation, but not abstract? RRS feed

  • Question

  • I am trying to relearn C# and came across something today that does not make sense to me.  

    The methods in the below image are clearly extension methods (not only does the class name indicate that, but so do the method signatures - having 'this' with the first parameter).   That's fine; I get extension methods in general.

    The methods have no implementation, no curly braces, and end with a semicolon: as far as I know, that's a clear indication that they are abstract methods.  That would be fine too, since I understand abstract methods.  But the methods don't have the word abstract in their declaration, and neither does the class (if any method in a class is abstract, then the class has to be abstract also).

    Maybe it has something to do with the extension methods being for an interface, IWebHost.  But that doesn't seem to work (in my mind) because interface method declarations don't have access modifiers and cannot be static (these methods are public and static), and the enclosing entity is defined to be a class, not an interface.

    Can someone please explain?


    namespace Microsoft.AspNetCore.Hosting
    {
      public static class WebHostExtensions
      {
        public static void Run(this IWebHost host);
        public static Task RunAsync(this IWebHost host, ...);
        public static Task StopAsync(this IWebHost host, ...);
        public static void WaitForShutdown(this IWebHost host);
        public static Task WaitForShutdownAsync(this IWebHost host, ...);
      }
    }



    • Edited by RP2691 Sunday, July 28, 2019 12:01 AM
    Saturday, July 27, 2019 11:59 PM

Answers

  • You probably are seeing a description, which is generated by Visual Studio when you press <F12> or execute “Go to Definition” from context menu. In case of library functions without sources, it only shows the declaration and documentation comments for information purposes. It is not a fully valid file.




    • Edited by Viorel_MVP Sunday, July 28, 2019 5:22 AM
    • Marked as answer by RP2691 Sunday, July 28, 2019 1:52 PM
    Sunday, July 28, 2019 5:19 AM

All replies

  • Hello,

    Here is the source code which should assist you for those extensions.

    https://github.com/aspnet/Hosting/blob/master/src/Microsoft.AspNetCore.Hosting/WebHostExtensions.cs


    Please remember to mark the replies as answers if they help and unmarked them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.

    NuGet BaseConnectionLibrary for database connections.

    StackOverFlow
    profile for Karen Payne on Stack Exchange

    Sunday, July 28, 2019 12:46 AM
    Moderator
  • You probably are seeing a description, which is generated by Visual Studio when you press <F12> or execute “Go to Definition” from context menu. In case of library functions without sources, it only shows the declaration and documentation comments for information purposes. It is not a fully valid file.




    • Edited by Viorel_MVP Sunday, July 28, 2019 5:22 AM
    • Marked as answer by RP2691 Sunday, July 28, 2019 1:52 PM
    Sunday, July 28, 2019 5:19 AM
  • Yes, I used "Go to Definition" to get there.  Thanks.




    Sunday, July 28, 2019 1:54 PM