Answers for "string empty or null c#"

C#
2

string empty or null c#

// use
if(string.IsNullOrEmpty(myStringVariable)) {
	// myStringVariable is null or empty
} else {
	// myStringVariable is not null and is not empty
}
  
// examples
string s1 = "abcd";
string s2 = "";
string s3 = null;

Console.WriteLine("String s1 {0}.", Test(s1));
Console.WriteLine("String s2 {0}.", Test(s2));
Console.WriteLine("String s3 {0}.", Test(s3));

string Test(string s)
{
	if (string.IsNullOrEmpty(s))
    	return "is null or empty";
	else
    	return string.Format("(\"{0}\") is neither null nor empty", s);
}

// The example displays the following output:
//       String s1 ("abcd") is neither null nor empty.
//       String s2 is null or empty.
//       String s3 is null or empty.
Posted by: Guest on July-01-2021

C# Answers by Framework

Browse Popular Code Answers by Language