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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T04:44:04+00:00 2026-06-08T04:44:04+00:00

I had the following: string Name = name.First + + name.Last; This returns Tom

  • 0

I had the following:

string Name = name.First + " "  +  name.Last;

This returns Tom Jones just fine.

In case name.First may be null or name.Last may be null, I have the following:

string SpeakerName = name.First ?? string.Empty + " "  +  name.Last ?? string.Empty;

What is strange is that it only returns Tom. Why is this and how can I fix it such that if null it defaults to empty string for either first or last name?

  • 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-08T04:44:06+00:00Added an answer on June 8, 2026 at 4:44 am

    Because of the relative precedence of the ?? and + operators. Try this:

    string SpeakerName = (name.First ?? "") + " " + (name.Last ?? "");
    

    Your original example is evaluating as if it was:

    string SpeakerName = name.First ?? ("" + " "  +  (name.Last ?? ""));
    

    Also, read Jon’s answer here: What is the operator precedence of C# null-coalescing (??) operator?

    As he suggests there, this should work as well:

    string SpeakerName = name.First + " " + name.Last;
    

    Because that compiles to @L.B.’s answer below, minus the trim:

    string SpeakerName = String.Format("{0} {1}", name.First, name.Last)
    

    EDIT:

    You also asked that first and last both == null makes the result an empty string. Generally, this is solved by calling .Trim() on the result, but that isn’t exactly equivalent. For instance, you may for some reason want leading or trailing spaces if the names are not null, e.g. ” Fred” + “Astair ” => ” Fred Astair “. We all assumed that you would want to trim these out. If you don’t, then I’d suggest using a conditional:

    string SpeakerName = name.First + " " + name.Last;
    SpeakerName = SpeakerName == " " ? String.Empty : SpeakerName;
    

    If you never want the leading or trailing spaces, just add a .Trim() as @L.B. did

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

Sidebar

Related Questions

Suppose I had the following class: template<typename T> class Value { public: Value(std::string name,
If I had the following table. create_table :my_table, :id => false do |t| t.string
I had the following HTML page rendering to use the Google Maps API. This
This question made me question a practice I had been following for years. For
Assuming the following view model definition: public class MyObject { public string Name {
Given the following string, I'd like to parse into a list of first names
In the first version of my database I had three rows. The following methods
Hi i am new to Spring and I have a following problem: I had
I had following cgridview in Yii application, I want to change date format in
I had the following query that doesn't work and I was wondering how to

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.