为什么C#是静态类型的? [英] Why is C# statically typed?

查看:102
本文介绍了为什么C#是静态类型的?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是一名PHP网络程序员,正在尝试学习C#.

I am a PHP web programmer who is trying to learn C#.

我想知道为什么C#要求我在创建变量时指定数据类型.

I would like to know why C# requires me to specify the data type when creating a variable.

Class classInstance = new Class();

为什么我们需要在类实例之前知道数据类型?

Why do we need to know the data type before a class instance?

推荐答案

正如其他人所说,C#是静态/强类型的.但是,我的问题更多是为什么您要想要将C#像这样静态/强类型化?与动态语言相比,它有什么优势?"

As others have said, C# is static/strongly-typed. But I take your question more to be "Why would you want C# to be static/strongly-typed like this? What advantages does this have over dynamic languages?"

考虑到这一点,有很多充分的理由:

With that in mind, there are lots of good reasons:

  • 稳定性现在,在代码无法将其投入生产之前,编译器会自动捕获某些类型的错误.
  • 可读性/可维护性,您现在向将来阅读该代码的开发人员提供有关该代码应如何工作的更多信息.您添加了有关特定变量旨在保存某种类型的值的信息,这有助于程序员推断该变量的用途.

  • Stability Certain kinds of errors are now caught automatically by the compiler, before the code ever makes it anywhere close to production.
  • Readability/Maintainability You are now providing more information about how the code is supposed to work to future developers who read it. You add information that a specific variable is intended to hold a certain kind of value, and that helps programmers reason about what the purpose of that variable is.

例如,这可能就是为什么Microsoft的样式指南建议VB6程序员使用带变量名的类型前缀,而VB.Net程序员不这样做的原因.

This is probably why, for example, Microsoft's style guidelines recommended that VB6 programmers put a type prefix with variable names, but that VB.Net programmers do not.

性能,这是最弱的原因,但是后期装订/鸭式打字的速度可能较慢.最后,变量是指以某种特定方式构造的内存.如果没有强类型,该程序将不得不在运行时在幕后进行额外的类型验证或转换,因为您使用的是一种物理构造的内存,就好像它是另一种逻辑构造的内存一样.

Performance This is the weakest reason, but late-binding/duck typing can be slower. In the end, a variable refers to memory that is structured in some specific way. Without strong types, the program will have to do extra type verification or conversion behind the scenes at runtime as you use memory that is structured one way physically as if it were structured in another way logically.

我会毫不犹豫地提及这一点,因为最终您通常还必须使用强类型语言进行这些转换.只是强类型语言将转换的确切时间和范围留给了程序员,除非有必要,否则它不会做任何额外的工作.它还允许程序员强制使用更有利的数据类型.但是,这些实际上是 programmer 而不是平台的属性.

I hesitate to include this point, because ultimately you often have to do those conversions in a strongly typed language as well. It's just that the strongly typed language leaves the exact timing and extent of the conversion to the programmer, and does no extra work unless it needs to be done. It also allows the programmer to force a more advantageous data type. But these really are attributes of the programmer, rather than the platform.

这本身就是忽略这一点的一个较弱的理由,只是一种好的动态语言通常会比程序员做出更好的选择.这意味着动态语言可以帮助许多程序员编写更快的程序.不过,对于好的程序员来说,强类型的语言具有潜在的更快的速度.

That would itself be a weak reason to omit the point, except that a good dynamic language will often make better choices than the programmer. This means a dynamic language can help many programmers write faster programs. Still, for good programmers, strongly-typed languages have the potential to be faster.

或者也许您只是想知道为什么必须在同一行为同一变量两次指定类名?答案有两个:

Or perhaps you were just wondering why you have to specify the class name twice for the same variable on the same line? The answer is two-fold:

  1. 通常你不知道.在C#3.0和更高版本中,在许多情况下,可以使用var关键字代替类型名称.通过这种方式创建的变量仍然是静态类型的,但是编译器现在为您推断了类型.
  2. 由于继承和接口,有时左侧的类型与右侧的类型不匹配.
  1. Often you don't. In C# 3.0 and later you can use the var keyword instead of the type name in many cases. Variables created this way are still statically typed, but the type is now inferred for you by the compiler.
  2. Thanks to inheritance and interfaces sometimes the type on the left-hand side doesn't match the type on the right hand side.

这篇关于为什么C#是静态类型的?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆