+ 3
Difference between var and int
Is there a difference between "var num = 15" and "int num = 15"? Performance wise or memory wise? Thanks in advance.
9 Respostas
+ 9
All of the other answers are wrong.
C# is strongly typed, which means that every variable needs to be defined with a type. it can not change type at run time.
the keyword var can only be used if the variable type can be inferred by the compiler. it's only function is to make the code look more clean.
I.e. let's say I want to make a list of strings. I could either do this:
List<string> myList = new List<string>();
From the right hand side if the expression the compiler knows the type, so it is redundant to write it twice and you can instead use the var keyword
var myList = new List<string>();
what you cannot do is something like this:
var myList;
myList = new List<string>();
because the compiler does not know which type to give myList when it is declared
+ 5
If you know then there is no need of using var keyword just use the specific type that you want :)
Don't forget to thumbs up my answer :)
+ 4
As you know every variable has a specific type and you declare variable according to your need as which type of variable you want.. Right?
But in some cases you don't know what type or variable you need actually.. In this case you can declare a variable with var keyword. The compiler will assign a suitable variable type to you variable during the execution of program :) hope you understand.. If any problem comment again :)
+ 2
I understand the part in which "I don't know the type". But what if I do?
If I know that I will be using a number, is there any difference?
+ 2
if U have numbers U want to stor it ..so .U mast use " int "..
+ 1
Using var means that on the time of the initialization you do not know what variable type you need so you let the c# compiler decide.
0
Using var means that on the time of the initialization you do not know what variable type you need so you let the c# compiler decide.
0
Var can be Int, double, char und string. But int is just whole number,
Var x='H'; //this is char
Var x=15; //this is int
BUT !
int x=15 //whole number
int x='H' //ERROR
- 4
a