其实主要写这个东西是发现以前看的在构造函数里用this调用别的构造函数在进行本身构造函数的用法已经忘的一干二净了。所以写下加深下印象顺便写下这个技术的用在默认参数的示例。代码如下:
public class QuoteServer
{
private int port;
private string filename;

public QuoteServer() : this("quote.txt")
{
}
public QuoteServer(string filename) : this(filename,7890)
{
}
public QuoteServer(string filename,int port)
{
this.filename = filename;
this.port = port;
}
}
public class QuoteServer
{
private int port;
private string filename;
public QuoteServer() : this("quote.txt")
{
}
public QuoteServer(string filename) : this(filename,7890)
{
}
public QuoteServer(string filename,int port)
{
this.filename = filename;
this.port = port;
}
}


浙公网安备 33010602011771号