Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

this statement after contructor's arguments [duplicate]

Tags:

c#

constructor

I saw this code block when i was trying build something with APN. Could someone explain me what does "this" statements do there ?

public ApplePushService(IPushChannelFactory pushChannelFactory, ApplePushChannelSettings channelSettings)
        : this(pushChannelFactory, channelSettings, default(IPushServiceSettings))

Is it like default values of those arguments ?

like image 776
paroxit Avatar asked May 31 '13 15:05

paroxit


People also ask

Is it mandatory to pass the object reference as an argument in copy constructor?

It is necessary to pass object as reference and not by value because if you pass it by value its copy is constructed using the copy constructor. This means the copy constructor would call itself to make copy. This process will go on until the compiler runs out of memory.

What does this do in a constructor?

Within an instance method or a constructor, this is a reference to the current object — the object whose method or constructor is being called. You can refer to any member of the current object from within an instance method or a constructor by using this .

Why do you need to use a reference in the argument to the copy constructor?

When we create our own copy constructor, we pass an object by reference and we generally pass it as a const reference. One reason for passing const reference is, we should use const in C++ wherever possible so that objects are not accidentally modified.


2 Answers

this calls the overloaded constructor for the ApplePushService class with the specified parameters.

For example

// Set a default value for arg2 without having to call that constructor
public class A(int arg1) : this(arg1, 1) 
{
}

public class A(int arg1, int arg2)
{
}

This lets you call one constructor that can invoke another.

like image 131
Brandon Avatar answered Oct 03 '22 08:10

Brandon


Sure - that chains one constructor onto another. There are two forms - this to chain onto another constructor in the same class, and base to chain to another constructor in the base class. The body of the constructor you're chaining to executes, and then your constructor body executes. (Of course, the other constructor may chain onto another one first.)

If you don't specify anything, it automatically chains to a parameterless constructor in the base class. So:

public Foo(int x)
{
    // Presumably use x here
}

is equivalent to

public Foo(int x) : base()
{
    // Presumably use x here
}

Note that instance variable initializers are executed before the other constructor is called.

Surprisingly, the C# compiler doesn't detect if you end up with mutual recursion - so this code is valid, but will end up with a stack overflow:

public class Broken
{
    public Broken() : this("Whoops")
    {
    }

    public Broken(string error) : this()
    {
    }
}

(It does prevent you chaining onto the exact same constructor signature, however.)

For more details, see my article on constructor chaining.

like image 34
Jon Skeet Avatar answered Oct 03 '22 08:10

Jon Skeet