var lady first

Local variables in C # implicit type is to support anonymous types arise, and also to solve the result of part of the query operation returns IQueryable type, and the result of another part of the query returns IEnumerable type due to the IQueryable as IEnumerable to use, and thus can not use enhanced features IQueryProvider provided. Here there will be a worry if readers did not specify the type of the variable type is not cause safety problems. So, here I want to tell our readers that the developer did not specify the type of security have variable types and variables nothing to do, so we do not worry. Var using implicit declaration of a variable type in most cases, the compiler will automatically select the appropriate type to handle. See here, there will be readers say that since there is a clear type, why not clearly specify the type of it, and will use the implicit variable easy for other developers do not know the type of the variable. So now I give you answer it.

First, explicit type may cause changes in the order of execution of the code, which I will explain in detail in a later article. Secondly, this is a naming convention coding problem, we can see that the type of the variable type by right-hand side when a new object. E.g:

var s = new Student();

From the above code, we can see that the type variable s Student, but this code there is a problem, is named variables. s name mixed in a large amount of code is difficult to see its type. Then we changed the name of the variable student can easily see the type of the variable. This variable naming conventions must remember that this rule can be said that you keep in mind. This error is not just novice developers will be committed, I have seen in practical work and projects in a number of so-called technical cattle this error, and finally do not know their own name of this variable is doing, what is Types of. [Laughs cry] some experienced developers tend to be novice developers say not to use anonymous types, because it affects affect the performance of static type checking. Well, I am here to say that this is entirely in nonsense! First, the local variable is not equal to type inference dynamic type checking, variable var not declared dynamic variables, c # variable type may be determined according to the type of the value to the left operand of an assignment of the right of the equal sign. Second, the compiler will automatically determine the type. Here, I need to emphasize that the real type implicitly typed variables is determined by the method signature, that regardless of the implicit type of real type is a class that inherits from a class or class implements one or more interfaces as long as there is no clear performs type conversion, the compiler will decide the true type of implicit type according to the method's signature.

Above we said implicit type of advantage, then we'll look for shortcomings implicit type. First var variable declaration makes the code to read it a bit difficult, because there may be what we consider the final type and the type of compiler is not the same, which led to an error in the code lead to the development of maintenance bug. This is because the var declared variable compiler will automatically infer its type, but developers can not see the type inferred. Secondly, true type if you use implicitly typed variables are built-in numeric types can cause problems if the type of conversion accuracy decline. This problem occurs only in a narrow conversion, such as conversion to long int. The problem is not the compiler, but because the compiler will type their own set of variables, that is the best choice to make part of an assignment based on the right of the equal sign.

Tip: This refers to the implicit type of variables are local variables

发布了204 篇原创文章 · 获赞 101 · 访问量 35万+

Guess you like

Origin blog.csdn.net/gangzhucoll/article/details/103966170