Discussion: Explicitly typed "var" declarations in C# #376
Replies: 5 comments
-
I prefer either of these: var dictionaryOfStrings = (Dictionary<string, string>)null;
var dictionaryOfStrings = default(Dictionary<string, string>); to Since we have out variables, I don't see a common need to declare a variable without initializing it to a value and I like the explicitness of writing |
Beta Was this translation helpful? Give feedback.
-
I'd agree with you if var dictionaryOfStrings: Dictionary<string, string>;
dictionaryOfStrings["greeting"] = "Hello"; Would raise a "Use of unassigned local variable 'dictionaryOfStrings'" compiler error, while this: var dictionaryOfStrings = (Dictionary<string, string>)null;
dictionaryOfStrings["greeting"] = "Hello"; Compiles just fine. Also, |
Beta Was this translation helpful? Give feedback.
-
@jonathanmarston That was my (poorly made) point. I don't see a common need to declare a local without initializing it to a value. In the very rare cases where this happens, |
Beta Was this translation helpful? Give feedback.
-
I don't think it makes sense to add alternative syntax for declaring variables just to make some code somewhat prettier. Especially considering that:
|
Beta Was this translation helpful? Give feedback.
-
So you want to add a language feature for the handful of cases when you're in this scenario, so that your code is more symmetric? That hardly seems worth the cost of adding another means of specifying types into the language. |
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
-
Problem
You can currently define variables in 3 ways using explicit types or an implicit type using the
var
keyword:This is fine when either all variable declarations are explicit, or when all of them are implicit. However, because there is no way to specify a type using the
var
keyword, the type and the variable are flip-flopped when writing code where some variables are initialized, and others aren't initialized, or are initialized tonull
.Example:
This creates an inconsistent flow in the code, requiring the reader to switch sides of the declaration when looking for the type.
There is, in fact, a workaround of sorts, where you cast
null
to the desired type:However, this changes the meaning of the code by disabling the compiler check for reading a reference variable before assigning to it.
Proposed Solution
It would be helpful to increase consistency and readability of code if you could supply an explicit type when using the
var
keyword, without needing to initialize the variable.With this proposal, the previous example would become:
The syntax here is borrowed from TypeScript, and makes variable declarations in C# read more closely to VB, where the
var
keyword can be consistently used, and the type consistently appears on the right-hand side of the declaration.It would also be valid to specify a type and an initializer concurrently using the
var
keyword:Potential Issues
The main issue I see with this proposal is that some could view it as over-complicating the language by adding another way to say the same thing, but I'd argue that the
var
keyword has already done that. This proposal is simply allowing developers that have embracedvar
to use a more consistent syntax throughout their code.Beta Was this translation helpful? Give feedback.
All reactions