Test your C#: Generic overloaded constructors

I love languages.  Here’s one for a language nut.

public class Response {

    private T result;
    private string error;

    public Response(T result) { this.result = result; }
    public Response(string message) { this.error = message; }

}

You can use this generic class as a wrapper for returning data.

return new Response(1000);

Or to return an abnormal result

return new Response(“Something has gone wrong”);

Question 1

The fun part then, is what happens when you have this?

var result = new Response(“Is this a result or an error?”);

What is result

Question 2

What about this:

public class Sample
{
    public static Response GetSample(T arg)
    {
        return new Response(arg);
    }
}

and then:

var result = Sample.GetSample(“Is this an error?”);

What is result

Discussions