我可以缩短或简化C#中的许多构造函数吗?

时间:2012-07-08 05:51:10

标签: c#

我得到了一些关于构造函数的建议,但现在我有很多。我可以通过功能或其他方式简化这些吗?这是我的班级?

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。有人说过命名和可选参数。这对我有帮助吗?

4 个答案:

答案 0 :(得分:5)

试试这个:

    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');

答案 1 :(得分:2)

使用所有参数编写单个构造函数:

    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中使用Optional Arguments

答案 2 :(得分:1)

是的,例如您的构造函数

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”

如果你想真的疯了,那么你的两个参数构造函数可以调用你的三个参数构造函数,依此类推等等

答案 3 :(得分:0)

在你的情况下,你可以只有一次构造函数:

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", "rk");

var content = new ContentViewModel("pk", "rk", "user");

甚至

var content = new ContentViewModel("pk", user: "user"); //without rk field