为什么新的关键字在字符串类中不是必需的 [英] Why new Keyword is not mandatory with string class

查看:50
本文介绍了为什么新的关键字在字符串类中不是必需的的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

关于 string 类的小困惑
创建任何非静态类的实例都需要使用 new 关键字.
为什么在 string 类中不是强制性的.喜欢

little confusion about string class
Creating instance of any non static class requires use of new keyword.
why it is not mandatory with string class. Like

string str = "Hello";



原谅我,如果是愚蠢的问题

谢谢

:)



forgive me if it is stupied question

Thanks

:)

推荐答案

因为创建"Hello"有效地构造了一个新对象(它并不完全一样,就好像您在两个不同的地方使用"Hello"一样)它们都引用相同的string的地方.)
如果您考虑一下,string并不是唯一以这种方式运行的类:int, double, bool等,也不需要new关键字(谢谢!)

我怀疑,真正的原因是它会使程序难以阅读!
Because the creation of the "Hello" effectively constructs a new object (it doesn''t quite, as if you use "Hello" in two different places they both refer to the same string).
If you think about it, string is not the only class that behaves in this way: int, double, bool, etc. do not require the new keyword either (thank goodness!)

The real reason, I suspect, is that it would make programs difficult to read!


这篇关于为什么新的关键字在字符串类中不是必需的的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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