什么是术语的最佳定义;类型"特别是在C#语言? [英] what is the Best definition of the term "Type" specially in c# language?

查看:157
本文介绍了什么是术语的最佳定义;类型"特别是在C#语言?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这可能是一个很简单的问题,但我有一个关于在类型定义的混乱。

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.

是不是同一个类定义!? 类型==类!?

如果是这样,我们知道,在C#中其他(参考)类型不是接口代表

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.

推荐答案

正如其他人所指出的那样,C#不规范没有正式定义的类型。 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.

有型的许多可能的定义在精密不同级别。举例来说,ECMAScript规范有些非正式地定义了一个类型为一套价值,但由于ECMAScript中唯一有九个可能的类型,它并不需要有一个严格的,有根有据的定义。

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.

另一个回答说,一个类型由一组值,对这些值运行的一套规则,和名称。这是一种非常常见的工作定义,但它在尝试更正式去想它运行到的问题。什么是匿名类型的名称?为双* [] [] 中的名称的类型指针翻番的锯齿形二维数组的?这是否型甚至有名字吗?是列表< INT> 列表< System.Int32> 两个不同的名字为同一类型?难道的任何的设定值形成一种?是类型自身价值?这是一种类型的类型?等等。 ,这是一个很好的工作定义,但它并没有完全受到审查撑起

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.

作为一个编译器作者,顺便我想在C#中的类型如下:类型是的分类的可以应用到的表达式的。一个表达式被分类为特定类型的,如果一个<青霉>证明的存在,显示了如何表达可以合法列为该类型,根据C#的规则

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#.

例如,假设我们正在试图制定出表达1 + 2.3的类型。我们首先制定出表达式的类型为1。 C#的规则给我们;这种形式的表达总是被归类为一个int。我们制定出表达2.3的类型。此外,C#的规则告诉我们,这种形式的表达被归类为双。什么是整个表达式的类型? C#的规则告诉我们,一个Int和双的总和被列为双。所以这个表达式的类型是双规

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.

但是,所有类型的,在这个层面上,是一个简单的分类。你可以做同样的事情与任何域。你说,在正整数的领域,某些数字被列为奇有些数字被列为甚至。某些号码被列为素和复合。如果你想一些分类,比如123,那么你可能会写一个证明显示,123被列为双方奇和复合。

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!

一个类型的系统可以让任何可能的分类方案。我们可以写C#,使奇和偶和素和复合是的INT亚型。我们可以写C#这样的任何的,你可以写下来的整数属性是int的子类型!我们不这样做,因为这种类型的系统把在编译器一个巨大的负担;与这种类型的系统工作的编译器是非常复杂的,速度很慢,而且可以进入,他们要解决的问题是不可能的情况。 CLR和C#的设计者建,我们有这样的编译器可以(通常)在表达式分成类型是非常快的类型系统。

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.

这篇关于什么是术语的最佳定义;类型&QUOT;特别是在C#语言?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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