Jesper Niedermann's .NET Blog
about .NET and related technologies RSS 2.0
# Tuesday, 07 April 2009

I recently was in a company where I created the coding standard. Mostly by referring to the great standard from IDesign but with a twist of company specific things. When C#3.0 came out I couldn't find any coding standard material on the new stuff out there on the web. So I was forced to think...

My thoughts on the var keyword, is that it should be used extensively. But I place a restriction on it. You should only use it if the type of the object is explicitly readable from the line.

So this is allowed:

var person = new Person();

And this is not allowed:

var person = PersonFactory.Create();

And similar for a Linq expression.

Allowed (actually it is required because of the anonymous type):

var person = 
from c in Customers
select new { c.FirstName, c.LastName };

Not allowed:

var person = 
from c in Customers
select Person.Create(c.FirstName, c.LastName);

Otherwise I place no restriction on the use. This way you get maximum readability without comprising that you should always know the type of what you are dealing with. Even when not using intellisense.

At the meeting I mentioned in "The Missing Linq" I asked Anders Hejlsberg why var is not possible on member variables in C#. He told me that they are looking into parallelizing the compiler. And it would be impossible to infer the type across assemblies compiled by different threads of the compiler.

Tuesday, 07 April 2009 09:01:55 (GMT Daylight Time, UTC+01:00)  #    Comments [0] -
Comments are closed.
<2018 September>
About the author/Disclaimer
I am a software architect with focus on Microsoft Technologies. I have been working with these in different large companies since 1995. I am currently employed at UVdata A/S.
Here is my View Jesper Niedermann's profile on LinkedIn

The opinions expressed herein are my own personal opinions and do not represent my employer's view in any way.

Privacy policy
The privacy policy of this site.

© Copyright 2018
Jesper Niedermann
Sign In
Total Posts: 28
This Year: 0
This Month: 0
This Week: 0
Comments: 32
All Content © 2018, Jesper Niedermann
DasBlog theme 'Niedermann' created by Jesper Niedermann, based on 'Business' created by Christoph De Baene (delarou)