通过使用和不使用'new'运算符在C#中声明对象有什么区别? [英] What is the difference between declaring objects in C# by using and by not using the ‘new’ operator?

查看:151
本文介绍了通过使用和不使用'new'运算符在C#中声明对象有什么区别?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在C#中,可以使用new运算符声明对象,也可以不使用new运算符。除语法之外的方法有什么区别吗?



例如 -



In C# objects can be declared by using the new operator and also without using the new operator. Is there any difference between the methods other than there syntaxes?

for example-

string s = "abc";











and

char[] arr = { 'a', 'b', 'c' }; 
string s = new string(arr);





PS-我使用的是Visual Studio 2015



我的尝试:



...... .................................................. .......................



P.S- I am using Visual Studio 2015

What I have tried:

...............................................................................

推荐答案

几乎所有X之间有什么区别和Y,这不是一个有效的问题(第一个案例是一个陈述;另一个案例是几个陈述:-)),但从根本上说,它确实有一定道理。



答案是:根本没有功能差异。结果完全一样。



这是后台: System.String ,这个类型也可以通过它的C#别名使用 string (有什么区别?完全没有功能差异,只是代码的不同文本:-))是引用类型。但它完美地模仿了值类型语义,这不是一个微不足道的特征,非常重要。从根本上说,所有引用类型都是通过构造函数实例化的。只有一个 System.String 构造函数;你展示它的用途。至于将字符串文字abc赋值给字符串变量,可以将其视为重载的赋值运算符



但是,如果将相同的东西分配给另一个字符串对象,事情会变得复杂得多。如果它不是字符串,则它将是对同一对象的两个不同引用。在字符串的情况下,它的方式更复杂。太了解发生了什么,你必须要理解 string intern pool 这样的高级功能。请参阅:

字符串实习 - 维基百科,免费百科全书

String.Intern方法(字符串)(系统)

String.IsInterned方法(字符串)(系统)



如果您觉得这有点过头(但更好地学习和理解它) ,只是为了有一个想法),不要担心:在实践中,没有什么可担心的。只有你应该清楚地理解更基本的事实:字符串是严格不可变的



-SA
Like nearly all "what is the difference between X and Y", this is not a valid question (first case is one statement; and another case is a couple of statements :-)), but, fundamentally, it does have some sense.

The answer is: no functional difference at all. The result is exactly the same.

Here is the background: System.String, the type which can also be used via its C# alias "string" (what's the difference? no functional difference at all, just different text of the code :-)) is a reference type. But it perfectly mimics the value-type semantic, which is not a trivial feature, very non-trivial. All reference types, fundamentally, are instantiated via the constructor. There is only one System.String constructor; you show its use. As to the assignment of the string literal "abc" to the string variable, you can consider it as an overloaded assignment operator.

But things will look much more complicated if you assign the same thing to another string object. If it wasn't string, it would be two different references to the same object. In case of string, it's way, way more sophisticated. Too understand what's going on, you have to understand such advanced thing as string intern pool. Please see:
String interning — Wikipedia, the free encyclopedia,
String.Intern Method (String) (System),
String.IsInterned Method (String) (System).

If you feel that this is a bit over your head (but better learn and understand it, just to have an idea), don't worry: in practice, there is nothing much to worry about. Only you should clearly understand more fundamental fact: strings are strictly immutable.

—SA


大多数(但不是全部)引用类型需要 new 运算符才能创建类的实例。例外是字符串,它不需要它 - 部分是因为它会使代码变得丑陋,部分是因为它们是不可变的。

所有其他引用类型都需要 new 为了在Heap上分配空间并返回对它的引用,以便可以使用类实例。



另一方面,值类型不需要 new - 只有在创建需要构造函数的值类型时才需要它(比如Point需要两个值才能生成正确的位置)。对于其他值类型,它主要是隐含的。例如,你可以说

Most (but not all) reference types need the new operator in order to create an instance of the class. The exception is strings, which don't require it - partly because it would make the code ugly, and partly because they are immutable.
All other reference types require new in order to allocate the space on the Heap and return a reference to it so the class instance can be used.

Value types on the other hand don't need new - it's only required if you create a value type that needs a constructor (like Point which needs two values to make a "proper" location). For the other value types, it is mostly implied. For example, you can say
int i = new int();
i = 6;

但它更短,更易于理解

But it's shorter an more readable to say

int i = 6;


这篇关于通过使用和不使用'new'运算符在C#中声明对象有什么区别?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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