TheDeveloperBlog.com

Home | Contact Us

C-Sharp | Java | Python | Swift | GO | WPF | Ruby | Scala | F# | JavaScript | SQL | PHP | Angular | HTML

C# string.IsNullOrEmpty Method

This C# tutorial shows how to use the string.IsNullOrEmpty method. It provides benchmarks.

IsNullOrEmpty. The string.IsNullOrEmpty method tests strings.

It checks for string references that are null or have no data. Strings are reference types and can be equal to null like any other reference type. Strings can also be empty—of zero length.

Empty Strings

Example. Here we look at several examples of the IsNullOrEmpty static method on the string type in the C# language. The three parts in the code sample show the IsNullOrEmpty method matching a null string, an empty string, and then a normal string.

Null StringsString Literal

Next: In this program, the parts A and B in the example will match, but part C won't.

C# program that uses string.IsNullOrEmpty

using System;

class Program
{
    static void Main()
    {
	// A.
	// Test with IsNullOrEmpty.
	string s = null;
	if (string.IsNullOrEmpty(s) == true)
	{
	    // True.
	    Console.WriteLine("Null or empty");
	}
	else
	{
	    Console.WriteLine("Not null and not empty");
	}

	// B.
	// Test with IsNullOrEmpty again.
	string y = "";
	if (string.IsNullOrEmpty(y) == true)
	{
	    // True.
	    Console.WriteLine("Null or empty");
	}
	else
	{
	    Console.WriteLine("Not null and not empty");
	}

	// C.
	// Test with IsNullOrEmpty without writing 'true'.
	string z = "sam";
	if (string.IsNullOrEmpty(z))
	{
	    // False.
	    Console.WriteLine("Null or empty");
	}
	else
	{
	    Console.WriteLine("Not null and not empty");
	}
    }
}

Output

Null or empty
Null or empty
Not null and not empty

IsNullOrEmpty detects two conditions. In the C# language strings can be equal to the null literal, meaning they don't reference any data. They can also be empty, meaning they reference a character buffer that is zero characters long.

Null

This method detects both of these conditions at the same time. It is static because you may want to call it on a null string. Using an instance method would cause the NullReferenceException to be raised.

Static Method, Class and ConstructorNullReferenceException

And: The method returns bool—either true or false. This indicates whether the string is empty or null.

So: In other words, if it has something substantial in it, the result bool will be false.

BoolReturn Bool

Alternatives. Next, we compare alternative ways of doing the same thing that IsNullOrEmpty does. Please note how the string is tested against null before checking its length. This avoids a null reference exception.

Version 1: C#

// Test version 1.
if (s == null || s == "")
{
    Console.WriteLine("1");
}

Version 2: C#

// Test version 2.
if (string.IsNullOrEmpty(s))
{
    Console.WriteLine("2");
}

Version 3: C#

// Test version 3.
if (s == null || s.Length == 0)
{
    Console.WriteLine("3");
}

Results of the benchmark. I found that string.IsNullOrEmpty is about in the middle in performance. However, the code for it is much clearer, which is a huge positive. You can see the results of the benchmark here.

Version 1

733 ms
Checks string for null, and then checks against "".

Version 2

546 ms
Calls string.IsNullOrEmpty static method in framework.

Version 3

406 ms
Checks string for null, and then check Length.

Summary. We used the IsNullOrEmpty method on the C# string type. This method provides us with a good and relatively efficient method of checking whether a string is okay to save or use. For performance it may be better to use manual null checks.


Related Links

Adjectives Ado Ai Android Angular Antonyms Apache Articles Asp Autocad Automata Aws Azure Basic Binary Bitcoin Blockchain C Cassandra Change Coa Computer Control Cpp Create Creating C-Sharp Cyber Daa Data Dbms Deletion Devops Difference Discrete Es6 Ethical Examples Features Firebase Flutter Fs Git Go Hbase History Hive Hiveql How Html Idioms Insertion Installing Ios Java Joomla Js Kafka Kali Laravel Logical Machine Matlab Matrix Mongodb Mysql One Opencv Oracle Ordering Os Pandas Php Pig Pl Postgresql Powershell Prepositions Program Python React Ruby Scala Selecting Selenium Sentence Seo Sharepoint Software Spellings Spotting Spring Sql Sqlite Sqoop Svn Swift Synonyms Talend Testng Types Uml Unity Vbnet Verbal Webdriver What Wpf