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 7948081
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T01:37:08+00:00 2026-06-04T01:37:08+00:00

The following is C#, though the code pattern is probably relevant to any OO

  • 0

The following is C#, though the code pattern is probably relevant to any OO language.

I have two methods, MethodWithTry and MethodWithSomeReturnValue that I believe are functionally equivalent. I would like to know if one of them is the “correct” way. Is there something about one (ex. concurrency) that makes one of them a bad choice.

    public void MethodWithTry()
    {
        int returnValue;
        string someInput;

        if (TryGetValueThisWay(someInput, returnValue))
        {
            //do something this way
        }
        else
        {
            if (TryGetValueThatWay(someInput, returnValue))
            {
                //do something that way
            }
            else
            {
                //do something a default way
            }
        }
    }

    public void MethodWithSomeReturnValue()
    {
        int? returnValue;
        string someInput;

        returnValue = GetValueThisWay(someInput);
        if (returnValue != null)
        {
            //do something this way
        }
        else
        {
            returnValue = GetValueThatWay(someInput);
            if (returnValue != null)
            {
                //do something that way
            }
            else
            {
                //do something a default way
            }
        }
    }

The signatures for the methods being called are

    public int? GetValueThisWay(string input)
    public int? GetValueThatWay(string input)
    private bool TryGetValueThisWay(string input, out int value)
    private bool TryGetValueThatWay(string input, out int value)

EDIT — ADDITIONAL INFO

The methods in question being called are doing finds in collections. So different names might have been

public int? GetValueFromCollectionA()
public int? GetValueFromCollectionB()

IMHO, TrySomeMethodName – makes the code somewhat more readable.
However, using an OUT variable, especially when the return value is an integer, means that it is always mutable and assigned to at least twice (set to 0 as default).

  • 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-06-04T01:37:10+00:00Added an answer on June 4, 2026 at 1:37 am

    If you operate on value types (like int) and the result of your method can be null, you should go for the Try version. This is because value types do not mix too well with null. For example int? is way slower than int because of boxing introduced by ?. All .NET TryParse methods work with value types and they follow this pattern. I think it’s good to conform to this approach.

    When you start to operate on reference types it becomes more natural to use method result and return null when needed.

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

Sidebar

Related Questions

I have the following code that's looping through files in a folder and doing
Suppose you have this following code in HTML - <div id=pattern> <span class=a>Content</span> </div>
The following code seems not to work, even though the file appears to be
I have the following code through which i am able to retrieve phone numbers.
I have downloaded sample code from Apple Center.I also have gone through following question:
Never thought I'd have this problem :) The following snippet of code works in
I have the following design pattern: var myObjectWithEvents = new ObjectWithEvents(); using (var mre
Ok, this is really really weird. I have the following simple regex search pattern
I'm stuck with the following scenario. It has a code smell, that I'm not
Currently (in PHP) I have the following regex pattern: \[(.*) (.*)=(.*)\] This matches [doSomething

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.