-1

I came across the following code

 public static IMyQueries<T> Foo<T>(this ASet<T> items)
 {
           ...
 }

My question is in the parameter of this function the keyword this is being used. Why is that ? From what I understand is that ASet is the typename which is templated. Why is the keyword this there. Can someone give a simple example of why this is employed as a parameter?

jps
  • 20,041
  • 15
  • 75
  • 79
MistyD
  • 16,373
  • 40
  • 138
  • 240
  • 3
    https://learn.microsoft.com/en-us/dotnet/csharp/programming-guide/classes-and-structs/extension-methods – madreflection Feb 17 '20 at 03:33
  • Arguments with `this` keyword use for creating Extension methods. You can call thsi method on an instance of `ASet` as it is part of the type. https://learn.microsoft.com/en-us/dotnet/csharp/programming-guide/classes-and-structs/extension-methods – Chetan Feb 17 '20 at 03:34

1 Answers1

4

It shows that the function is extension for ASet type.

An example for string extension could be:

    public static class Extensions
    {
        public static string SayHiExtension(this string to)
        {
            return $"Hello {to}";
        }

        public static string SayHi(string to)
        {
            return $"Hello {to}";
        }
    }

And usage difference:

  string name = "World";

   // Extension
  string output = name.SayHiExtension();

  // Without extension
  output = Extensions.SayHi(name);
SerhatDev
  • 241
  • 1
  • 4