This project is read-only.

short variable declaration using :=

Topics: C# Language Design
Apr 4, 2014 at 6:15 PM
Edited Apr 6, 2014 at 5:13 PM
GoLang uses := for short variable declaration. Would love to have this in C#.

current C#
int i = 0;

var i = 0;
alternate shorthand version
i := 0;
Apr 7, 2014 at 2:24 AM
You're talking about a difference of 2 characters (3 if you include whitespace). There is no semantic difference, and it's not much shorted, so it's hard to argue it's easier to read.

In addition i := 0 could be easily misread as i = 0 to someone not familiar with the shorthand and the interpretation could be completely different than what is expected.

I remember the addition of the var keyword caused enough confusion to some people, many who don't follow the latest news didn't realize it was a new keyword, and thought it may have been a new type. These types of people might not even realize that the : is there, and I don't think saving 3 letters is worth the confusion.
Apr 7, 2014 at 2:45 AM
for(i := 0; i < 100; i++) {...}
... nay, I don't want it. I'll get more trouble with it. Even though I like var - it's misused heavily and this is just another var with more terse syntax.