Home > Blockchain >  Why does this function of C# have an argument of type this?
Why does this function of C# have an argument of type this?

Time:07-06

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

CodePudding user response:

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.

  • Related