TheDeveloperBlog.com

Home | Contact Us

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

C# Array Property, Return Empty Array

This C# example program uses an array property. It uses an empty string array.

Array properties often cause errors.

We develop a reliable array property that avoids errors in foreach-loops. MSDN suggests a specific design pattern that does not use the null value, even when there is no data.

Example. Here we see an interesting way to simplify your code that returns string arrays. If you have a method or property that returns null and you use that result in a foreach-loop, your program will throw a NullReferenceException.

However: You can instead return an empty string[0] array. This array could be cached in some programs.

C# program that demonstrates array property

class Program
{
    static void Main()
    {
	foreach (string item in Empty)
	{
	    System.Console.WriteLine(item); // Never reached
	}
    }

    /// <summary>
    /// Get an empty array.
    /// </summary>
    public static string[] Empty
    {
	get
	{
	    return new string[0]; // Won't crash in foreach
	}
    }
}

Output
    No output.

The code shows that when you loop over an empty string array, you will never execute the loop contents. If you instead had Empty return null, you would hit the NullReferenceException. This could make your code more complex.

Null Tips

The Empty property above might do some kind of processing before it decides to return the empty array of zero length. You might have code that checks a backing field each time, and if the field is null, returns the empty array.

Null Array Use

Discussion. Let's look at MSDN and see the usage guidelines for the .NET Framework. In the document Array Usage Guidelines, it is suggested that null array references not be returned, even when the backing store is null.

String and Array properties should never return a null reference. Null can be difficult to understand in this context.

Array Usage Guidelines: MSDN

Summary. We looked at how you can return an empty array from a public property in a C# program. When users of this property try to use the foreach-loop on the empty array, no exception will be thrown. The code in the foreach-loop will not execute.

And: This results in more reliable and simpler software, with the only downside being a null check in the property.


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