Please use caution in the C # var

we all know,For example, Swift 、 Python,,You do not need to declare a special variable type - although they are type-safe languages,But it can help you to dynamically infer the type of a variable in context。

Python do what you do,Write a name is the default declare a variable;And Swift,It is more close to our topic,It also uses where

If you have had experience in the use Swift,Again when using C #,Will find the where ,Is no longer the where

In C #,You can not casually use var,You can only use it in the process of lining。

Speaking where ,In fact, it touches the same name,They are variable,Meaning all variables。The reason for using the word,Meaning that in most cases so that you do not have to write a complex type declaration,Only you need to focus your content like,And the rest to type inference to complete。

However, in C #,It is more conservative for this support。You can not like, like Swift,Can not be used everywhere like Python type inference --C # only allow you to use the local variable type inference;That is,You can only use a specific method in where ,As for the statement of the type of property? Have old grammar。

C # type inference is not as clever as you think

In the MSDN,on where ofIntroduction pageVery simple,Only a few simple chestnuts - of course,In these simple cases C # type inference works well。

But if I want to ListView Being in a Item ,So unfortunately,You will not get ListViewItem Example,And most likely a object

The reason why talk about this example,Because it has troubled me for a long time,We finally found out that C # type inference to blame。

Type inference makes the code ambiguities?

Other,For this self 2008 Function since,It seems not many people use,But a lot of controversy,such as: C # debate:When should I use var?

I contacted the time is not long C #,But in var stepped pit but a lot。But I used Python and Swift ,I did not think that type inference makes sense ambiguous code,To read nor pain - on the contrary,It makes me more time to write the code to focus on the content - namely "Statement - Use”,Rather than go to think about what type。When I think of the contents of the time,The compiler has been recommended for the type I - the majority of cases are suitable。

The C # it? It is the only conservative type inference used in the local variable - and thanks so right,Otherwise, this should be considered a disaster。 C # type inference can always just right to avoid the type I most wanted。So I think,Use type inference In C #,Not make sense of ambiguous codes,But to let you do not know what type of a variable is declared Why ......

- You do not know when you become a variable type is declared object ,Also used smugly ToString() Technique。

 

Original article written by Gerber drop-off:R0uter's Blog » Please use caution in the C # var

Reproduced Please keep the source and description link:https://www.logcg.com/archives/1722.html

About the Author

R0uter's Blog

The non-declaration,I have written articles are original,Reproduced, please indicate the link on this page and my name。

Comments

Leave a Reply

Your email address will not be published.