0

What is the "proper" way to use

var x = "text";

or

string x = "text";

Does it matter which I use? Is there a "standard" way people usually do it? I only ask because sometimes Rider suggests that I use var.

Scott Chamberlain
  • 124,994
  • 33
  • 282
  • 431
Ben Richeson
  • 63
  • 2
  • 3
  • It doesn't affect compile time, for one. Really, just use it when you feel comfortable. It also makes code looks cleaner. For example, If I have a `Dictionary, boolean> dictionary`, I don't want to have to explicitly type that every single time. – Frontear Nov 10 '18 at 04:14

3 Answers3

3

There are a few times where you must use an explicit type, and instances where you must use var.

Must use explicit type:

If you declare a variable without initializing it, or, if you want to initialize a variable explicitly as null

 string myString;
 int i;
 string someString=null;
 DateTime? nullableDate=null;

Must use var:

When you use an anonymous type, or a generic type with one or more type parameters that are anonymous types.

var stuff = new {Name="Bob", Age=25};
var bunchOfStuff = from item in items select new {item.Name, item.Age};

Otherwise

It's a question of style and/or taste

Flydog57
  • 6,851
  • 2
  • 17
  • 18
  • Another must-use of `var` is in Reflection, when dealing with classes that are not public, hence cannot be directly referenced or cast to a known type. – Jimi Nov 10 '18 at 05:22
  • @Jimi: Yes, but... Reflection is an `object`-based API. You can say `var myList=Activator.CreateInstance(typeofAListofAnonymousTypes);`, but the static type of `myList` will simply be `object`. The compiler won't let you call `Add` on it unless you cast it to an `IList` first. Yes, I usually use `var` with Reflection, but I also use it with strings. – Flydog57 Nov 10 '18 at 16:03
  • Not what I meant: take the [`ExtractSslProtocol` snippet from here](https://stackoverflow.com/questions/48589590/which-tls-version-was-negotiated?answertab=active#tab-top) and try to solve it without using the type inference provided by `var`. Those are not `object` types and not anonymous types. – Jimi Nov 10 '18 at 16:47
  • All the uses of `var` that I see in that sample declare variables of type `reference to object`. The object to which they refer has a full type (just like my `Activator.CreateInstance` example does), but as far as the compiler knows, they are an `object`. The cool thing about `var` isn't that it can declare a variable that can be any type (`object` or `dynamic` are good enough for that), it's that the compiler can deduce the type and properly type the _variable_. – Flydog57 Nov 10 '18 at 17:03
  • None of those types are `object`. They're private/internal classes and properties referencing classes that can't be cast to a .Net class. – Jimi Nov 10 '18 at 17:06
1

var is mostly syntactic sugar however there are times when you must use var and that's when you assign variables to anonymous types.

Anonymous type example:

var anonymousType = new { Name = "Mathew", DOB = DateTime.Now };
Console.WriteLine($"{anonymousType.Name} : {anonymousType.DOB}");

As far as when to use it otherwise... this is an opinion and or team guideline. There's nothing gained or lost doing it either way. That said; here's my opinion.. this question will get put on hold for too broad but in the mean time here's a little something to look at however you choose.

I like var but I believe it can be confusing if you're not careful how you name / use variables but... personally I prefer var. It's shorter and can clean up code or make it somewhat shorter. Especially when you have somethin like Person person = new Person(); There's just too many Person's there for me... I also like how var mimics the syntax of JavaScript somewhat (although the two declarations are different).

var is not different than declaring the type out right so in your question string x or var x it doesn't matter. The compiler infers the type that is being assigned and that's what var becomes.

I do recommend this... When you declare ANY variables, especially those using var I would always name the declaration as close to the type as possible so that it doesn't matter which you choose. Name the variables correctly and no one should give you a hard time either way... Then again; this is all opinion.

var person1 = new Person();
Person person2 = new Person();

var employee1 = new Employee();
Employee employee2 = new Employee();

var employee3 = new Employee();
Person person3 = new Employee();    

person1.Name = "Mathew";
person2.Name = "Mark";

employee1.Name = "Luke";
employee2.Name = "John";
employee3.Name = "Acts";

person3.Name = "Romans";
Michael Puckett II
  • 6,586
  • 5
  • 26
  • 46
1

The documentation for var says:

An implicitly typed local variable is strongly typed just as if you had declared the type yourself, but the compiler determines the type.

In the end, there is functionally no difference. So, to answer your question, no, it doesn't matter which one you use. And it is for that very reason that there is no "standard" or "proper" one to use.

Use what you prefer.

Gabriel Luci
  • 38,328
  • 4
  • 55
  • 84