I’m trying to practice my english skills, if you are willing to help me with terminology and grammar, please contact me by email or wechat or qq, great rewards await.
As I have learned lots of knowledges in work, writting cleanner C# codes are still challengeable for me.
The book “effective C#”(referd as “The book” in context) is expected to be the saver for me.
item1 Prefer Implicitly Typed Local Variables
According to the book, implicit type “var” is introduced to support anonymous types in C#.
In most cases, for example var foo = new MyType();
almost every one could recognize foo’s type at glance, but some cases behave in opposite manner.
if you write something like
1 | var f = GetMagicNumber(); |
the output could be ambiguous.
So such situation should be avoid on whatever.
msdn gives some examples and we can do more in practice
1 | // Example #1: var is optional because |
For the 1st example, it could be more efficient to let the compiler select a IQueryable<string>
interface, and for the 2nd example, the anonymous type could be assigned as variables without type cast.
Type relationship in linq query operations
The following illustration shows a LINQ to Objects query operation that performs no transformations on the data. The source contains a sequence of strings and the query output is also a sequence of strings.
Relation of data types in a LINQ query
The type argument of the data source determines the type of the range variable.
The type of the object that is selected determines the type of the query variable. Here name is a string. Therefore, the query variable is an IEnumerable
The query variable is iterated over in the foreach statement. Because the query variable is a sequence of strings, the iteration variable is also a string.
Prefer readonly
to const
C# has two types of const variable, one in compile-time, other one is runtime. They behave quite different.
for example,
1 | //compile-time constant |
the compile-time is just a declaration and the number 2000 will replace all Millennium token appeared.
thus, const declaration shouldn’t be used to userdefined type or the type you prefer it to be a reference.
and const is used to define those variables must be resolved.