-5

I saw on a C# project (I don't remember which one) a piece of code that looks like this:

public static class ComExt
{
    // ... blablabla ...

    static public void QueryService<T>(this IServiceProvider serviceProvider, out T service) where T : class
    {
        // ... blablabla ...
    }

    static public int QueryInterface(this object provider, ref Guid riid, ref IntPtr ppvObject)
    {
        // ... blablabla ...
    }

but in the first method, I don't understand the syntax. especially the QueryService<T> and (this and ) where T : class

is someone can explain to me what's happening in that method? and maybe other example using that syntax?

Thanks :)

Lenor
  • 1,471
  • 10
  • 19
  • 1
    This is an extension method – Pavel Anikhouski Jul 12 '19 at 12:31
  • 3
    read about generics https://learn.microsoft.com/en-gb/dotnet/csharp/programming-guide/generics/generic-methods and extensions https://learn.microsoft.com/en-gb/dotnet/csharp/programming-guide/classes-and-structs/extension-methods – demo Jul 12 '19 at 12:31
  • 1
    for the this, read [this link](https://www.google.com/url?sa=t&rct=j&q=&esrc=s&source=web&cd=1&cad=rja&uact=8&ved=2ahUKEwi79vaosa_jAhWiyqYKHSFPA9cQFjAAegQIAhAB&url=https%3A%2F%2Flearn.microsoft.com%2Fen-us%2Fdotnet%2Fcsharp%2Fprogramming-guide%2Fclasses-and-structs%2Fextension-methods&usg=AOvVaw1JdTPnXmpCuura1Sgtd7tx) for the where T, read [this link](https://learn.microsoft.com/en-us/dotnet/csharp/programming-guide/generics/generic-methods) – skolldev Jul 12 '19 at 12:32
  • 1
    simple research will return a lot of informations. Like https://stackoverflow.com/questions/4737970/what-does-where-t-class-new-mean – Steve Jul 12 '19 at 12:32
  • 2
    `this` stands for *extension method*: in addition to `ComExt.QueryService(myProvider, out service);` it allowes `myProvider.QueryService(out service);` syntax: *as if* `myProvider` has `QueryService` method – Dmitry Bychenko Jul 12 '19 at 12:32

1 Answers1

2

The problem of your question is that the title is done that way, probably nobody will search it and answering here probably won't bring any points later :).

As in the comments were mentioned these are extension methods. It means that they can extend some regular classes' functions. Some information about extension methods:

  1. They should be declared in a static class;
  2. The extension class should be the first class in the file. (In C# you can have more than one class in one file, but extension class will work only if it is the first in the file);
  3. Extension functions should be static as well because there is a dynamic member following the keyword "this" which is extended;
  4. Extension function's first parameter should be starting with "this" keyword (mentioned above) as it tells the compiler which object is extended.

In your case: static public void QueryService<T>(this IServiceProvider serviceProvider, out T service) where T : class means that thanks to this function, any IServiceProvider type object will be able to have additional (extended) function QueryService which will take generic type which should be class (not struct) see: where T : class.

out T service means that passed parameter can be not initialized (as out doesn't need parameter initialization despite ref).

Usage will be something like this:

CustomServiceType myService; 
IServiceProvider serviceProvider = new TypeWhichImplementsIServiceProvider();
//Now we can use extension function
serviceProvider.QueryService<CustomServiceType>(out myService); 
//After this function myService should be initialized (probably...)
zviad
  • 586
  • 7
  • 18