C#中构造函数的返回类型是什么? [英] What is the return type of a constructor in C#?

查看:27
本文介绍了C#中构造函数的返回类型是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在这个链接上针对 Java 提出了这个问题

I have asked this question for Java on this link

我在 java 中得到了一些答案.现在我想在 C# 中知道它.

I got some answers in java.Now i want to know it in C#.

正如我们所知,我们不必向 C# 构造函数添加任何返回类型.

As we know the we do not have to add any return type to a C# constructor.

class Sample{
  .....
  Sample(){
    ........
  }
}

在Objective C中,如果我们创建一个构造函数,它会返回一个指向它的类的指针.但我认为这不是强制性的.

In Objective C, if we create a constructor, it returns a pointer to its class. But it is not compulsory, I think.

AClass *anObject = [[AClass alloc] init];//init is the constructor with return type a pointer to AClass

同样的,构造函数是否转换为一个方法,该方法返回对其自身类的引用??

Similarly, Is the constructor converted to a method which return a reference to its own class??

像这样:

class Sample{
    .....
    Sample Sample(){
      ........

      return this;
    }
}

编译器是否将返回类型添加到构造函数的同一类的引用?构造函数发生了什么?有什么参考资料可以研究这个吗?

Does the compiler add a return type a reference to same class to constructor? What is happening to a constructor? Any reference to study this?

推荐答案

根据 C# 4.0 语言规范,第 1.6 节:

According to the C# 4.0 Language Specification, section 1.6:

类的实例是使用 new 运算符创建的,该运算符为新实例分配内存,调用构造函数来初始化实例,并返回对实例的引用.

Instances of classes are created using the new operator, which allocates memory for a new instance, invokes a constructor to initialize the instance, and returns a reference to the instance.

new 操作符负责分配内存,将新分配的对象的引用传递给构造函数,然后返回对实例的引用.这种机制也在 7.6.10.1 节中解释:

It is the new operator who is responsible of allocating memory, passing a reference of the newly allocated object to the constructor and then returning a reference to the instance. This mechanism is also explained in section 7.6.10.1:

表单的object-creation-expression的运行时处理new T(A),其中 Tclass-typestruct-typeA 是可选的argument-list,包括以下步骤:

The run-time processing of an object-creation-expression of the form new T(A), where T is class-type or a struct-type and A is an optional argument-list, consists of the following steps:

  • 如果 T 是一个 class-type:

  • 分配了一个 T 类的新实例.如果不够可用于分配新实例的内存,一个System.OutOfMemoryException 被抛出并且没有进一步的步骤执行.

  • A new instance of class T is allocated. If there is not enough memory available to allocate the new instance, a System.OutOfMemoryException is thrown and no further steps are executed.

新实例的所有字段都初始化为其默认值值(第 5.2 节).

All fields of the new instance are initialized to their default values (§5.2).

实例构造函数根据函数成员调用规则(第 7.5.4 节).参考新的分配的实例会自动传递给实例构造函数并且可以从该构造函数中以 this 的形式访问该实例.

The instance constructor is invoked according to the rules of function member invocation (§7.5.4). A reference to the newly allocated instance is automatically passed to the instance constructor and the instance can be accessed from within that constructor as this.

[…]

这意味着构造函数本身没有返回类型(void).

This would mean that the constructor per se has no return type (void).

这篇关于C#中构造函数的返回类型是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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