为什么C#中的原始类型具有自己的操作? [英] Why do primitive types in C# have their own operations?
问题描述
几天前,我决定开始学习C#.于是,我有了书,开始阅读和练习代码.当我看到C#中的 string
被认为是原始类型时,我感到很惊讶.
A few days ago, I decided to start learning C#. So, I got a book and started reading and practicing with code. I was surprised when I saw that string
in C# is considered a primitive type.
但是当我看到 string
以及C#中的所有其他原始类型都具有操作时,我感到非常惊讶.我是Java开发人员,我的理解是原始数据类型没有操作,只有类有.但是在C#中,以下内容是有效的:
But I was more surprised when I saw that string
, as well as all the other primitive types in C# have operations. I'm a Java developer and my understanding was that primitive data types don't have operations, only classes have. But in C#, the following is valid:
string name = "alex";
Console.WriteLine(name.ToUpper());
这怎么可能?他们真的是原始人吗?我在这里想念什么?
How is this possible? Are they really primitives? What am I missing here?
推荐答案
string
不是C#中的原始类型.它是C#中两个预定义(即语言规范的一部分)引用类型之一(另一个是 object
).C#中的基本类型为 Boolean
( bool
), Byte
( byte
), SByte 代码>(
sbyte
), Int16
( short
), UInt16
, Int32
( int
),UInt32( uint
), Int64
( long
),UInt64( ulong
), IntPtr
, UIntPtr
, Char
( char
), Double
( double
)和 Single
( single
).注意,规范指出也可以使用结构和运算符重载以在C#语言中实现新的原始"类型.但是如果 MyStruct
是用户定义的 struct
,则 typeof(MyStruct).IsPrimitive
为 false
.
string
is not a primitive type in C#. It's one of two predefined (i.e., part of the language specification) reference types in C# (the other being object
). The primitive types in C# are Boolean
(bool
), Byte
(byte
), SByte
(sbyte
), Int16
(short
), UInt16
, Int32
(int
), UInt32 (uint
), Int64
(long
), UInt64 (ulong
), IntPtr
, UIntPtr
, Char
(char
), Double
(double
), and Single
(single
).
Note that the specification states "it is also possible to use structs and operator
overloading to implement new "primitive" types in the C# language" but that typeof(MyStruct).IsPrimitive
is false
if MyStruct
is a user-defined struct
.
我有一本书,开始阅读和练习代码.当我看到C#中的字符串被认为是原始类型时,我感到很惊讶.
I got a book and started reading and practicing with code. I was surprised when I saw that string in C# is considered a primitive type.
这本书怎么说?哪本书?
The book said this? Which book?
我是Java开发人员,我的理解是原始数据类型没有操作,只有类有操作.
I'm a Java developer and my understanding was that primitive data types don't have operations, only classes have.
简单而又简单地讲,C#和Java是不同的语言.在C#中,有一个 object
的概念,几乎所有东西都从该概念派生而来(是的,有一些例外,其中最重要的是接口).从 object
开始,有一个名为 ValueType
的派生类型. ValueType
的派生词是具有值语义的 struct
. object
的所有其他派生类都是引用类型.所有这些 object
都封装了数据和行为(即,它们可以具有方法).
Plainly and simply, C# and Java are different languages. In C# there is the notion of object
from which almost everything derives (yes, there are exceptions the most important of which is interfaces). From object
there is a derived type called ValueType
. Derivatives of ValueType
are struct
s which have value semantics. All other derivatives of object
are reference types. All of these object
s encapsulate data and behavior (i.e., they can have methods).
字符串名称="alex";
Console.WriteLine(name.ToUpper());
这怎么可能?
我不明白您对此代码段的困惑. name
是 string
的一个实例,该实例肯定是由字符串文字"alex"
分配的,我们正在调用该方法的重载之一 name
上的 String.ToUpper
.然后调用 Console.WriteLine
的重载,该重载接受 string
的实例.你甚至可以做到
I don't understand your confusion with this code snippet. name
is an instance of string
that is definitely assigned by the string literal "alex"
and we are invoking one of the overloads of the method String.ToUpper
on name
. Then the overload of Console.WriteLine
that accepts an instance of string
is invoked. You can even do this
Console.WriteLine("alex".ToUpper());
它们真的是原始的吗?
Are they really primitives?
不. string
不是原始的.
我在这里想念什么?
What am I missing here?
C#和Java是相关的,但是编程语言却大不相同.
That C# and Java are related but very different programming languages.
这篇关于为什么C#中的原始类型具有自己的操作?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!