एक स्ट्रिंग को एक खाली सूची में इनिशियलाइज़ करने के लिए -
string myStr = null;
अब, सूची खाली है या नहीं यह जांचने के लिए अंतर्निहित विधि IsNullOrEmpty() का उपयोग करें -
if (string.IsNullOrEmpty(myStr)) {
Console.WriteLine("String is empty or null!");
} आइए देखें पूरा कोड -
उदाहरण
using System;
namespace Demo {
class Program {
static void Main(string[] args) {
string myStr = null;
if (string.IsNullOrEmpty(myStr)) {
Console.WriteLine("String is empty or null!");
}
Console.ReadKey();
}
}
} आउटपुट
String is empty or null!
एक स्ट्रिंग को एक खाली स्ट्रिंग में प्रारंभ करने का दूसरा तरीका, निम्न कोड आज़माएं। यहाँ, हमने string.Empty -
. का उपयोग किया हैउदाहरण
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!");
}
}
}
} आउटपुट
String is empty or null!