术语“类型”的最佳定义是什么?特别是在 C# 语言中?

发布于 2024-11-25 00:41:39 字数 474 浏览 9 评论 0原文

这可能是一个非常简单的问题,但我对 Type 定义感到困惑。

当您想要提供术语类型的定义时,例如Int、String或...
立即出现Template一词介意,但它与“Class”定义非常接近,现在我想知道术语Type的确切定义是什么。

与类定义相同!? 类型==类!?

如果是这样,我们就知道 c# 中还有其他(参考)类型不是 Class,例如 InterfaceDelegate

互换使用类型和类是否正确?
您能给我一个关于类型的全面定义吗
提前致谢。

This is probably a very simple question but I have a confusion about the Type definition.

When you want to provide a definition of the term Type such as Int, String or...
Immediately the word Template comes to mind, but its so close to the "Class" definition, now I want to know what is exactly the definition of The term Type.

Is it the same as class definition !? Type == class !?

If so, we know that there are other (reference)Types in c# that are not Class such as Interface and Delegate.

Is it correct to use Type and Class interchangeably?
Could you please give me a comprehensive definition about Type.
thanks in advance.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(7

荒人说梦 2024-12-02 00:41:39

正如其他人所指出的,C# 规范没有正式定义“类型”。 C# 规范并不试图成为语言语义的正式数学描述或初学者程序员的教程;在开始阅读规范之前,您应该知道“类型”和“值”等词语的含义。

“类型”有许多可能的定义,其精度水平各不相同。例如,ECMAScript 规范在某种程度上非正式地将类型定义为“一组值”,但由于 ECMAScript 只有九种可能的类型,因此不需要有严格的、有根据的定义。

另一个答案说类型由一组值、一组对这些值进行操作的规则和一个名称组成。这是类型的一个非常常见的工作定义,但是当您尝试更正式地考虑它时,它会遇到问题。匿名类型的名称是什么? double*[][] 是“指向 double 的指针的锯齿状二维数组”类型的名称吗?这种类型有名字吗? ListList 是同一类型的两个不同名称吗? 任意组值是否构成类型?类型本身就是值吗?什么是类型的类型?等等。这是一个很好的工作定义,但它并不能经受住严格审查。

作为一名编译器编写者,我对 C# 中的类型的看法如下:类型是可以应用于表达式分类。如果存在证明根据 C# 规则如何将表达式合法地分类为该类型,则表达式被分类为特定类型。

例如,假设我们正在尝试计算表达式“1 + 2.3”的类型。我们首先计算表达式“1”的类型。 C# 的规则告诉我们;该形式的表达式始终被归类为 int。我们计算出表达式“2.3”的类型。同样,C# 的规则告诉我们,这种形式的表达式被归类为“double”。整个表达式的类型是什么? C# 的规则告诉我们,“int”和“double”的和被归类为“double”。所以这个表达式的类型是“double”。

这就是编译器在执行类型分析时所做的事情:它构建特定表达式可以以特定方式合法分类的证明,或者,如果程序错误,它会告诉您为什么它无法构建证明。

但在这个层面上,所有类型都只是一种分类。您可以对任何域执行相同的操作。您说在正整数域中,某些数字被归类为“奇数”,某些数字被归类为“偶数”。某些数字被分类为“素数”和“合数”。如果您想对一个数字(例如“123”)进行分类,那么您可以编写一个证明来表明“123”既被分类为“奇数”又被分类为“合数”。

你可以编出任何你想要的分类,你知道你刚刚做了什么吗?你刚刚做了一个类型。您可以将数字分类为“两个素数之和”和“不是两个素数之和”,以及“大于四”和“不大于四”。然后你可以将它们组合在一起形成“大于四的偶数而不是两个奇素数之和”之类的类型。很容易确定任何特定整数是否是该类型的成员;到目前为止,我们尝试过的所有整数都被确定为是该类型的成员。目前尚不清楚该类型是否有任何成员;仅仅因为您可以想出一个类型并不意味着您知道该类型的大小!

类型系统可以允许任何可能的分类方案。我们可以编写 C#,使“odd”、“even”、“prime”和“composite”成为“int”的子类型。我们可以编写 C#,以便您可以写下的整数的任何属性都是 int 的子类型!我们不这样做是因为这样的类型系统给编译器带来了巨大的负担;使用此类类型系统的编译器非常复杂、非常慢,并且可能会陷入必须解决不可能的问题的情况。 CLR 和 C# 的设计者构建了我们拥有的类型系统,以便编译器(通常)可以非常快速地将表达式分类为类型。

As others have noted, the C# specification does not formally define "type". The C# spec does not attempt to be either a formal mathematical description of the language semantics or a tutorial for beginner programmers; you are expected to know what words like "type" and "value" and so on mean before you start reading the specification.

There are many possible definitions of "type", at varying levels of precision. For example, the ECMAScript specification somewhat informally defines a type as "a set of values", but since ECMAScript only has nine possible types, it does not need to have a strict, well-founded definition.

Another answer says that a type consists of a set of values, a set of rules for operating on those values, and a name. This is a very common working definition of a type, but it runs into problems when you try to think about it more formally. What is the name of an anonymous type? Is double*[][] the name of the type "jagged two dimensional array of pointers to double"? Does that type even have a name? Are List<int> and List<System.Int32> two different names for the same type? Does any set of values form a type? Are types themselves values? What is the type of a type? And so on. It's a good working definition but it doesn't quite hold up under scrutiny.

As a compiler writer, the way I think about types in C# is as follows: a type is a classification that can be applied to an expression. An expression is classified as being of a particular type if a proof exists that shows how the expression may be legally classified as that type, according to the rules of C#.

For example, suppose we are attempting to work out the type of the expression "1 + 2.3". We begin by working out the type of the expression "1". The rules of C# give us that; an expression of that form is always classified as an int. We work out the type of the expression "2.3". Again, the rules of C# tell us that an expression of this form is classified as "double". What is the type of the whole expression? The rules of C# tell us that the sum of an "int" and a "double" is classified as a "double". So the type of this expression is "double".

That's what the compiler does when it performs type analysis: it constructs proofs that particular expressions can legally be classified in particular ways, or, if the program is erroneous, it tells you why it was unable to construct a proof.

But all a type is, at this level, is simply a classification. You can do the same thing with any domain. You say that in the domain of positive integers, certain numbers are classified as "odd" and certain numbers are classified as "even". Certain numbers are classified as "prime" and "composite". If you want to classify a number, say, "123", then you might write a proof that shows that "123" is classified as both "odd" and "composite".

You can make up any classification you want, and you know what you just did? You just made a type. You can classify numbers into "the sum of two primes" and "not the sum of two primes", and "greater than four" and "not greater than four". And then you can combine them together into types like "even integers that are greater than four and not the sum of two odd primes". It is easy to determine if any particular integer is a member of this type; so far all integers that we've tried have been determined to not be members of that type. It is at this time unknown whether that type has any members or not; just because you can come up with a type does not mean that you know the size of the type!

A type system can allow any possible classification scheme. We could write C# so that "odd" and "even" and "prime" and "composite" were subtypes of "int". We could write C# so that any property of integers that you can write down is a subtype of int! We do not do so because such type systems put an enormous burden upon the compiler; compilers that work with such type systems are very complicated, very slow, and can get into situations where they have to solve impossible problems. The designers of the CLR and C# built the type system that we have such that the compiler can (usually) be extremely fast in classifying expressions into types.

梦里°也失望 2024-12-02 00:41:39

不,互换使用“类型”和“类”是不正确的。

类型可以是以下任何一种:

  • 接口
  • 委托类型 枚举类型
  • 结构
  • 指针类型
  • 数组类型(也是一个类)

据我所知,C# 规范并没有真正“定义” “类型”一词。甚至早在介绍中就谈到了 C# 具有“统一类型系统”,但没有定义什么是类型。

我怀疑对于大多数开发人员来说,给出“类型”一词的含义的示例比尝试准确定义该词更简单且更具沟通性。

No, it's not correct to use "type" and "class" interchangably.

A type can be any of:

  • A class
  • An interface
  • A delegate type
  • An enum type
  • A struct
  • A pointer type
  • An array type (which is also a class)

As far as I can see, the C# spec doesn't really "define" the word "type". Even as early as the introduction it talks about C# having a "unified type system" without defining what a type is.

I suspect that for most developers, giving examples of what you mean by the word "type" is simpler and more communicative than trying to define the word accurately.

扬花落满肩 2024-12-02 00:41:39

您不应该互换使用类型和类。

这是一本关于类型的有趣读物。


请注意,类型包括 结构接口代理指针类型enum 构造以及 课程

You shouldn't use type and class interchangeably.

This is an interesting read regarding types.


Note that types include structs, interfaces, delegates, pointer types and enum constructs as well as classes.

不弃不离 2024-12-02 00:41:39

这里是...

类型是任何可以实例化的东西(我的定义,而不是 MSFT 的定义)。

接口可以被实例化,因为必须有另一个类型实现该接口。

匿名类型是没有类定义但可以实例化的类型。

任何类型都可以从另一个(非匿名)类型继承。然后我们将该其他类型称为基本类型。

编辑:正如 Jon Skeet 所说,静态类无法实例化,所以我的定义是平淡的。也许它应该是“任何可以实例化的东西,或者静态类”......

Here goes...

A type is anything that can be instantiated (my definition, not MSFT's).

An interface can be instantiated in the sense that there must be another type that implements the interface.

Anonymous types are types for which there is no class definition, but they can be instantiated.

Any type may inherit from another (non-anonymous) type. We then call that other type the base type.

EDIT: As Jon Skeet remarked, static classes cannot be instantiated, so my definition falls flat. Maybe it should be "anything that can be instantiated, OR a static class"...

不知在何时 2024-12-02 00:41:39

我能想出的最好的日常答案是:

类型是值的集合、对这些值的操作的集合以及使其唯一的名称。

如果您正在从计算机科学的角度寻找关于类型本质上是什么的深入答案,我认为您不会在 C# 文档中找到它。类型是我们大多数人在日常生活中成功使用的东西,而不必确定它们的确切定义。但如果你真的想知道,我认为你会在编程理论和语言社区(特别是 ML 和 Haskell)中找到更好的答案,那里的人们非常小心地精确地形式化他们所谈论的内容。

类型系统的 Wikipedia 条目 是一个很好的起点。

The best workaday answer I can come up with is:

A type is a collection of values, a collection of operations on those values, and a name to make it unique.

If you are looking for a deep answer about what a type fundamentally is from a computer science perspective, I don't think you'll find it in the C# documentation. Types are something that most of us use successfully in our daily lives without having to pin down their exact definition. But if you really want to know I think you'll find better answers in the programming theory and languages community (specifically, ML and Haskell) where people take great care to precisely formalize what they are talking about.

The Wikipedia entry for Type System is a good place to start.

冬天旳寂寞 2024-12-02 00:41:39

类和类型不能互换。类始终是类型,但类型并不总是类。例如,结构和接口是类型,但不是类。

Classes and types are not interchangeble. A class is always a type, but a type is not always a class. Structs and interfaces, for example, are type, but not classes.

﹉夏雨初晴づ 2024-12-02 00:41:39

“类型”是“数据类型”的缩写。您使用的任何变量都有某种数据类型,无论是所谓的“简单类型”(如 int 或 long )还是“复杂类型”,如 struct 或

虽然类始终是类型,但并非每种类型都是类。例如,int 是一种类型,但不是类。 C# 的特点是,每个简单类型都有一个代表类 (bool => Boolean)。

您不能使用 class 和 type可以互换,但如果你这样做的话,大部分都会被理解:-)

"type" is simply short for "data type". Any variable you use has some data type, be it a so called "simple type" (like int or long) or a "complex type" like a struct or a class.

While a class is always a type, not every type is a class. For example, int is a type but not a class. C# has the feature that for every simple type there's also a representing class (bool => Boolean).

You can not use class and type interchangeably, but it will mostly be understood if you do :-)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文