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.
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.
There are a few times where you must use an explicit type, and instances where you must use var
.
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;
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};
It's a question of style and/or taste
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";
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.