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
With var you can shorten the notation of a variable declaration. Instead of
one can write
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:
= 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.
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 »
- Good guys cheat the most
- The inductor blocks the alternating current completely
- There are salmon in Lake Michigan
- Most slasher movies are predictable
- What does instantaneously mean in physical terms
- How is logic used in computer programming
- When did pianos have pedals?
- What is the course of pharmacovigilance
- What are the different types of submissives
- Should I be more suggestible or more analytical
- What is the 72 hour rule
- What kind of animal is a dingo
- Coldplay is a chick band
- Which subjects are out of date?
- Can we distribute prasadam on ekadashi
- Is a keto diet expensive
- Can root canals cause bad breath
- What is semiconductor equipment
- How common is abuse in mental hospitals?
- What are Richie Lee's companies doing
- What is an online game
- Is Mark Knopfler homophobic If so, how
- Which games are similar to Spent
- Day trading is an ethical business