How to use string.Empty or String.Empty to initialize a string in C#?


Set the string as empty using the string.Empty in C# −

string myStr = string.Empty;

To check whether it is a string or not, use the IsNullOrEmpty() method −

if (string.IsNullOrEmpty(myStr)) {
   Console.WriteLine("String is empty or null!");
}

The following is an example −

Example

 Live Demo

using System;

namespace Demo {
   public class Program {
      public static void Main(string[] args) {
         string myStr = string.Empty;

         if (string.IsNullOrEmpty(myStr)) {
            Console.WriteLine("String is empty or null!");
         } else {
            Console.WriteLine("String isn't empty or null!");
         }
      }
   }
}

Output

String is empty or null!

Updated on: 22-Jun-2020

526 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements