Here is the code:
class Class1
{
private Class2 object;
public Class1(Class2 obj) : this(obj.ToString())
{
this.object = obj;
}
}
More specifically, what does the : this part do.
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
The
:this(obj.ToString)causes the constructor code for the constructor defined taking a string parameter to run first. Once it runs, then the constructor code (settingthis.object = obj) is executed.This is covered in MSDN’s page on Constructors (the last example set).
Note that, in your code above, as written, this will cause a compiler error. You would also need to have a constructor like the following:
With this constructor in place, it will get called, perform it’s initialization steps, then the first constructor (which sets
this.object) will run.