Why C# Need Integral Types already it has “Var” keyword?
本问题已经有最佳答案,请猛点这里访问。
这可能是个愚蠢的问题,但我真的不知道原因。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 | // 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>(); |
如果是这样,为什么
另一种方式,
这些有什么区别?
1 2 | var s ="Hello"; string s ="Hello"; |
(P)特别是挤奶…(p)
- 字母名称
- C′35;Hasn't always had EDOCX1 commercial 0,and you can't remove keywords without utterly breaking existing code.
- You can only use VAR in method variables-not fields,parameters,return types etc.
- 大多数人都是快乐的类型EDOCX1音标2在瓦尔镇!Especially when the type matters and there are bytes,floats,enums,dec马尔s etc that all work with the text EDOCX1 niplication 5;I.E.EDOCX1 individual 6-why change only EDOCX1 indicatual 2
- You can't use EDOCX1 penal 0 to specify a generic type parameter(EDOCX1 penal 10)or in a EDOCX1
- You can't use EDOCX1 penal 12 in place of EDOCX1 penal 13
(P)许多人会说,这是更好的做法,写作以东X1的英文23,拉瑟而不是以东X1的英文24。There are many reasons for this,but the most compelling to me is that I can look at the code and know immediately,without stretching my brain,what type EDOCX1 individual 25.这是我的勇气集中在困难的问题上。(p)(P)In fact EDOCX1 subjective 0 sill was introduced to declare variables whose types could not easily be named.It was not introduced as a conventience,although it can be used that way.(p)
(P)The EDOCX1 original 0 Keyword is just syntactic sugar,which can only be used for local variables in methods.它仍然是一个巨大的typed变量。You can hover the mouse over it in visual studio to see what type it is.(p)(P)我们仍然需要类型作为其他方式,你会如何做DOOCX1你需要的是第16个字母(p)(P)In many cases its best to be explicit,for example when working with literals that could be a number of different types,or to increase readability.(p)
(P)EDOCX1的英文字母17和EDOCX1的英文字母18没有区别。然而,有一个巨大的区别在字母名称19和字母名称20之间。In the first case all of foo's members are accessible,in the latter it's ensured that only interface members is used.这可能是重要的,以确保行为与接口兼容,而不仅仅是具体实施。(p)(P)Also,EDOCX1 0 original can't be used to pass function arguments or to declar member variables.As of use or not to use EDOCX1 English 0,I refer to this question.(p)