Empty String

In this chapter you will learn:

  1. Empty string vs null string
  2. String length for empty string

Empty string vs null string

string is a reference type therefore we can assign null to string variable.

using System;//from j a  v a2s .  c o  m

class Sample
{
    public static void Main()
    {

      string s = null;

    }
}

Empty string is a string with no characters.

The length of an empty string is 0.

Empty string is not null string.

using System;//from   j a v a 2 s.c o m

class Sample
{
    public static void Main()
    {

      string s = "";
      Console.WriteLine(s.Length);
    }
}

The output:

String length for empty string

From the code above we can see that the length of a empty string is 0. We cannot check the Length property on an empty string.

using System;//  ja  v a  2s .  co m

class Sample
{
    public static void Main()
    {

      string s = null;
      Console.WriteLine(s.Length);
    }
}

The output:

Next chapter...

What you will learn in the next chapter:

  1. Compare two DateTimeOffset values for equality
  2. How to use EqualsExact method to compare DateTimeOffset
  3. Compare method
Home » C# Tutorial » String
string
String creation
Char in string
Compare strings
String equality
String concatanation
String copy
String Join
String split
String Search for Index
String contains
String start with
String insert
String case
Replacing substring
Remove from a string
Substring
Escape Characters
String verbatim
String padding
Switch on String
String trim
String intern
String normalization
Empty String