Introduction
Default values give a starting value to variables when you don't set one yourself. This helps avoid errors from using empty or unknown data.
default(Type)
int number = default(int);
bool isReady = default(bool);
string text = default(string);
double price = default;using System; class Program { static void Main() { int defaultInt = default(int); bool defaultBool = default(bool); string defaultString = default(string); double defaultDouble = default(double); Console.WriteLine($"Default int: {defaultInt}"); Console.WriteLine($"Default bool: {defaultBool}"); Console.WriteLine($"Default string: {defaultString ?? "null"}"); Console.WriteLine($"Default double: {defaultDouble}"); } }
int type variable in C#?int have a default value of zero when not initialized.intint is 0, not null or any other value.T in C#?T is default(T).T.default(), default = T, and T.default are invalid syntax in C#.bool flag = default;
Console.WriteLine(flag);
bool in C# is false.flag is assigned default, so it holds false. Printing it outputs "False".string text = default;
int number = default;
Console.WriteLine(text.Length);
string, default is null.text.Length when text is null causes a NullReferenceException at runtime.struct Point { public int X; public int Y; }
Point p = default;
Console.WriteLine(p.X + "," + p.Y);default.X and Y are int, so default is 0 for each.p.X + "," + p.Y outputs "0,0".Pass the test and this pattern moves unknown