C# 是强类型语言还是弱类型语言?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/14946043/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Is C# a strongly typed or a weakly typed language?
提问by stackoverflow
Can someone please clarify if C# a strongly typed or a weakly typed language? And explain the answer why.
有人可以澄清一下 C# 是强类型语言还是弱类型语言吗?并解释原因。
if I have a function called concat
that can take any object is this then considered weakly typed?
如果我调用了concat
一个可以接受任何对象的函数,那么这是否被认为是弱类型的?
function concat(Object stuff)
{
//do something here to stuff
}
回答by Servy
Hereis a blog post on the subject from someone who was (at the time he wrote the post) one of the principal developers of the C# compiler.
这是一篇关于这个主题的博客文章,来自一个人(在他写这篇文章时)是 C# 编译器的主要开发人员之一。
In short, the question itself is flawed and cannot be reasonably answered in its current form.
简而言之,这个问题本身是有缺陷的,以目前的形式无法合理回答。
回答by Gideon
From http://ericlippert.com/2012/10/15/is-c-a-strongly-typed-or-a-weakly-typed-language/
来自http://ericlippert.com/2012/10/15/is-ca-strongly-typed-or-a-weakly-typed-language/
Is C# a strongly typed or a weakly typed language?
Yes.
That is unhelpful.
I don't doubt it. Interestingly, if you rephrased the question as an "and" question, the answer would be the same.
What? You mean, is C# a strongly typed and a weakly typed language?
Yes, C# is a strongly typed language and a weakly typed language.
I'm confused.
Me too. Perhaps you should tell me precisely what you mean by "strongly typed" and "weakly typed".
Um. I don't actually know what I mean by those terms, so perhaps that is the question I should be asking. What does it really mean for a language to be "weakly typed" or "strongly typed"?
"Weakly typed" means "this language uses a type verification system that I find distasteful", and "strongly typed" means "this language uses a type system that I find attractive".
C# 是强类型语言还是弱类型语言?
是的。
那是没有帮助的。
我不怀疑。有趣的是,如果您将问题改写为“和”问题,答案将是相同的。
什么?您的意思是,C# 是一种强类型语言还是一种弱类型语言?
是的,C# 是一种强类型语言和一种弱类型语言。
我糊涂了。
我也是。也许你应该准确地告诉我你所说的“强类型”和“弱类型”是什么意思。
嗯。我实际上不知道这些术语是什么意思,所以也许这就是我应该问的问题。“弱类型”或“强类型”的语言究竟意味着什么?
“弱类型”的意思是“这种语言使用了一种我觉得令人反感的类型验证系统”,“强类型”的意思是“这种语言使用了一种我觉得很有吸引力的类型系统”。
回答by Plynx
There are many, many definitions of both strongly typedand weakly typed, to the point where you need to define what you mean by the term when you use it. A definition I find useful is "does the language force me to specify a type for things like parameters?"This separates languages like C# to one side and Javascript to the other, a distinction I find useful.
强类型和弱类型的定义有很多很多,以至于您在使用该术语时需要定义该术语的含义。我认为有用的定义是“语言是否强迫我为参数之类的东西指定类型?” 这将 C# 之类的语言与一侧分开,将 Javascript 与另一侧分开,我认为这种区别很有用。
Requiring someone to name a type instead of relying on things like "duck typing" creates advantages in static analysis but disadvantages when it comes to specifying types that share common functionality. For this reason, many of these languages evolve elaborate type relation specification systems, typically first class-based programmingand later elaborate template systemsor inference systemsso that programmers can say things like "type A is a subset of type B" or "type C is a metatype that can be applied to any other type that satisfies conditions D and E" and so on.
要求某人命名类型而不是依赖于诸如“鸭子类型”之类的东西在静态分析中具有优势,但在指定具有公共功能的类型时却不利。出于这个原因,这些语言中的许多开发了复杂的类型关系规范系统,通常首先是基于类的编程,然后是复杂的模板系统或推理系统,以便程序员可以说“类型 A 是类型 B 的子集”或“类型 C”是一种元类型,可以应用于满足条件 D 和 E" 等的任何其他类型。
回答by Mr. Young
C# is strongly typed.
C# 是强类型的。
ECMA-334 Defines C# as "C# (pronounced “C Sharp”) is a simple, modern, object oriented, and type-safe programming language."
ECMA-334 将 C# 定义为“C#(发音为“C Sharp”)是一种简单、现代、面向对象且类型安全的编程语言。”
Wikipedia defines type safety
维基百科定义了类型安全
Type safety is synonymous with one of the many definitions of strong typing; but type safety and dynamic typing are mutually compatible.
类型安全是强类型的众多定义之一;但是类型安全和动态类型是相互兼容的。
Wikipedia defines strong-typing as
维基百科将强类型定义为
In computer science and computer programming, a type system is said to feature strong typing when it specifies one or more restrictions on how operations involving values of different data types can be intermixed. The opposite of strong typing is weak typing.
在计算机科学和计算机编程中,如果类型系统指定了一个或多个关于如何混合涉及不同数据类型的值的操作的限制,则该类型系统被称为具有强类型。强类型的反面是弱类型。
Perhaps it's better to ask if C# is a type-safe language since nobody can agree on what "strong" and "weak typing" really mean if the compiler will do type checking.
也许最好问问 C# 是否是一种类型安全的语言,因为如果编译器会进行类型检查,没有人会就“强类型”和“弱类型”的真正含义达成一致。
C# does have somedynamic language like constructs available but remarkably these are still type-safe at compile time.
C# 确实有一些动态语言,如可用的构造,但值得注意的是,这些在编译时仍然是类型安全的。
Beginning in Visual C# 3.0, variables that are declared at method scope can have an implicit type
var
. An implicitly typed local variable is strongly typedjust as if you had declared the type yourself, but the compiler determines the type.
从 Visual C# 3.0 开始,在方法范围内声明的变量可以具有隐式类型
var
。隐式类型的局部变量是强类型的,就像您自己声明了类型一样,但编译器会确定类型。
http://msdn.microsoft.com/en-us/library/bb383973.aspx
http://msdn.microsoft.com/en-us/library/bb383973.aspx
The dynamic
keyword basically works the same way except it is evaluated at run-time instead of at compile time as the case with var
.
该dynamic
关键字的基本工作方式相同,除了它是在运行时,而不是在编译时与情况进行评估var
。
Visual C# 2010 introduces a new type, dynamic. The type is a static type, but an object of type dynamic bypasses static type checking. In most cases, it functions like it has type object. At compile time, an element that is typed as dynamic is assumed to support any operation.
Visual C# 2010 引入了一种新类型,dynamic。该类型是静态类型,但动态类型的对象绕过静态类型检查。在大多数情况下,它的功能就像它具有类型对象一样。在编译时,假定类型为动态的元素支持任何操作。
回答by ispiro
In general: C# is usedin a strongly typed manner, meaning: a variable is declared of a specific Type
(either: string, int, a user-defined type, etc.) and cannot, later, be assigned a value of a different type.
一般而言:C#被用于在强类型的方式,这意味着:一个变量声明特定的Type
(或者:字符串,整数,一个用户定义的类型,等等),并且不能,以后,被分配一个不同类型的值.
For example: You can't have the following in C#:
例如:您不能在 C# 中包含以下内容:
int i = 10;
i = "ten";
As in comments below, C# canbe used differently.
正如在下面的评论中,C#可以以不同的方式使用。
Using "strong" typing lets the editor/compiler alert you to mistakes, and lets the editor give you suggestions narrowed down to what you are likely going to need.
使用“强”类型可以让编辑器/编译器提醒您出现错误,并让编辑器向您提供缩小到您可能需要的范围的建议。