0
public static T GetResult<T>(this RpcResponseMessage response, bool returnDefaultIfNull = true, JsonSerializerSettings settings = null)
{
    if (response.Result == null)
    {
        if (!returnDefaultIfNull && default(T) != null)
        {
            throw new Exception("Unable to convert the result (null) to type " + typeof(T));
        }
        return default(T);
    }
    try

I am playing around with Nethereum code. I noticed something very weird. The second parameter of GetResult has type bool. The third one has type JsonSerializerSettings and so on.

But the first argument:

The type is "this"

What the hell?

I thought this in C# is like me in VB.net: it means a pointer to itself. A reference to itself.

So why is the type this?

Perhaps the real type is RpcResponseMessage. But what does the word this state there?

I use Telerik to convert the code to vb.net and I get

Public Shared Function GetResult(Of T)(ByVal response As RpcResponseMessage, ByVal Optional returnDefaultIfNull As Boolean = True, ByVal Optional settings As JsonSerializerSettings = Nothing) As T
    If response.Result Is Nothing Then
        If Not returnDefaultIfNull AndAlso Nothing IsNot Nothing Then
            Throw New Exception("Unable to convert the result (null) to type " & GetType(T))
        End If
        Return Nothing
    End If

Which is weird again. The word this is gone. It's not replaced by me. Also I wonder why we need to add byVal in vb.net? Class is always passed by reference and primitive type is passed by value.

Also, the code is called like this

return response.GetResult<T>();

Whoa!. A static class function is called by an object. I've heard a long time ago that the way object function works is it passes itself as argument to a function. I have never seen it's treated directly like this.

What am I missing?

Where can I learn more about it? What sort of weird syntax is this?

Update: I was aware of what extension method is. I forget. In VB.net it doesn't use me or this. It's done differently in vb.net I am using this convertor to convert vb.net to C#.

https://converter.telerik.com/

I didn't remember if there was <extension()> before or not. This is the new translation

<Extension()>
Public Shared Function GetResult(Of T)(ByVal response As RpcResponseMessage, ByVal Optional returnDefaultIfNull As Boolean = True, ByVal Optional settings As JsonSerializerSettings = Nothing) As T
    If response.Result Is Nothing Then

        If Not returnDefaultIfNull AndAlso Nothing IsNot Nothing Then
            Throw New Exception("Unable to convert the result (null) to type " & GetType(T))
        End If

        Return Nothing
    End If
End Function
user4951
  • 32,206
  • 53
  • 172
  • 282
  • 10
    [Extension method](https://learn.microsoft.com/en-us/dotnet/csharp/programming-guide/classes-and-structs/extension-methods). – Sweeper Jul 04 '22 at 11:08
  • 4
    These are called [extension methods](https://learn.microsoft.com/en-us/dotnet/csharp/programming-guide/classes-and-structs/extension-methods) and are widely used in C#, as an example you can look at LINQ, specifically the lambda/ method syntax of Linq is implemented using extension methods – MindSwipe Jul 04 '22 at 11:09
  • It is indeed, a reference to itself or -better- to the object upon which the method is called. – Davide Vitali Jul 04 '22 at 11:19
  • Extension methods are a way to extend a type by "adding" methods to it without modifying the type itself. All LINQ methods (Select, Where, etc) are extension methods on `IEnumerable` or `IQueryable`. That's not a new feature either, LINQ was introduced in 2007 – Panagiotis Kanavos Jul 04 '22 at 11:31
  • 1
    Does this answer your question? [How extension methods are implemented internally](https://stackoverflow.com/questions/6543827/how-extension-methods-are-implemented-internally) Also https://stackoverflow.com/questions/71976566/what-does-the-extension-method-do-in-c-sharp-and-why-do-you-need-it and https://stackoverflow.com/questions/8804290/how-do-extension-methods-work-under-the-hood and https://stackoverflow.com/questions/17756646/vb-net-extension-functions-within-a-class – Charlieface Jul 04 '22 at 11:32
  • The converted VB code should have had an `` attribute applied to it. Did it do so? If so, why did you choose to ignore that? Also, you're confusing reference/value types and the similar *but not the same* concept of pass by value and pass by reference for parameters. All parameters are passed by value by default but the *value* being passed, in the case of a reference type, is the *reference* itself. – Damien_The_Unbeliever Jul 04 '22 at 12:36
  • Argh..... The translation does have however I ignored it. Basically I only copy part of the code. What I thought to be the important ones. – user4951 Jul 05 '22 at 08:09

1 Answers1

1

The type of argument is RpcResponseMessage. "This" is added before the type tells the compiler you want to use it as an extension method.

So you can call it like that (without or without "this"):

var response = new RpcResponseMessage();
GetResult<string>(response);

And like that (only with "this"):

var response = new RpcResponseMessage();
response.GetResult<string>();

That is why it's called the "extension method" - it extends the class's functionality without modifying the class.

JTO
  • 156
  • 2
  • 1
    And for completeness, note that the VB version uses the attribute instead of the `this` keyword. The first argument is still the object upon which the extension method operates. – Craig Jul 05 '22 at 14:37