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

  • Home
  • SEARCH
  • 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 7677097
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T17:20:47+00:00 2026-05-31T17:20:47+00:00

In his answer to this question , BlackBear suggested replacing string y = Session[key]

  • 0

In his answer to this question, BlackBear suggested replacing

 string y = Session["key"] == null ? "none" : Session["key"].ToString();

with

 string y = (Session["key"] ?? "none").ToString();

This works great if Session["key"] is a string value, but what if I want to do this with an object? For example, I could do this:

 string y = GetMyObject() == null ? "none" : GetMyObject().ToString();

But what if I don’t want to evaluate GetMyObject() twice? Is my only choice to store the results in a variable, then check that variable for null?

 var x = GetMyObject();
 string y = (x == null) ? "none" : x.ToString();

Edit: This is all theoretical – I’ve run into it before, but I don’t have a specific example in front of me right now. That being said, here’s an example class.

class MyObject
{
   private MyObject() { }
   public MyObject GetMyObject() { return new MyObject(); }
   public override string ToString() { return "A String"; }
}

If I do string y = (GetMyObject() ?? "none").ToString();, I get

Operator ‘??’ cannot be applied to operands of type ‘MyObject’ and ‘string'”.

Ideally, I’d be able to do

 string y = GetMyObject().ToString() ?? "none";

and have it work even if GetMyObject() is null. Basically null-coalescing operator acting as a self-contained try {} catch (NullReferenceException) {}. I know I can’t, but that would be the ideal.

  • 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-31T17:20:48+00:00Added an answer on May 31, 2026 at 5:20 pm

    BlackBear’s solution works perfectly fine as it is:

    string y = (GetMyObject() ?? "none").ToString();
    

    That will either set y to GetMyObject().ToString() (if GetMyObject() returns a non-null reference) or to "none".ToString() otherwise. There’s a tiny, tiny hit calling ToString() on a string, but I’d very much doubt that it’s significant.

    That’s fine if GetMyObject() returns object. If it returns some specific type which string can’t be converted to, just cast one of them to object, e.g.

    string y = (GetMyButton() ?? (object) "none").ToString();
    

    Having said that, I’d probably use an extension method at that point – or just do it longhand.

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

Sidebar

Related Questions

Background This question is related to Why does String.valueOf(null) throw a NullPointerException? Consider the
Example #1 bschaeffer's answer to this question - in his last example: $this->load->model('table'); $data
I hope this question has an obvious answer to anyone who knows his way
Thanks to Insin for answering a previous question related to this one. His answer
I am new to Css. I don't know whether this question had answer or
30 jun 2011 -- I am awarding @Pablo for this question, because of his
This question asks how to select a user's rank by his id. id name
I asked this question and got an excellent answer (thanks!). Part of the problem
I was intrigued by this answer to my question about getting vim to highlight
Im new to JSF 2. My question is related to BalusC's answer to this

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.