What is the return type of a constructor in C#?

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

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.

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:

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:

  • If T is a class-type:

    • 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.

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

    • 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.

  • […]

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

Leave a Comment

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)