Search code examples
c#oopvar

Passing a var as a method parameter in C#


I'm trying to pass a var parameter to a method:

class Program
{
    static void Main()
    {
        var client = new RestClient("http://example.com");
        var request = new RestRequest("resource/{id}", Method.POST);
        var response = client.Execute(request);
        PrintResponseStuff(response);
    }

    public static void PrintResponseStuff(var response)
    {
        Console.WriteLine(response.StatusCode);
        Console.WriteLine(response.StatusDescription);
        Console.WriteLine(response.IsSuccessful);
        Console.WriteLine(response.Content);
        Console.WriteLine(response.ContentType);
    }
}

The easiest way would be to pass a var; however, if there's a data type that can hold request that should work too. Is there anyway to do this or do I need to pass each item individually?


Solution

  • var is not a "type" but just compiler sugar. It is smart enough to know what type it is. In fact, you can just hover over it and see it.

    Change the PrintResponseStuff parameter to that type.