-2

Can somebody explain what means : this(123) in a constructor ?

public class MyObject
{      
    public MyObject(): this(123)
    {
    }
    ............
}
user2818430
  • 5,853
  • 21
  • 82
  • 148
  • 1
    do you perhaps have a `MyObject(int variable){ }` constructor? – default Oct 22 '13 at 19:23
  • 1
    You can read about constructors [here](http://msdn.microsoft.com/en-us/library/ms173115%28v=vs.90%29.aspx) – default Oct 22 '13 at 19:24
  • 1
    Why don't you step through the code with a debugger to see what it does? – Servy Oct 22 '13 at 19:24
  • 1
    Did you [search](http://stackoverflow.com/search?q=%5Bc%23%5D+constructor+this)? We already have some answers that might have worked for you. – crashmstr Oct 22 '13 at 19:24
  • I will guess that when I instantiate a new object MyObject, the int parameter is somehow optional but by default set to 123 (like you will make an optional parameter in a method: public void Method(int val =5)) ... – user2818430 Oct 22 '13 at 19:24
  • this means call of overloaded class constructor which take in one integer parameter. – Shad Oct 22 '13 at 19:24
  • Yep I have another constructor, so basically when I call new MyObject() then it will call my second constructor MyObject(int val) with the value 123 ... I got it! – user2818430 Oct 22 '13 at 19:27
  • 1
    See [Using Constructors](http://msdn.microsoft.com/en-us/library/ms173115.aspx), near the bottom, where it says: *A constructor can invoke another constructor in the same object by using the `this` keyword. ...* – Jeppe Stig Nielsen Oct 22 '13 at 19:28
  • 1
    Let me SymbolHound that for you http://symbolhound.com/?q=c%23+%3A+this%28 (but seriously, that site is extremely useful for searching for symbols and other words that are hard to google) – Tim S. Oct 22 '13 at 19:29

6 Answers6

3

Because your class has another constructor which takes and int as parameter.

public class MyObject
{
    public MyObject()
        : this(123)
    {
    }

    public MyObject(int x) //something like this
    {
    }
}

See: Using Constructors (C# Programming Guide)

A constructor can invoke another constructor in the same object by using the this keyword.

Habib
  • 219,104
  • 29
  • 407
  • 436
2

This means, that you are calling another constructor with the fixed Value "123":

public class MyObject
{      
    public MyObject(): this(123)
    {

    }

    public MyObject(int number)
    {

    }
}   

Means: Whenever you call new MyObject(), without any parameter, it equals the call to new MyObject(123);

dognose
  • 20,360
  • 9
  • 61
  • 107
  • *"it equals the call to new MyObject(123);"* Not really.. there can be stuff in the default constructor that is not executed in the other constructors – default Oct 22 '13 at 19:27
  • In this example, there is nothing in the constructors, so it's equal :P – dognose Dec 13 '22 at 22:46
1

this is used to call one constructor from another within the same class. Refer to this article for better understanding.

http://www.codeproject.com/Articles/7011/An-Intro-to-Constructors-in-C

Irfan TahirKheli
  • 3,652
  • 1
  • 22
  • 36
1

You have another constructor that accepts an int (thought it could be long or double, or anything else that int can implicitly cast to)

    public class MyObject
    {      
       public MyObject(): this(123)
       {
       }

       public MyObject(int num)
       {
          //do something with the num
       }
   }
Bryan
  • 5,065
  • 10
  • 51
  • 68
0

That means "before you execute what between the curly brackets, execute the suitable Constructor with parameters 123"

Orel Eraki
  • 11,940
  • 3
  • 28
  • 36
0

The syntax provided is used for "constructor chaining" whereby the specified constructor (which accepts an integer argument) is called before the body of the current constructor.

EricLaw
  • 56,563
  • 7
  • 151
  • 196