That can be silly question but I really don’t know the reason.
// i is compiled as an int
var i = 5;
// s is compiled as a string
var s = "Hello";
// a is compiled as int[]
var a = new[] { 0, 1, 2 };
// expr is compiled as IEnumerable<Customer>
// or perhaps IQueryable<Customer>
var expr =
from c in customers
where c.City == "London"
select c;
// anon is compiled as an anonymous type
var anon = new { Name = "Terry", Age = 34 };
// list is compiled as List<int>
var list = new List<int>();
If so, why C# needs integral types like int, string, int[], List<> ? If we can do that only with var keywords, why are there integral types?
Another way,
What is the difference between these ?
var s = "Hello";
string s = "Hello";
in no particular order…
varis only a compiler construct; evenvar i = 4;is typed asintvar, and you can’t remove keywords without utterly breaking existing code.intin place of var! Especially when the type matters and there are bytes, floats, enums, decimals etc that all work with the literal0; i.e.short x = 0; int y = 0; long z = 0;– why change onlyinttovarthere?varto specify a generic type parameter (<T>) or in atypeofvar.TryParsein place ofint.TryParse