TheDeveloperBlog.com

Home | Contact Us

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

C# Null String Example

This C# article provides some facts about null strings. It shows how to avoid errors.

Null strings present difficulties.

We often deal with null or uninitialized string references. Strings are initialized to null automatically at the class level, but we must assign them in methods.

Tip: One popular method for dealing with null strings is the string.IsNullOrEmpty method.

IsNullOrEmpty

Example. First, all class-level reference variables are initialized to null. Strings are reference types—they do not contain the character values in the variable. Instead the variable points to an object on the heap that contains that data.

NullObject

C# program that uses null string

using System;

class Program
{
    static string _test1;

    static void Main()
    {
	// Instance strings are initialized to null.
	if (_test1 == null)
	{
	    Console.WriteLine("String is null");
	}

	string test2;

	// // Use of unassigned local variable 'test2'
	// if (test2 == null)
	// {
	// }
    }
}

Output

String is null

This example shows that when a class-level variable (static or not) is used in any method, it is null if it has not been initialized yet. The compiler allows the code to be executed. This is a pattern that many programs use.

Next, the commented-out part shows that a compile-time error will occur if you not assign the string to the null literal when used in the method. This process is called definite assignment analysis and it prevents many errors.

Definite Assignment Analysis

Note: The example shows that you can use unassigned member variable strings, but not unassigned local variable strings.

Length. One common problem with null strings is trying to take their lengths with the Length property. This will cause a NullReferenceException. The article on the string Length property on this site addresses this issue.

String Length

Exception thrown

System.NullReferenceException:
Object reference not set to an instance of an object.

Summary. We saw how null strings are initialized at the class level, and how to definitely assign them in the local variable declaration space. We saw some common errors and reviewed the fact that strings are a reference type, not a value type.


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