我可以缩短或简化C#中的许多构造函数吗?
我得到了一些关于建设者的建议,但现在我有很多。我可以用一个函数或其他方法来简化它们吗?这是我的课我可以缩短或简化C#中的许多构造函数吗?,c#,C#,我得到了一些关于建设者的建议,但现在我有很多。我可以用一个函数或其他方法来简化它们吗?这是我的课 public class ContentViewModel { public ContentViewModel() { } public ContentViewModel(Content content) { this.Content = content; } public Content
public class ContentViewModel
{
public ContentViewModel() { }
public ContentViewModel(Content content)
{
this.Content = content;
}
public ContentViewModel(string pk)
{
this.Content = new Content();
this.Content.PartitionKey = pk;
this.Content.Created = DateTime.Now;
}
public ContentViewModel(string pk, string rk)
{
this.Content = new Content();
this.Content.PartitionKey = pk;
this.Content.RowKey = rk;
this.Content.Created = DateTime.Now;
}
public ContentViewModel(string pk, string rk, string user)
{
this.Content = new Content();
this.Content.PartitionKey = pk;
this.Content.RowKey = rk;
this.Content.Created = DateTime.Now;
this.Content.CreatedBy = user;
}
public Content Content { get; set; }
public bool UseRowKey {
get {
return this.Content.PartitionKey.Substring(2, 2) == "05" ||
this.Content.PartitionKey.Substring(2, 2) == "06";
}
}
public string TempRowKey { get; set; }
}
我用的是c#4。有人提到了命名参数和可选参数。这对我有帮助吗?编写一个包含所有参数的构造函数:
public ContentViewModel(string pk, string rk, string user)
{
this.Content = new Content();
this.Content.PartitionKey = pk;
this.Content.RowKey = rk;
this.Content.Created = DateTime.Now;
this.Content.CreatedBy = user;
}
public ContentViewModel(string pk) : this(pk, "","") { }
然后从其他构造函数调用该构造函数,为缺少的参数传递默认值:
public ContentViewModel(string pk, string rk, string user)
{
this.Content = new Content();
this.Content.PartitionKey = pk;
this.Content.RowKey = rk;
this.Content.Created = DateTime.Now;
this.Content.CreatedBy = user;
}
public ContentViewModel(string pk) : this(pk, "","") { }
或者,您可以在C#4.0中使用。编写一个包含所有参数的构造函数:
public ContentViewModel(string pk, string rk, string user)
{
this.Content = new Content();
this.Content.PartitionKey = pk;
this.Content.RowKey = rk;
this.Content.Created = DateTime.Now;
this.Content.CreatedBy = user;
}
public ContentViewModel(string pk) : this(pk, "","") { }
然后从其他构造函数调用该构造函数,为缺少的参数传递默认值:
public ContentViewModel(string pk, string rk, string user)
{
this.Content = new Content();
this.Content.PartitionKey = pk;
this.Content.RowKey = rk;
this.Content.Created = DateTime.Now;
this.Content.CreatedBy = user;
}
public ContentViewModel(string pk) : this(pk, "","") { }
或者,您可以在C#4.0中使用。尝试以下方法:
public ContentViewModel() { }
public ContentViewModel(Content content)
{
this.Content = content;
}
public ContentViewModel(string pk): this(new Content)
{
this.Content.PartitionKey = pk;
this.Content.Created = DateTime.Now;
}
public ContentViewModel(string pk, string rk): this(pk)
{
this.Content.RowKey = rk;
}
public ContentViewModel(string pk, string rk, string user): this(pk, rk)
{
this.Content.CreatedBy = user;
}
或者你可以试试:
public ContentViewModel(Content content = null, string pk = null,
string rk = null, string user = null)
{
this.Content = content ?? new Content();
if (pk != null) this.Content.PartitionKey = pk;
if (rk != null) this.Content.RowKey = pk;
if (user != null) this.Content.CreatedBy = user;
this.Content.Created = DateTime.Now;
}
有了它,您可以调用构造函数设置或缺少任何参数,从左到右提供这些参数,或使用@Robert Harvey建议的命名参数。示例:
var c = new ContentViewModel();
var c = new ContentViewModel(pk: 'ppp', user: 'marco');
var c = new ContentViewModel(null, 'pk', null, 'marco');
试试这个:
public ContentViewModel() { }
public ContentViewModel(Content content)
{
this.Content = content;
}
public ContentViewModel(string pk): this(new Content)
{
this.Content.PartitionKey = pk;
this.Content.Created = DateTime.Now;
}
public ContentViewModel(string pk, string rk): this(pk)
{
this.Content.RowKey = rk;
}
public ContentViewModel(string pk, string rk, string user): this(pk, rk)
{
this.Content.CreatedBy = user;
}
或者你可以试试:
public ContentViewModel(Content content = null, string pk = null,
string rk = null, string user = null)
{
this.Content = content ?? new Content();
if (pk != null) this.Content.PartitionKey = pk;
if (rk != null) this.Content.RowKey = pk;
if (user != null) this.Content.CreatedBy = user;
this.Content.Created = DateTime.Now;
}
有了它,您可以调用构造函数设置或缺少任何参数,从左到右提供这些参数,或使用@Robert Harvey建议的命名参数。示例:
var c = new ContentViewModel();
var c = new ContentViewModel(pk: 'ppp', user: 'marco');
var c = new ContentViewModel(null, 'pk', null, 'marco');
是的,比如你的构造函数
public ContentViewModel(string pk)
{
this.Content = new Content();
this.Content.PartitionKey = pk;
this.Content.Created = DateTime.Now;
}
可以打电话
public ContentViewModel(string pk, string rk)
使用以下语法使用rk的默认值:
public ContentViewModel(string pk) : this(pk, "defaultRkValue"){}
您可以用一些有意义的数据替换“defaultRkValue”
如果你真的想发疯,那么你的双参数构造函数可以调用你的三参数构造函数,依此类推是的,例如你的构造函数
public ContentViewModel(string pk)
{
this.Content = new Content();
this.Content.PartitionKey = pk;
this.Content.Created = DateTime.Now;
}
可以打电话
public ContentViewModel(string pk, string rk)
使用以下语法使用rk的默认值:
public ContentViewModel(string pk) : this(pk, "defaultRkValue"){}
您可以用一些有意义的数据替换“defaultRkValue”
如果你真的想发疯,那么你的双参数构造函数可以调用你的三参数构造函数等等。在你的例子中,你可以只调用一个构造函数:
public ContentViewModel(string pk, string rk = null, string user = null)
{
this.Content = new Content();
this.Content.PartitionKey = pk;
this.Content.RowKey = rk;
this.Content.Created = DateTime.Now;
this.Content.CreatedBy = user;
}
你甚至可以这样称呼它:
var content = new ContentViewModel("pk");
或
或
甚至
var content = new ContentViewModel("pk", user: "user"); //without rk field
在您的情况下,您可以只使用一次构造函数:
public ContentViewModel(string pk, string rk = null, string user = null)
{
this.Content = new Content();
this.Content.PartitionKey = pk;
this.Content.RowKey = rk;
this.Content.Created = DateTime.Now;
this.Content.CreatedBy = user;
}
你甚至可以这样称呼它:
var content = new ContentViewModel("pk");
或
或
甚至
var content = new ContentViewModel("pk", user: "user"); //without rk field
如果你在C#4.0中工作,你可以使用,你可以从一个构造函数调用另一个构造函数,传递默认值。如果我能使用命名参数和可选参数,我非常感兴趣。这会更清楚吗?如果你在使用C#4.0,你可以使用你可以从一个构造函数调用另一个构造函数,传递默认值。是的,我使用的是C#4。如果我能使用命名参数和可选参数,我非常感兴趣。这会让事情变得更清楚吗?这里的一个人谈到使用命名和可选的论点。那对我有帮助吗。我使用的是C#4。至于第二个选项:OP实际上没有一个构造函数,它接受
内容
和其他值,这是非常有意义的——要么创建内容
并分配其值,要么准备一个带有值的内容
,但不能同时使用这两个值(注意,您总是设置内容。创建内容,但OP不设置)。此外,null
检查假定null是无效值-我们不知道,也不知道默认值不是null。@Kobi:这只是一个例子……对于我们拥有的元素,很难给出更精确的信息……这里的一个人说要使用命名和可选参数。这对我有帮助吗?我使用的是C#4。作为f或者第二个选项:OP实际上没有接受内容
和其他值的构造函数-这非常有意义-要么创建内容
并分配其值,要么准备一个带有值的内容
,但不是两者都有(注意,您总是设置内容。创建的
,但OP没有)。此外,null
检查假定null是无效值-我们不知道,也不知道默认值不为null。@Kobi:这只是一个示例…对于我们拥有的元素,很难给出更精确的信息…此示例使用C#4.0中的“可选和命名参数”。此示例使用“可选和命名参数”从C#4.0开始,这是一种正常的方式——更一般的构造函数调用传递默认值的更具体的构造函数。这使得在最具体的构造函数上调用基构造函数更简单。这是一种正常的方式——更一般的构造函数调用传递默认值的更具体的构造函数。这使得它更简单然后在最具体的构造函数上调用基构造函数。