When should I use "var" in declaring a variable? | Sololearn: Learn to code for FREE!
Новый курс! Каждый программист должен знать генеративный ИИ!
Попробуйте бесплатный урок
+ 20

When should I use "var" in declaring a variable?

For example, var k = 0, as opposed to, int k = 0. Which one is better to use? Do I only use var when I'm not certain of the data type?

1st Nov 2016, 6:49 AM
Cosmo Nibbs
Cosmo Nibbs - avatar
23 ответов
+ 20
"var" automatically uses the correct data-type. One isn't better than another. They are basically the same. Var might need more time because it has to find the data-type but if the program is small it shouldn't matter.
1st Nov 2016, 6:59 AM
nedas
nedas - avatar
+ 19
1. It is like what nedas stated. 2. if you forgot/unsure what type of data-type to fill in. (^^^ I recommend to manually search/find the desired data-type and put it in yourself if you really forget)
1st Nov 2016, 5:36 PM
Wen Qin
Wen Qin - avatar
+ 14
The var keyword in C# lets the compiler infer the data type based upon context (usually, its value assigned determines the data type). So although it can be used in many contexts, it's generally better to use the specific type that's appropriate to be explicit and clear. The best practice for using var is when the value assigned is the result of a LINQ expression, where the type of collection returned might best or most easily determined by the compiler. Examples: int x = 17; string[] greetings = {"hi", "hello", "heya", "howdy-doody"}; var longGreets = from g in greetings where g.Length > 2 orderby g.Length descending select g; The first two statements are easy to determine the type, so we set them explicitly. The third statement results in a variable where the type is dependent on the specifics of the LINQ expression, but we usually only care if it is a collection we can loop through, so var lets the compiler determine the exact type to apply.
2nd Nov 2016, 7:34 AM
OpenRD
OpenRD - avatar
+ 11
no, var do exists in C#, but you have to declare its value if you wanna use it.
1st Nov 2016, 10:32 PM
Wen Qin
Wen Qin - avatar
+ 7
Just to clarify: A programming language that pretends to be classified in the 'strongly-typed' power-list of features can't have the luxury of using the var keyword!
13th Nov 2016, 3:04 PM
Klodian Lula
Klodian Lula - avatar
+ 4
Besides, you can use var to make your code a little bit shorter and nicer.
2nd Nov 2016, 7:40 AM
Remmae
Remmae - avatar
+ 4
It's not really a good practice to use var instead of specific data type. Coding standards in some companies even forbids usage of var keyword. It may look fine and innocent in small programs but in big ones It may be quite confusing. For example: var myValue = GetValue(); It's not clear what kind of data type method returns, and you need to check for that function return type. Anyway, better solution is to use this: int myValue = GetValue(); It is clear that method returns integer and you don't need to worry about It and your code is easier to read.
4th Nov 2016, 1:10 PM
Karolis Strazdas
Karolis Strazdas - avatar
+ 3
hmmm.. Maybe I'm wrong.. but I think you are mixing up concepts... by your tags I guess that you are asking about C++ and in C++ you don't have the "var" keywork.. at least not by default, so far as I know.That's a feature of JS and other languages... So, in C++ you must declare the variable type before using a variable the first time.
1st Nov 2016, 5:27 PM
Nelson Urbina
Nelson Urbina - avatar
+ 2
Nelson the tags say c# not c++. Two different languages.
1st Nov 2016, 6:53 PM
nedas
nedas - avatar
+ 2
Aren't you talking about a strongly-typed language like C#? So var doesn't exist in C# no?
1st Nov 2016, 7:23 PM
Klodian Lula
Klodian Lula - avatar
+ 2
Soooo sorryyyy... nedas and K2 Shape are completely right... I was wrong... I read the tags too fast and thought it was about C++... and yeah, in C# there is a var keywork for implicit type selection during compile time. Even so, I would stick with the strongly-type style. I don't see the advantage besides the weakly coding style which could let to confusion, specially if other coder is watching my code. But maybe is just a matter of taste and likes.
2nd Nov 2016, 2:34 AM
Nelson Urbina
Nelson Urbina - avatar
+ 1
normally I try to declare the data type when declaring a variable, but sometimes it is easier to use var instead. if your not exactly sure what data type is returned like an async Task<> data type or if your being lazy and use var myVar = new List<>(); instead of List<> myVar one thing I remember is when using var you must assign a value on declaration, it cannot be a empty variable; and it may make it hard for people to read if you use a lot of them.
2nd Nov 2016, 12:58 PM
Gregory Rozzo
Gregory Rozzo - avatar
+ 1
I think int is better
2nd Nov 2016, 1:08 PM
Nikita
+ 1
it's better to use strongly type variables. loosely typed variables use var in c# and auto in c++
2nd Nov 2016, 10:00 PM
yungcheda
yungcheda - avatar
+ 1
var is better for small codes but not very good for bigger codes because it takes more time to execute and also it is an implicitly typed keyword i.e values can't be assigned later. eg: var a=5; //correct var a; a=5; //error into a; a=5; //correct
3rd Nov 2016, 3:25 AM
Sachin.. Toppo
Sachin.. Toppo - avatar
+ 1
vinay you are placing a question inside someone elses question. how about make your own?
29th Nov 2016, 12:59 AM
julio
0
"var" is an automaticly selected data type. ex: var x=10 or var x="text" or var x=true etc
2nd Nov 2016, 2:14 AM
Yekta Doğru
Yekta Doğru - avatar
0
alguém fala português?
3rd Nov 2016, 1:35 AM
Iris Pelegrin
Iris Pelegrin - avatar
0
I suppose only if you don't require any data hiding or encapsulation
4th Nov 2016, 12:38 PM
Joseph
Joseph - avatar
0
In my opinion var is used for security purposes. You allow the user to enter whatever information they like without crashing the system. Then doing your own type check on the information enter by the user before storing the data in it's permanent location.
13th Nov 2016, 3:53 AM
Gary Colbert
Gary Colbert - avatar