C# Keywords Tutorial Part 20: default

C# Keywords Tutorial Part 20: default

The “default” expression in C# is used to set a variable’s default value at initialization time. When working with generic classes or resetting a variable to its initial value after use, it is especially helpful. The “default” term will be discussed in detail in this article, along with its usage in some sample code.

What is the “default” keyword in C#?

The “default” expression in C# is used to set a variable’s default value at initialization time. The data type of a variable determines its preset value. For instance, a boolean’s default value is untrue and an integer’s default value is 0.

The syntax for using the “default” keyword is as follows:

dataType variableName = default;

For example, to initialize an integer variable to its default value, we could use the following code:

int variableName = default;

How does the “default” keyword work?

When you use the “default” keyword, C# initializes the variable with its default value. The default value depends on the data type of the variable. For value types, such as integers and booleans, the default value is 0 or false. For reference types, such as objects and strings, the default value is null.

Code examples:

Let’s take a look at some code examples to illustrate the use of the “default” keyword in C#.

// Declare an integer variable and initialize it with its default value
int count = default;

// Output the value of the variable to the console
Console.WriteLine("Count: {0}", count);

In this example, we declare an integer variable called “count” and initialize it with its default value using the “default” keyword. We then output the value of the variable to the console, which is 0 since the default value of an integer is 0.

// Declare a boolean variable and initialize it with its default value
bool isReady = default;

// Output the value of the variable to the console
Console.WriteLine("Is Ready: {0}", isReady);

In this example, we declare a boolean variable called “isReady” and initialize it with its default value using the “default” keyword. We then output the value of the variable to the console, which is false since the default value of a boolean is false.

// Declare a generic list and initialize it with its default value
List<int> numbers = default;

// Output the value of the variable to the console
Console.WriteLine("Numbers: {0}", numbers == null ? "null" : "not null");

In this example, we declare a generic list of integers called “numbers” and initialize it with its default value using the “default” keyword. We then output the value of the variable to the console, which is null since the default value of a reference type is null.

// Reset a variable to its default value
int count = 10;

// Output the value of the variable to the console
Console.WriteLine("Count: {0}", count);

// Reset the variable to its default value
count = default;

// Output the value of the variable to the console
Console.WriteLine("Count: {0}", count);

In this example, we first declare an integer variable called “count” and assign it a value of 10. We then output the value of the variable to the console. We then use the “default” keyword to reset the variable to its default value of 0, and output the value of the variable to the console again.

Share this post

Leave a Reply

Your email address will not be published. Required fields are marked *