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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T18:11:10+00:00 2026-05-30T18:11:10+00:00

Everywhere I read constantly rehashes the idea of never return nulls but what should

  • 0

Everywhere I read constantly rehashes the idea of “never return nulls” but what should I return if not null in the cases where a value cannot be found?

Take the following method

List<Customer> GetCustomerListByRoleID(Guid RoleID) {}

In this case (and in most plural methods) its easy to simply return an empty List<Customer> if we can’t find our value and we’re good.

However in the case of a method like

Customer GetCustomerByID(Guid CustomerID) {}

You do not have the luxury of returning an empty array. Infact all you can do is return a New Customer(); but then you have a potentially uninitialized object (which you still need to check for) or null.

So what would be the recommended alternative to returning a null in the singular method?

  • 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-30T18:11:13+00:00Added an answer on May 30, 2026 at 6:11 pm

    For the single value case consider the TryGet pattern

    bool TryGetCustomerByID(Guid CustomerID, out Customer customer) { }
    

    This clearly expresses the intent that this method can and will fail and makes it much more awkward to ignore the aspect of failure. Dealing with it correctly though produces very readable code.

    Customer c;
    if (container.TryGetCustomer(id, out c)) {
      ...
    } else {
      // Deal with failure
    }
    

    Often I like to pair my TryGet APIs with a version that throws for those occasions when the caller knows it must be present else it’s a violation of some implicit contract.

    Customer GetCustomerByID(Guid id) {
      Customer c;
      if (!TryGetCustomerByID(id, out c)) {
        throw new Exception(...);
      }
      return c;
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I read everywhere (and see in practice) that usernames should not be changeable. When
I've read everywhere that PHP does not support multithreading but there are workarounds. But
It seems everywhere I read that either a library boasts if not needing RTTI
I'm using PHP PDO for my queries, everywhere, but I read that in very
I've been looking everywhere but I'm either blind or it's really not there. I'm
Okay I've read the other StackOverflow postings, forum posts on MSDN, everywhere. I CANNOT
I've be told and read it everywhere (but no one dared to explain why)
I have read everywhere that the Add method fails if it already exists but
I have read everywhere that unsigned Java applets are not allowed to make network
Everywhere you can read how to replace environment variables. But I want to insert

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.