📖 Scenario: Imagine you are creating a simple program to understand what default values different types have in C#. This helps when you declare variables but don't assign values yet.
🎯 Goal: You will create variables of different types and print their default values to see what C# assigns automatically.
📋 What You'll Learn
Create variables of type int, bool, and string without assigning values.
Create a variable called defaultInt and set it to the default value of int using default keyword.
Create a variable called defaultBool and set it to the default value of bool using default keyword.
Create a variable called defaultString and set it to the default value of string using default keyword.
Print the values of defaultInt, defaultBool, and defaultString.
💡 Why This Matters
🌍 Real World
Knowing default values helps when you declare variables and want to understand what initial values they hold before assignment.
💼 Career
Understanding default values is important for debugging and writing safe code that avoids unexpected behaviors.
Progress0 / 4 steps
1
Create variables without assigning values
Create variables called number of type int, flag of type bool, and text of type string without assigning any values.
C Sharp (C#)
Hint
Just declare the variables with their types and names, but do not assign any values.
2
Assign default values using default keyword
Create variables called defaultInt, defaultBool, and defaultString. Assign them the default values of int, bool, and string respectively using the default keyword.
C Sharp (C#)
Hint
Use the syntax type variable = default(type); to get the default value for each type.
3
Print the default values
Use Console.WriteLine to print the values of defaultInt, defaultBool, and defaultString each on its own line.
C Sharp (C#)
Hint
Use Console.WriteLine(variable); to print each variable's value.
4
Run the program to see the output
Run the program and observe the output. It should print the default values of int, bool, and string types in order.
C Sharp (C#)
Hint
The output shows 0 for int, False for bool, and an empty line for string because its default is null.
Practice
(1/5)
1. What is the default value of an int type variable in C#?
easy
A. 0
B. null
C. 1
D. "" (empty string)
Solution
Step 1: Understand default values for value types
In C#, value types like int have a default value of zero when not initialized.
Step 2: Recall default for int
The default for int is 0, not null or any other value.
Final Answer:
0 -> Option A
Quick Check:
Default int = 0 [OK]
Hint: Remember: numbers default to zero in C# [OK]
Common Mistakes:
Thinking default int is null
Confusing default int with 1
Assuming default is empty string
2. Which of the following is the correct syntax to get the default value of a type T in C#?
easy
A. T.default()
B. default = T
C. default(T)
D. T.default
Solution
Step 1: Recall C# syntax for default values
The correct syntax to get the default value of a type T is default(T).
Step 2: Check other options
Options like T.default(), default = T, and T.default are invalid syntax in C#.
Final Answer:
default(T) -> Option C
Quick Check:
Use default(T) to get default value [OK]
Hint: Use default(Type) syntax to get default value [OK]
Common Mistakes:
Using invalid syntax like T.default()
Confusing assignment with default keyword
Trying to access default as a property
3. What will be the output of this C# code?
bool flag = default; Console.WriteLine(flag);
medium
A. False
B. null
C. True
D. Compilation error
Solution
Step 1: Understand default value of bool
The default value of bool in C# is false.
Step 2: Analyze the code output
The variable flag is assigned default, so it holds false. Printing it outputs "False".
Final Answer:
False -> Option A
Quick Check:
Default bool = false [OK]
Hint: Default bool is always false, not true [OK]
Common Mistakes:
Assuming default bool is true
Expecting null for bool
Thinking code causes error
4. Identify the error in this C# code snippet:
string text = default; int number = default; Console.WriteLine(text.Length);
medium
A. Compile-time error on text.Length
B. No error, code runs fine
C. Syntax error on default keyword
D. NullReferenceException at runtime
Solution
Step 1: Understand default for reference types
For reference types like string, default is null.
Step 2: Analyze accessing property on null
Accessing text.Length when text is null causes a NullReferenceException at runtime.