Home > Blockchain >  What is the difference between both a class (instance) constructor and a struct (instance) construct
What is the difference between both a class (instance) constructor and a struct (instance) construct

Time:01-09

Recently I have been brushing up on some early C# material. Specifically in relevance to an instance constructor.

What is the difference between both a class (instance) constructor and a struct (instance) constructor in terms of what they return (if anything is returned that is)?

I have heard (though I have not seen/read the following in any C# documentation) that:

  • a class constructor when invoked creates an instance of the class type and returns a reference to the instance.
  • While a struct constructor when invoked creates an instance of the struct type and returns the instance itself (a reference to the instance is NOT returned).

Then again, I am not sure if that is correct since a constructor (class constructor or struct constructor) does not have (and cannot have) a return type in its signature...

    public class Program
    {
        public static void Main()
        {
            Person person1 = new Person("Tom");

            Dog dog1 = new Dog("Milo");

        }
    }

    public class Person
    {
        private string _name;

        public Person(string name)
        {
            _name = name;
        }
    }

    public struct Dog
    {
        private string _name;

        public Dog(string name)
        {
            _name = name;
        }
    }

In the above code, I invoke/call the explicitly defined parameterized (instance) constructor of class type Person and struct type Dog.

CodePudding user response:

Constructors themselves do not return anything. It would be better to think of them as returning void. As the name implies, a constructors job is to initialize an object that has been allocated in memory. Now this might be confusing because the new operator does invoke the constructor, so you might be thinking that the constructor has to return something. However, I think it is better to think of the new operator as doing two things, 1) Allocates a storage location in memory, 2) Calls the constructor to initialize this storage location.

A better way to look at the constructor is the following:

void PersonConstructor(this, string name);

In other words, a constructor does not return anything and gets passed a reference to the object instance (this) as its first argument so it can initialize its members. Now after this happens, the new operator returns the initialized memory and stores it in the variable.

Here is how I would reword what you wrote:

  • The new operator when invoked with a class type allocates an instance
    of the class type in memory, invokes the class type constructor to initialize its members, and returns a reference to the instance.

  • The new operator when invoked with a struct type creates allocates instance of the struct type, invokes the struct type constructor to initialize its members, and returns the instance itself.

CodePudding user response:

Normally, you don't have to worry, if you understand the difference between a class and a struct. It is technically correct, that a constructor of a class returns the reference to the newly generated instance. You can't declare that - it's implicit. A variable taking a class is called a reference, because it points to the instance of the class. If you assign it to another variable, it still points to the same instance. For a struct, this is different. An instance of a struct is stored in the variable that it contains. Therefore the constructor of a struct returns the instance itself, which is then copied into the target variable.

Again, this is happening behind the scenes and as long as you remember that class variables observe reference semantics and struct variable observe value semantics, you should be fine.

  •  Tags:  
  • Related