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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T13:13:36+00:00 2026-05-26T13:13:36+00:00

I was surprised to see an example of a string being initialised to null

  • 0

I was surprised to see an example of a string being initialised to null and then having something appended to it in a production environment. It just smelt wrong.

I was sure it would have thrown a null object exception but this greatly reduced example also works:

string sample = null;
sample += "test";
// sample equals "test"

*Note the original code I found sets a string property to null and appends to it elsewhere so answers involving the compiler optimizing out the null at compile-time are irrelevant.

Can someone explain why this works without error?

Follow-up:

Based on Leppie’s answer I used Reflector to see what is inside string.Concat. It is now really obvious why that conversion takes place (no magic at all):

public static string Concat(string str0, string str1)
{
    if (IsNullOrEmpty(str0))
    {
        if (IsNullOrEmpty(str1))
        {
            return Empty;
        }
        return str1;
    }
    if (IsNullOrEmpty(str1))
    {
        return str0;
    }
    int length = str0.Length;
    string dest = FastAllocateString(length + str1.Length);
    FillStringChecked(dest, 0, str0);
    FillStringChecked(dest, length, str1);
    return dest;
}

**Note: the specific implementation I was investigating (in the .Net library by Microsoft) does not convert to empty strings as is suggested by the C# standards and most of the answers, but uses a few tests to shortcut the process. The end result is the same as if it did but there you go 🙂

  • 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-26T13:13:37+00:00Added an answer on May 26, 2026 at 1:13 pm

    the + operator for strings are just shorthand for string.Concat which simply turns null arguments into empty strings before the concatenation.

    Update:

    The generalized version of string.Concat:

    public static string Concat(params string[] values)
    {
        int num = 0;
        if (values == null)
        {
            throw new ArgumentNullException("values");
        }
        string[] array = new string[values.Length];
        for (int i = 0; i < values.Length; i++)
        {
            string text = values[i];
            array[i] = ((text == null) ? string.Empty : text);
            num += array[i].Length;
            if (num < 0)
            {
                throw new OutOfMemoryException();
            }
        }
        return string.ConcatArray(array, num);
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am pretty new to silverlight and was very surprised to see that only
I was just pleasantly surprised to came across the documentation of Python's compiler package
I was surprised to see in the Java source that System.arraycopy is a native
I've look around and see a few plugins that do this, but I'm surprised
Since String implements IEnumerable<char> , I was expecting to see the Enumerable extension methods
I was surprised to see that R will coerce factors into a number when
Is it possible to see if a string is the same as the name
After upgrading from Firefox 3.6 to FF4 i was surprised to see that jQuery
I'm new to oop and was surprised to see that code that worked properly
Surprised that i havent been able to find this myself, but anyway. Let's say

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.