"基本类型"与"内置值类型" [英] "Primitive types" versus "built-in value types"

查看:193
本文介绍了"基本类型"与"内置值类型"的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在使用Array.SetValue(Int32)方法最近采访在C#中的异常。唯一的例外是:

I recently caught an exception in C# while using the Array.SetValue(Int32) method. The exception was:

无法从源类型扩大至目标类型要么是因为源
型是不是基本类型或转换不能
来完成的。

Cannot widen from source type to target type either because the source type is a not a primitive type or the conversion cannot be accomplished.

字原始的提法让我感到惊讶了一下,因为我认为趋势是指这些类型内置类型,也是术语基本类型是一个非正式的术语。什么是原始类型和内置值类型之间的区别?我没有找到原始类型在C#语言规范的定义。

The reference to the word "primitive" surprised me a bit because I thought the tendency was to refer to these types as built-in types, also that the term "primitive type" was an informal term. What is the difference between "primitive" types and "built-in value types"? I don't find a definition of primitive types in the C# language specification.

推荐答案

原始类型在C#语言定义规范。他们是在.NET本身,而不是定义,对于基本类型的最好的参考就是直视的 Type.IsPrimitive MSDN上。具体来说,说明部分列出了基本类型可用。

Primitive Types are not defined in the C# Language Specification. They are instead defined in .NET itself, and the best reference for primitive types is to look straight at Type.IsPrimitive on MSDN. Specifically, the Remarks section lists the primitive types that are available.

所以,我们已经有了一个完整的参考这里,这些都是在的 CLI规格(见I.8.2.2):

So that we've got a complete reference here, these are the primitive types defined in the CLI Spec (Section I.8.2.2):

  • Boolean
  • Byte
  • SByte
  • Int16
  • UInt16
  • Int32
  • UInt32
  • Int64
  • UInt64
  • IntPtr
  • UIntPtr
  • Char
  • Double
  • Single

与普遍看法相反,仅仅因为一个类型都有一个相应的关键字是否不可以使它一个基本类型,最好的例子可能是字符串

Contrary to popular belief, just because a type has a corresponding keyword does not make it a primitive type, the best example is probably string.

可能值类型,在另一方面,可能还是不还的原语。有很多值类型除了那些在CLI规格定义的内置到.NET Framework的,但他们没有归类为原语。一个很好的例子是 的DateTime ,这是一个由.NET Framework提供结构,由定义可以认为它是一个内置的值类型。你可以阅读更多有关值类型(当然,这将覆盖内置的人也)这里

Value types, on the other hand, may or may not be primitives also. There are lots of value types "built-in" to the .NET Framework in addition to those defined in the CLI Spec, but they are not classed as primitives. A good example is DateTime, which is a struct provided by the .NET Framework, by that definition it could be considered a "built-in value type". You can read more about value types (which will of course cover the built-in ones also) here.

这篇关于"基本类型"与"内置值类型"的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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