c# - C# 中的链式构造函数 - 使用中间逻辑

标签 c# constructor

来自这个线程: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/

相关文章:

c# - 连接 Program.cs 和 Form.cs

C# Datetime 短年份与国家公约

c# - 如何访问 ASP.NET Entity Framework 中的旧实体值

c# - 添加自己的 javascript 进行验证

c# - 在设计时限制构造函数中参数的值

c++ - C++ 中的复制控制

c# - 将委托(delegate)转换为 System.Action

javascript - 引用错误 : Image is not defined

java - Java新手,尝试使用日期作为属性

c++ - 临时类对象