为String基本类型?类型、基本、String

2023-09-03 00:19:04 作者:十字路口

我很好奇的字符串和原始类型。文章如这说字符串是基本类型。然而第二篇文章 MSDN上没有列出的字符串作为基本类型。

I am curious about the string and primitive types. Article like this says string is primitive type. However second article on MSDN does not list string as primitive type.

然而,当我跑在第二篇文章中提供的code,它显示的字符串不是基本类型

However when I ran the code provided in second article, it displays String is not Primitive type.

任何一个可以指导我一下吗?

Can any one guide me on this?

推荐答案

这两篇文章说字符串不会基本类型。它不是。

Both articles say that string is NOT a primitive type. Which it is not.

如果你编译并运行从第二篇文章的例子code会打印:

If you compile and run the example code from the second article it would print:

字符串不是基本类型。

我觉得这个困惑是,创建一个新的字符串的语法类似于创造价值类型。

I think the confusion about this is, that the syntax of of creating a new string is similar to creating value types.

当定义一个值类型的所有这些都是相等的(在32位系统上是这样)

When defining a value type all of these are equal (on a 32 bit system anyway)

System.Int32 a = new System.Int32(5);
System.Int32 a = 5;
int a = 5;

就像这些创建引用类型的字符串时:

Just like these when creating a reference type string:

System.String s = new System.String(new char[]{'h', 'e', 'l', 'l', 'o'});
System.String s = "hello";
string s = "hello";

此外,我们可以,即使他们是引用类型按值比较字符串:

Also we can compare strings by value even though they are reference types:

s == "hello";//true

这仍然不能让字符串基本类型。

This still does not make string a primitive type.

在接受回答这个question应该给您的详细信息。

The accepted answer to this question should give you details on that.

 
精彩推荐
图片推荐