来自这个线程:http://geekswithblogs.net/kaju/archive/2005/12/05/62266.aspx有人问(在评论中)这个问题:
is there any way to do something like this:
public FooBar(string fooBar)
{
string[] s = fooBar.split(new char[] { ':' });
this(s[0], s[1]);
}
public Foo(string foo, string bar)
{
...
}
好吧,我遇到过需要同样东西的情况。这有可能吗?提前致谢。
编辑
我是这个意思
public Foo(string fooBar)
{
string[] s = fooBar.split(new char[] { ':' });
this(s[0], s[1]);
}
public Foo(string foo, string bar)
{
...
}
Foo 是一个构造函数。
我的问题是在调用另一个构造函数之前我必须做很多逻辑——包括一些 IO 的东西。
最佳答案
不是直接的,而是:
public FooBar(string fooBar)
: this(fooBar.Split(new char[] { ':' }))
{
}
private FooBar(string[] s)
: this(s[0], s[1])
{
}
public FooBar(string foo, string bar)
{
...
}
关于c# - C# 中的链式构造函数 - 使用中间逻辑,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4097927/