Click here to Skip to main content
14,266,194 members
   

C#

 
GeneralRe: Why is this necessary? Pin
Brian_TheLion17-May-19 15:35
memberBrian_TheLion17-May-19 15:35 
GeneralRe: Why is this necessary? Pin
OriginalGriff17-May-19 20:09
protectorOriginalGriff17-May-19 20:09 
AnswerRe: Why is this necessary? Pin
OriginalGriff15-May-19 21:32
protectorOriginalGriff15-May-19 21:32 
GeneralRe: Why is this necessary? Pin
Brian_TheLion16-May-19 15:02
memberBrian_TheLion16-May-19 15:02 
GeneralRe: Why is this necessary? Pin
BillWoodruff17-May-19 15:18
mveBillWoodruff17-May-19 15:18 
GeneralRe: Why is this necessary? Pin
OriginalGriff17-May-19 19:59
protectorOriginalGriff17-May-19 19:59 
GeneralRe: Why is this necessary? Pin
jschell18-May-19 6:52
memberjschell18-May-19 6:52 
AnswerRe: Why is this necessary? Pin
Richard Deeming16-May-19 1:47
mveRichard Deeming16-May-19 1:47 
For local variables, it's not necessary to repeat the type name:
var - C# Reference | Microsoft Docs[^]
Implicitly typed local variables - C# Programming Guide | Microsoft Docs[^]

The following two lines produce identical IL:
Animal fox = new Animal();
var fox = new Animal();

NB: You can't use var for fields, property types, method parameters, or return types.
Why no var on fields? – Fabulous Adventures In Coding[^]

There is a suggestion which would allow fields declared as: Animal fox = new();, but it hasn't been implemented yet:
csharplang/target-typed-new.md at master · dotnet/csharplang · GitHub[^]

NB2: Some people vehemently oppose any use of var beyond anonymous types. And it certainly can be overused - for example, var x = Foo(); would compile, but is not readable. But for a new expression, where the type is right next to the variable declaration, I don't see any problem with using var.

NB3: To clarify, based on the responses: using var for new expressions is fine; you should generally avoid it for anything else.
  • var x = new SomeType(); is fine.
  • var x = SomeMethod(); is bad.




"These people looked deep within my soul and assigned me a number based on the order in which I joined."
- Homer


modified 16-May-19 9:45am.

GeneralRe: Why is this necessary? Pin
OriginalGriff16-May-19 2:44
protectorOriginalGriff16-May-19 2:44 
GeneralRe: Why is this necessary? Pin
Gerry Schmitz16-May-19 3:49
mveGerry Schmitz16-May-19 3:49 
GeneralRe: Why is this necessary? Pin
OriginalGriff16-May-19 3:55
protectorOriginalGriff16-May-19 3:55 
GeneralRe: Why is this necessary? Pin
Richard MacCutchan16-May-19 4:03
protectorRichard MacCutchan16-May-19 4:03 
GeneralRe: Why is this necessary? Pin
Gerry Schmitz16-May-19 5:02
mveGerry Schmitz16-May-19 5:02 
GeneralRe: Why is this necessary? Pin
Brian_TheLion16-May-19 19:12
memberBrian_TheLion16-May-19 19:12 
GeneralRe: Why is this necessary? Pin
Richard MacCutchan16-May-19 3:33
protectorRichard MacCutchan16-May-19 3:33 
GeneralRe: Why is this necessary? Pin
Richard Deeming16-May-19 3:41
mveRichard Deeming16-May-19 3:41 
GeneralRe: Why is this necessary? Pin
Richard MacCutchan16-May-19 3:48
protectorRichard MacCutchan16-May-19 3:48 
GeneralRe: Why is this necessary? Pin
Richard Deeming16-May-19 3:53
mveRichard Deeming16-May-19 3:53 
GeneralRe: Why is this necessary? Pin
Gerry Schmitz16-May-19 3:52
mveGerry Schmitz16-May-19 3:52 
GeneralRe: Why is this necessary? Pin
Brian_TheLion16-May-19 14:44
memberBrian_TheLion16-May-19 14:44 
GeneralRe: Why is this necessary? Pin
Richard MacCutchan16-May-19 21:53
protectorRichard MacCutchan16-May-19 21:53 
GeneralRe: Why is this necessary? Pin
Brian_TheLion17-May-19 15:10
memberBrian_TheLion17-May-19 15:10 
GeneralRe: Why is this necessary? Pin
BillWoodruff17-May-19 15:29
mveBillWoodruff17-May-19 15:29 
GeneralRe: Why is this necessary? Pin
Brian_TheLion17-May-19 18:28
memberBrian_TheLion17-May-19 18:28 
GeneralRe: Why is this necessary? Pin
Brian_TheLion16-May-19 14:47
memberBrian_TheLion16-May-19 14:47 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.