What does this("Default Name") mean?
I am reading Begining Visual C# 2005. I encounted a question about the following part. Anyone help me out? see the comments!
using System;
using System.Collections.Generic;
using System.Text;
namespace Ch10Ex01
{
class MyClass
{
public readonly string Name;
private int intVal;
public int Val
{
get { return intVal; }
set
{
if (0 <= value && value <= 10)
intVal = value;
else
throw (new ArgumentOutOfRangeException("Val", value,
"Val must be assigned a value between 0 and 10."));
}
}
public override string ToString()
{
return "Name: " + Name + "\nVal: " + Val;
}
private MyClass()
: this("Default Name") /*Explaination in the book:Note that I've used this("Default Name") to ensure that Name gets a value if this constructor ever gets called, which is possible if this class is used to derive a new class. This is necessary as not assigning a value to the Name field could be a source of errors later.*/
/* What's puzzling me: How can it be used in a derived class as it's "private"? What does this("Default Name") mean? How does the object get the "Default Name" as its name? */
{
}
public MyClass(string newName)
{
Name = newName;
intVal = 0;
}
}
}
|