话不多说,直接上代码:
public class Test
{
private string _testname;
public string testname
{
set { _testname = value; }
get { return _testname; }
}
public Test():this("test")
{ }
public Test(string test)
{
this.testname = test;
}
}
控制台:
class Program
{
static void Main(string[] args)
{
DataHelp.Test test = new Test();
Console.WriteLine(test.testname);
Console.Read();
}
}
经过代码测试知道上面的含参构造函数在第一个构造函数中调用的,冒号后面为传的参数的值,构造函数没有返回值,这样便实现了实例化类时给类的属性赋值。