String 是原始类型吗? [英] Is String a primitive type?

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

问题描述

我对字符串和原始类型很好奇.this 之类的文章说字符串是原始类型.但是第二篇文章在 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.

然而,当我运行第二篇文章中提供的代码时,它显示String is not Primitive type.

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

有人可以指导我吗?

推荐答案

两篇文章都说字符串不是是原始类型.事实并非如此.

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

如果您编译并运行第二篇文章中的示例代码,它将打印:

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

string 不是原始类型.

string is not a primitive type.

我认为对此的混淆在于,创建新字符串的语法类似于创建值类型.

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.

对此问题 应该为您提供详细信息.

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

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

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