操作员在C#中对C ++新 [英] Operator new in C# vs C++

查看:124
本文介绍了操作员在C#中对C ++新的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

从C即将++,我很困惑,在C#中使用关键字。

Coming from C++, I am confused as to the use of the new keyword in C#.

我理解,它不喜欢工作,C ++的在这个意义上,您不必手动控制对象的生命周期,如C#有垃圾回收。

I understand that it doesn't work like C++'s new in the sense that you do not have to manually control the lifetime of the object, as C# has garbage collection.

然而,在其他国家的人民阅读C#代码时,我注意到像代码片段1岂不是更容易避免使用干脆,就像在代码片段2?

However, when reading over other peoples C# code I notice statements like in code snippet 1. Wouldn't it be far easier to avoid the use of new altogether, like in code snippet 2?

片段1

Foo fooInstance = new Foo();



片段2

Foo fooInstance;

我的问题是,什么是片断1之间的差异和片段2,何必我喜欢一个比其他?

推荐答案

我们必须三种情况之间有(1)局部变量区分,( 2)(非静态)的内部结构的字段,和(3)字段内的类

We must discriminate between three cases, (1) local variables, (2) (non-static) fields inside structs, and (3) fields inside classes.

有关的局部变量,即在一个方法中声明的变量(或一个构造函数,或属性/索引器/事件访问内部),两者是不等同的:

For local variables, that is variables declared inside a method (or inside a constructor, or property/indexer/event accessor), the two are not equivalent:

class C
{
  void M()
  {
    Foo fooInstance = new Foo();
    // the variable is "definitely assigned" and can be read (copied, passed etc)
    // consider using the 'var' keyword above!
  }
}

class C
{
  void M()
  {
    Foo fooInstance;
    // the variable is not "definitely assigned", you cannot acquire its value
    // it needs to be assigned later (or can be used as 'out' parameter)
  }
}






有关实例字段(非静态在结构,只有片段之一是允许的字段):


For instance fields (non-static fields) inside a struct, only one of the "snippets" is allowed:

struct S
{
  Foo fooInstance = new Foo(); // compile-time error! cannot initialize here
}

struct S
{
  Foo fooInstance; // OK, access level is 'private' when nothing is specified
}






对于一个类中的字段(和静态 struct的字段),这种情况要看是否本身是引用类型()或值类型(结构枚举)。默认值为默认(美孚)引用类型是,这并不是指任何引用。默认值为默认(美孚)或值类型是,所有领域都有其默认值类型的实例。对于值类型(结构和枚举),新富()(无参数)和默认(美孚)是同样的事情。因此:


For fields inside a class (and static fields of a struct), the situation depends on whether Foo itself is a reference type (class) or a value type (struct or enum). The default value default(Foo) of a reference type is null, the reference that does not refer anything. The default value default(Foo) or a value type is the "instance" of the type where all fields have their default values. For value types (struct and enum), new Foo() (no arguments) and default(Foo) is the same thing. Therefore:

class C
{
  Foo fooInstance = new Foo(); // OK, private
}

class C
{
  Foo fooInstance; // OK, private
  // equivalent to 'Foo fooInstance = null;' if 'Foo' is a reference type (class, interface, delegate, array)
  // equivalent to 'Foo fooInstance = new Foo();' is 'Foo' is a value type (struct, enum)
}



< HR>

应该注意,如果是引用类型,表达新富() 只允许在类型实际上有一个构造函数0的参数,如果该构造函数可以访问。


It should be noted that if Foo is a reference type, the expression new Foo() is only allowed if the type actually has a constructor that takes 0 arguments, and if that constructor is accessible.

在(1)我们忽视无聊的情况下是没有实例字段的结构。

In (1) we disregarded the silly case where Foo is a struct with no instance fields.

这篇关于操作员在C#中对C ++新的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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