Home | Contact Us

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

C# Default

This C# program demonstrates the default operator. Default returns the default value of a type.

Default. Every reference and value type has a default value.

This value is returned by the default(Type) expression. Default is most useful for writing generic classes. It also helps us understand the language's type system.

Example. We first look at four uses of the default expression. The four types we apply the default expression to are the StringBuilder, int, bool and Program types. The Program type is declared in the program itself.


Finally: The values that were assigned are printed to the screen. The null value is printed as a blank line.


C# program that uses default expression

using System;
using System.Text;

class Program
    static void Main()
	// Acquire the default values for these types and assign to a variable.
	StringBuilder variable1 = default(StringBuilder);
	int variable2 = default(int);
	bool variable3 = default(bool);
	Program variable4 = default(Program);

	// Write the values.
	Console.WriteLine(variable1); // Null
	Console.WriteLine(variable2); // 0
	Console.WriteLine(variable3); // False
	Console.WriteLine(variable4); // Null



Discussion. At the level of the intermediate language instructions, the default value expression is implemented using static analysis. This means the default expressions are evaluated at compile-time, resulting in no performance loss.

So: No reflection to the type system or metadata relational database is used at runtime.


And: Unlike with typeof expressions, caching the default expression would have no benefit.


Summary. We looked at the default value expression. The ECMA-344 specification describes this expression in chapter 14 and page 187. By understanding the usage of the default value expression, we can better understand the type system.

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