What is the use of the keyword var

The discussions about the keyword "var" in C # are still going on

The use of "var" in C # is not untyped programming, nor is it automatically a bad programming style.

As part of our information day on .NET 2015 and C # 6.0 yesterday in Essen, an old discussion among the C # developers reboiled up again: There were a handful of participants who massively criticized the lecturers for using the C # keyword var use. This is "untyped programming" and also "very bad programming style".

var is not untyped programming

The key word var Microsoft introduced C # 3.0 (in 2007), and I had thought that by now all C # software developers knew that var is not untyped programming and therefore does not deal with the Variant in Visual Basic or the var in JavaScript.

With var you can shorten the notation of a variable declaration. Instead of

Customer management. customer customer = new customer management. customer ();

one can write

var customer = new customer management. customer ();

After that is the variable customer just as strongly typed as in the first spelling. In no way can one then use the variable customer use it differently. The assignments customer = 1 or customer = "text" the C # compiler no longer accepts. The criticism that var is "untyped programming" is therefore unfounded.

Also "very bad programming style" is the use of var not, because Microsoft even recommends the use of in their "C # Coding Conventions (C # Programming Guide)" var in the cases shown yesterday like the one above:

"Use implicit typing for local variables when the type of the variable is obvious from the right side of the assignment, or when the precise type is not important." It also says: "Do not use var when the type is not apparent from the right side of the assignment". I agree: You should definitely not always var and the lecturers didn't do that yesterday either. You have worked according to Microsoft's "C # Coding Conventions". A nice contribution to the discussion too var, which underlines the usage shown yesterday, can also be found here: "Uses and misuses of implicit typing".

Resharper warns if you don't var used

Coding style tools, such as the established Resharper for C #, even generate a warning (!) And suggest a correction if one var not used:

The Resharper manufacturer JetBrains gave detailed reasons for this warning back in 2008 and came to the conclusion "improve the way your code speaks for itself".

Indeed, the readability of the program code becomes through var often better:

Bad:

Dictionary > dictionary
= new Dictionary > ();

Better:

var dictionary = new Dictionary > ();

Closely considered are those who var dislike, a small minority, as seen in only 8 (sic!) votes for the proposal to introduce a StyleCop rule "Abuse of the 'var' keyword". This minority should tolerate the majority sticking to the C # coding conventions.

The .NET 2015 information day will take place again on May 11th in Munich. I am curious if there var will also be an issue again.

Holger Schwichtenberg

Dr. Holger Schwichtenberg is Chief Technology Expert at MAXIMAGO, which offers innovation and experience-driven software development, including in highly critical security-related areas. He is also head of the expert network www.IT-Visions.de, which, with 38 renowned experts, supports numerous medium-sized and large companies with advice and training in the development and operation of software.

Read CV ยป