为String基本类型? [英] Is String a primitive type?

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

问题描述

我很好奇的字符串和原始类型。文章如说字符串是基本类型。然而第二篇文章 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.

在接受回答这个<一个href="http://stackoverflow.com/questions/636932/in-c-why-is-string-a-reference-type-that-behaves-like-a-value-type">question应该给您的详细信息。

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

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

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