C
C#2y ago
swilli

✅ Default operator

Can somebody give me ELI5 explanation for the necessity of the default operator? I'm under the impression everything that's declared but not initialized is automatically given a default value, typically 0 or null depending on the type. When would something not get an implicit default value? (Assuming that's why the default operator exists)
11 Replies
Aaron
Aaron2y ago
locals are never given a value implicitly
MODiX
MODiX2y ago
Windows10CE#8553
REPL Result: Failure
void Main() {
int i;
Console.WriteLine(i);
}
Main();
void Main() {
int i;
Console.WriteLine(i);
}
Main();
Exception: CompilationErrorException
- Use of unassigned local variable 'i'
- Use of unassigned local variable 'i'
Compile: 507.910ms | Execution: 0.000ms | React with ❌ to remove this embed.
Aaron
Aaron2y ago
you can use = default; to say you want the default value which might matter if you have say, a generic method, because you won't know what the default is ahead of time
MODiX
MODiX2y ago
Windows10CE#8553
REPL Result: Success
T GetDefaultValue<T>()
{
// can't use = null here, because T might be a struct like int or long.
// also can't use = 0, because it could be a class like string.
T val = default;
return val;
}
GetDefaultValue<int>()
T GetDefaultValue<T>()
{
// can't use = null here, because T might be a struct like int or long.
// also can't use = 0, because it could be a class like string.
T val = default;
return val;
}
GetDefaultValue<int>()
Result: int
0
0
Compile: 459.078ms | Execution: 33.766ms | React with ❌ to remove this embed.
swilli
swilli2y ago
So for locals that makes sense, but I was under the impression fields always got a default value. I'm seeing this in the language extension library though:
swilli
swilli2y ago
wouldn't bottom always get a default value upon instantiation? Regardless of whether A was a class, struct, or primitive type?
Aaron
Aaron2y ago
yes they're just being explicit
cathei
cathei2y ago
While it would have default value, analyzer could yell at you for not assigning anything. Especially for static readonly it’s better to be explicit
333fred
333fred2y ago
For the specific reason of why default exists, and we don't just tell people to use null or new MyStructType(), consider generics:
void M<T>()
{
T t1 = null; // Illegal: if T is a value type, null isn't valid.
T t2 = new T(); // Illegal: If T is a reference type, it may not have a parameterless ctor, and that's not a default instance anyway.
T t3 = default(T); // Null if T is a reference type, all 0s if T is a value type.
}
void M<T>()
{
T t1 = null; // Illegal: if T is a value type, null isn't valid.
T t2 = new T(); // Illegal: If T is a reference type, it may not have a parameterless ctor, and that's not a default instance anyway.
T t3 = default(T); // Null if T is a reference type, all 0s if T is a value type.
}
Accord
Accord2y ago
Was this issue resolved? If so, run /close - otherwise I will mark this as stale and this post will be archived until there is new activity.
swilli
swilli2y ago
That generic local makes things clear. Thanks everyone.
Want results from more Discord servers?
Add your server
More Posts