John
John

Reputation: 6553

What is the correct way to return an Interface Type from a Method?

Ok say I have the following simple classes:

public interface IResult<T>
{
    T Results { get; set;}
    string Message { get; set;}
}

public class Result<T> : IResult<T>
{
    T Results { get; set;}
    string Message { get; set;}
}

public class Result2<T> : IResult<T>
{
    T Results { get; set;}
    string Message { get; set;}
}

Now when I want to call some method to return a IResult, which way is correct?

public class TestClass
{
    public bool TryDoSomething(out IResult<bool> result)
    {
        // Do stuff
        result.Message = "Out!";
        return true; // for example
    }

    public IResult<bool> DoSomething(IResult<bool> result)
    {
        // Do Stuff
        result.Message = "Sent in!";
        result.Result = true;
        return result;
    }

    public IResult<bool> DoSomething()
    {
        // Do Stuff
        IResult<bool> result = new Result<bool>();
        result.Message = "I'm a Result!";
        result.Result = true;
        return result;
    }
}

Now say I actually want a Result2, is it better to create it and send it in as the parameter (methods #1 or #2) or create a new Result but recast it as IResult and then to Result2 as in method #3?

public class GetResults
{
    public void GetResults()
    {
        Result2<bool> results = new Result2<bool>();
        if (TryDoSomething(out results))
        {
            Debug.WriteLine(results.Message);
        }
        results = DoSomething(results);
        if (results.Result) Debug.WriteLine(results.Message);
        results = DoSomething();
        if (results.Result) Debug.WriteLine(results.Message);
    }
}

So is it better to create my interface implementation in the Method or send it in as a parameter?

Upvotes: 0

Views: 151

Answers (2)

phoog
phoog

Reputation: 43046

Following on from your comment to Jon's answer, the caller can specify what type is desired without creating the object, assuming the class has a public parameterless constructor:

public TResult DoSomething<TResult, U>() where TResult : IResult<U>, new()
{
    // Do Stuff     
    TResult result = new TResult();     
    result.Message = "I'm a Result!";     
    result.Result = true;     
    return result;     
}

usage:

Result<bool> result1 = DoSomething<Result<bool>, bool>();
Result2<bool> result2 = DoSomething<Result2<bool>, bool>();

Upvotes: 1

Jon Skeet
Jon Skeet

Reputation: 1500065

You should only use out parameters as a last resort - unless you really want to effectively return two values from the same method, and you've got a good reason not to encapsulate them together (or use Tuple), I definitely wouldn't suggest the first approach. (What's the bool meant to be for?)

If the aim is to manipulate an existing object, then the second method is appropriate - but if the aim is to create a result (which is more common, IMO) then the third approach is the most appropriate one.

It's hard to give concrete advice when we've got so little to go on, but personally I favour immutable types where practical, which rules out the second method anyway.

In terms of your question about "I actually want a Result2" - what wants a Result2? The calling code, or the method returning the result? Usually if you're creating a result then it's more appropriate for the "creating" code to know which implementation it wants to use.

Upvotes: 3

Related Questions