1024programmer Asp.Net Is var in C# strongly typed or weakly typed?

Is var in C# strongly typed or weakly typed?

Is var in C# strongly typed or weakly typed?

Foreword

In C#, the var keyword is used to declare variable types. It is a new feature introduced in C# 3.0. It allows the compiler to infer variable types based on initialization expressions. It is somewhat similar to JavaScript, and var in JavaScript is Weak type. It makes C# variable declarations more concise, but it also leads to some debates about whether C# is strongly or weakly typed.

Analysis

In the traditional sense, strongly typed languages ​​require variables to specify a type when they are declared, while weakly typed languages ​​allow variables to not specify a type. In C#, the var keyword can be used to declare variables without a specified type. It seems that C# is a weakly typed language.

However, C# is actually a strongly typed language because the compiler checks variable types during compilation. If the variable initialization expression types do not match, the compiler will report an error. Therefore, the var keyword in C# is not an arbitrary type of js, but allows the compiler to infer the variable type based on the initialization expression.

Case

We can see what type it is by decompiling the intermediate language of var. Take a look at the case below.

1. First create a new .NET 6 console project. Create 4 var variables of different types respectively. Generate it and run it normally. Finally, use the decompilation software ILspy to view the dll file just generated. As shown below

2. Open the dll file and find the main function in Program. We can see that var disappears, indicating that they have inferred the variable type at compile time.

3. We switched to IL intermediate language, and the var keyword was not found. From the decompilation results, var is a strong type, and the initialized variable type can be inferred through compilation.

So why does C#, as a static language, introduce var?

This also depends on LINQ. LINQ can perform filtering, transformation and other SQL-like operations on various IEnumerable types. This simplifies the complexity of strong type operations, and the types returned by different operations are somewhat comparable. Complex, it would be cumbersome to write out the type code every time. In this case, the var keyword syntax sugar is introduced.

Conclusion

This article talks about whether var is a strong type or a weak type, and uses cases to prove that var is a strong type. Do you think var is a strong type or a weak type? Welcome to leave a message for discussion.

1. Interviewer: Can you explain clearly the var and dynamic keywords in C#?

2. As a statically typed language, why does C# introduce var?

Source public account: DotNet development job-hopping
 
This article is from the internet and does not represent1024programmerPosition, please indicate the source when reprinting:https://www.1024programmer.com/is-var-in-c-strongly-typed-or-weakly-typed-2/

author: admin

Previous article
Next article

Leave a Reply

Your email address will not be published. Required fields are marked *

Contact Us

Contact us

181-3619-1160

Online consultation: QQ交谈

E-mail: [email protected]

Working hours: Monday to Friday, 9:00-17:30, holidays off

Follow wechat
Scan wechat and follow us

Scan wechat and follow us

Follow Weibo
Back to top
首页
微信
电话
搜索