在.Net中,为什么这些都被称为类型?

时间:2021-08-26 16:05:57

In his book Pro C# 2008 and the .NET 3.5 Platform, Andrew Toelsen says, "In the world of .NET, type is simply a general term used to refer to a member from the set {class, interface, structure, enumeration, delegate}.

在他的书Pro C#2008和.NET 3.5平台中,Andrew Toelsen说,“在.NET世界中,类型只是一个通用术语,用于指代集合中的成员{class,interface,structure,enumeration,delegate }。

What do these all have in common that they would be classed together as types? What would be a formal definition of type that would cover all of these under one umbrella term like that???

这些都有什么共同点,他们将被归类为类型?什么是正式的类型定义,涵盖所有这些类似的伞形术语?

3 个解决方案

#1


3  

A type is anything that can describe a variable. For insstance, I can have a variable 'a' of type 'int' (int is a struct). I can also have 'b' of type 'MyClass' (MyClass is a class).

类型是可以描述变量的任何东西。对于insstance,我可以有一个'int'类型的变量'a'(int是一个struct)。我也可以使用'MyClass'类型的'b'(MyClass是一个类)。

Basically, all type are used (more or less) in this way (to declare a variable):

基本上,所有类型都以这种方式使用(或多或少)(声明变量):

TypeName variableName;

The newly declared variable automatically has some sort of internal structure, for example, if it is of a class type, it can have methods and properties. If is of a delegate type, then it is designed to hold a reference to a method.

新声明的变量自动具有某种内部结构,例如,如果它是类类型,则它可以具有方法和属性。如果是委托类型,则它旨在保存对方法的引用。

Basically, a type is anything that you use to declare a variable. A type gives the variable its meaning, as well as restricts the variable to being used in a way which is suited to its existence.

基本上,类型是用于声明变量的任何类型。类型赋予变量其含义,并限制变量以适合其存在的方式使用。

#2


2  

Short answer: you can declare variables of these - a type defines the "kind of" or "structure" of a variable.

简短回答:您可以声明这些变量 - 类型定义变量的“种类”或“结构”。

Technically, when you declare a variable of a certain type (for example, a class, an integral type etc.), you can only assign values of this specific type (or a compatible one, or one where an implicit type conversion exists) to the variable. In a strongly typed language like C#, in most cases the compiler will tell you if you break that rule.

从技术上讲,当您声明某个类型的变量(例如,类,整数类型等)时,您只能分配此特定类型的值(或兼容的或存在隐式类型转换的值)变量。在像C#这样的强类型语言中,在大多数情况下,编译器会告诉您是否违反了该规则。

#3


0  

They're all types because they represent all the possible types of a variable. Simple! :)

它们都是类型,因为它们代表变量的所有可能类型。简单! :)

#1


3  

A type is anything that can describe a variable. For insstance, I can have a variable 'a' of type 'int' (int is a struct). I can also have 'b' of type 'MyClass' (MyClass is a class).

类型是可以描述变量的任何东西。对于insstance,我可以有一个'int'类型的变量'a'(int是一个struct)。我也可以使用'MyClass'类型的'b'(MyClass是一个类)。

Basically, all type are used (more or less) in this way (to declare a variable):

基本上,所有类型都以这种方式使用(或多或少)(声明变量):

TypeName variableName;

The newly declared variable automatically has some sort of internal structure, for example, if it is of a class type, it can have methods and properties. If is of a delegate type, then it is designed to hold a reference to a method.

新声明的变量自动具有某种内部结构,例如,如果它是类类型,则它可以具有方法和属性。如果是委托类型,则它旨在保存对方法的引用。

Basically, a type is anything that you use to declare a variable. A type gives the variable its meaning, as well as restricts the variable to being used in a way which is suited to its existence.

基本上,类型是用于声明变量的任何类型。类型赋予变量其含义,并限制变量以适合其存在的方式使用。

#2


2  

Short answer: you can declare variables of these - a type defines the "kind of" or "structure" of a variable.

简短回答:您可以声明这些变量 - 类型定义变量的“种类”或“结构”。

Technically, when you declare a variable of a certain type (for example, a class, an integral type etc.), you can only assign values of this specific type (or a compatible one, or one where an implicit type conversion exists) to the variable. In a strongly typed language like C#, in most cases the compiler will tell you if you break that rule.

从技术上讲,当您声明某个类型的变量(例如,类,整数类型等)时,您只能分配此特定类型的值(或兼容的或存在隐式类型转换的值)变量。在像C#这样的强类型语言中,在大多数情况下,编译器会告诉您是否违反了该规则。

#3


0  

They're all types because they represent all the possible types of a variable. Simple! :)

它们都是类型,因为它们代表变量的所有可能类型。简单! :)