Web検索はbingがおすすめ!

空文字列 "" と string.Empty と null との違い - C#

空文字列 "" と string.Empty と null との違いを紹介します。

概要

C#のstring型で、空文字列 "" と string.Empty と null との違いを紹介します。
C#のstring型で空の文字を表現する方法として、下記の3つがあります。
  • 空文字 ""
  • string.Empty
  • null
この記事では3つの記述方式に違いがあるのかを確認します。

結論

最初に結論をまとめると以下の表となります。
空文字列("")string.Emptynull
画面表示
""""null
引数として渡したときの ArgumentNullException発生しない発生しない発生する

プログラム例1

Windows Formアプリケーションを新規作成し、下記のプログラムを作成します。

UI

下図のUIを作成します。複数行のテキストボックスを1つ、ボタンを2つ配置します。

コード

下記のコードを記述します。
button1、button2 のClickイベントの実装が中心です。
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace StringNullEmptyBlank
{
  public partial class FormMain : Form
  {
    public FormMain()
    {
      InitializeComponent();
    }

    private void button1_Click(object sender, EventArgs e)
    {
      string NullString = null;
      string BlankString = "";
      string EmptyString = string.Empty;

      textBox1.Text += string.Format("Null String:{0}\r\n", NullString);
      textBox1.Text += string.Format("Blank String:{0}\r\n", BlankString);
      textBox1.Text += string.Format("Empty String:{0}\r\n", EmptyString);

    }

    private void button2_Click(object sender, EventArgs e)
    {
      string NullString = null;
      string BlankString = "";
      string EmptyString = string.Empty;

      if (NullString == BlankString) {
        textBox1.Text += "Null文字列 と 空文字列は 一致しています。\r\n";
      }
      else {
        textBox1.Text += "Null文字列 と 空文字列は 違う値です。\r\n";
      }

      if (NullString == EmptyString) {
        textBox1.Text += "Null文字列 と string.Emptyは 一致しています。\r\n";
      }
      else {
        textBox1.Text += "Null文字列 と string.Emptyは 違う値です。\r\n";
      }

      if (BlankString == EmptyString) {
        textBox1.Text += "空文字列 と string.Emptyは 一致しています。\r\n";
      }
      else {
        textBox1.Text += "空文字列 と string.Emptyは 違う値です。\r\n";
      }
    }
  }
}

解説

[button1]では、nullの文字列、空の文字列("")、string.Emptyの値の3つの文字列をテキストボックスに表示し、どのように画面に表示されるかを確認します。[button2]では、null文字列、空の文字列("")、string.Empty の値を比較し、値が一致しているかを確認します。

実行結果

上記のプロジェクトを実行します。下図のウィンドウが表示されます。


[button1]をクリックします。3つの文字列の値が画面に表示されます。いずれの値も文字列としては何も表示されません。


続いて[button2]をクリックします。null文字列と空文字列の値、null文字列とstring.Emptyの値が違うことがわかります。一方、string.Empty と 空文字列("")は同じ値であることがわかります。

プログラム例2

Windows Formアプリケーションを新規作成し、下記のプログラムを作成します。

UI

下図のUIを作成します。複数行のテキストボックスを1つ、ボタンを3つ配置します。

コード

下記のコードを記述します。ボタンのClickイベントを実装します。
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace StringNullEmptyBlank
{
  public partial class FormNullException : Form
  {
    public FormNullException()
    {
      InitializeComponent();
    }

    private void button1_Click(object sender, EventArgs e)
    {
      Dictionary<string, string> dic = new Dictionary<string, string>();
      dic.Add("penguin", "ぺんぎん");
      dic.Add("bear", "くま");
      dic.Add("duck", "あひる");

      string NullString = null;
      string BlankString = "";
      string EmptyString = string.Empty;

      if (dic.ContainsKey(NullString) == true) {
        textBox1.Text = dic[NullString];
      }
    }

    private void button2_Click(object sender, EventArgs e)
    {
      Dictionary<string, string> dic = new Dictionary<string, string>();
      dic.Add("penguin", "ぺんぎん");
      dic.Add("bear", "くま");
      dic.Add("duck", "あひる");

      string NullString = null;
      string BlankString = "";
      string EmptyString = string.Empty;

      if (dic.ContainsKey(BlankString) == true) {
        textBox1.Text = dic[BlankString];
      }
    }

    private void button3_Click(object sender, EventArgs e)
    {
      Dictionary<string, string> dic = new Dictionary<string, string>();
      dic.Add("penguin", "ぺんぎん");
      dic.Add("bear", "くま");
      dic.Add("duck", "あひる");

      string NullString = null;
      string BlankString = "";
      string EmptyString = string.Empty;

      if (dic.ContainsKey(EmptyString) == true) {
        textBox1.Text = dic[EmptyString];
      }
    }
  }
}

解説

Dictionaryオブジェクトを作成し、ContainsKeyメソッドの引数に null文字列、空の文字列、string.Empty の文字列を与えた場合の動作の違いを確認します。

実行結果

プロジェクトを実行します。下図のウィンドウが表示されます。


[button1] をクリックします。例外(System.ArgumentNullException)が発生しアプリケーションの実行が止まります。引数にnullを与えたため、ArgumentNullExceptionが発生してしまいます。


[button2] をクリックします。例外が発生せずテキストボックスには何も表示されません。こちらは空の文字列でnullではないため例外は発生しないようです。


[button3] をクリックします。例外が発生せずテキストボックスには何も表示されません。こちらも空の文字列でnullではないため例外は発生しないようです。


著者
iPentecのメインプログラマー
C#, ASP.NET の開発がメイン、少し前まではDelphiを愛用
掲載日: 2018-10-09
iPentec all rights reserverd.