+ 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?
22 Respuestas
+ 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.
+ 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)
+ 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.
+ 11
no, var do exists in C#, but you have to declare its value if you wanna use it.
+ 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!
+ 4
Besides, you can use var to make your code a little bit shorter and nicer.
+ 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.
+ 2
Nelson the tags say c# not c++. Two different languages.
+ 2
Aren't you talking about a strongly-typed language like C#? So var doesn't exist in C# no?
+ 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.
+ 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.
+ 1
I think int is better
+ 1
it's better to use strongly type variables. loosely typed variables use var in c# and auto in c++
+ 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
+ 1
vinay you are placing a question inside someone elses question. how about make your own?
0
"var" is an automaticly selected data type.
ex: var x=10 or var x="text" or var x=true etc
0
alguém fala português?
0
I suppose only if you don't require any data hiding or encapsulation
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.
0
Var is a keyword.
By using 'var' you can put any type of data in that var type variable.
Once you initialize the value of that variable then after that only that type of data you can put in that variable.
Example :
Var a;
a='SoloLearn';
Here variable 'a' is string typed variable and will always take string typed value.