Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7437417
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T10:23:49+00:00 2026-05-29T10:23:49+00:00

Ok say I have the following simple classes: public interface IResult<T> { T Results

  • 0

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?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-29T10:23:50+00:00Added an answer on May 29, 2026 at 10:23 am

    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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have the following simple class class base { public: int x; base &set(int
Say I have the following classes: public class BaseConfig { } public class SpecialConfig
Say I have the following simple java bean: class MyBean { private Date startDate;
Say, I have the following 2 classes: class A def a_method end end class
Using Mongoid, let's say I have the following classes: class Map include Mongoid::Document embeds_many
Start with these simple classes... Let's say I have a simple set of classes
I have the following classes: [DataContract] [KnownType(typeof(SpecifiedItemCollection))] public class ItemCollection<T> : IEnumerable where T
Lets say that you have a following simple application: <form action=helloServlet method=post> Give name:<input
Let's say I have the following, very simple block of code: <script> function hasVal(field)
Lets say we have following models. class User(db.Model): username=db.StringProperty() avatar=db.ReferenceProperty() class User(db.Model): username=db.StringProperty() avatar=db.StringProperty()

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.