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

CsharpProgrammingServer Side Programming

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!
raja
Updated on 22-Jun-2020 12:48:03

Advertisements