C# 在C中创建类时,是否可以在构造函数中创建对象#

C# 在C中创建类时,是否可以在构造函数中创建对象#,c#,C#,我有这门课: public class ContentViewModel { public Content Content { get; set; } public bool UseRowKey { get { return Content.PartitionKey.Substring(2, 2) == "05" || Content.PartitionKey.Substring(2, 2) == "06";

我有这门课:

public class ContentViewModel
{
    public Content Content { get; set; }
    public bool UseRowKey { 
        get {
            return Content.PartitionKey.Substring(2, 2) == "05" ||
               Content.PartitionKey.Substring(2, 2) == "06";
        }
    }
    public string TempRowKey { get; set; }

}
我现在正在这样做:

        var vm = new ContentViewModel();
        vm.Content = new Content(pk);
        vm.Content.PartitionKey = pk;
        vm.Content.Created = DateTime.Now;
有什么方法可以改变我的ContentViewModel,这样我就不需要做最后三项了吗 声明?

是的,像这样:

public class ContentViewModel 
{ 
    public ContentViewModel(Content c) 
    {
        if (c == null) throw new ArgumentNullException("Cannot create Content VM with null content.");
        this.Content = c;
    }
    public ContentViewModel(object pk) : this(Guid.NewGuid()) {}
    public ContentViewModel(object pk)
    {
        this.Content = new Content(pk); 
        this.Content.PartitionKey = pk; 
        this.Content.Created = DateTime.Now; 
    }

    public Content Content { get; set; } 
    public bool UseRowKey {  
        get { 
            return Content.PartitionKey.Substring(2, 2) == "05" || 
               Content.PartitionKey.Substring(2, 2) == "06"; 
        } 
    } 
    public string TempRowKey { get; set; } 

} 
是的,像这样:

public class ContentViewModel 
{ 
    public ContentViewModel(Content c) 
    {
        if (c == null) throw new ArgumentNullException("Cannot create Content VM with null content.");
        this.Content = c;
    }
    public ContentViewModel(object pk) : this(Guid.NewGuid()) {}
    public ContentViewModel(object pk)
    {
        this.Content = new Content(pk); 
        this.Content.PartitionKey = pk; 
        this.Content.Created = DateTime.Now; 
    }

    public Content Content { get; set; } 
    public bool UseRowKey {  
        get { 
            return Content.PartitionKey.Substring(2, 2) == "05" || 
               Content.PartitionKey.Substring(2, 2) == "06"; 
        } 
    } 
    public string TempRowKey { get; set; } 

} 

为什么不将参数传递给构造函数呢

public class ContentViewModel
{
    public ContentViewModel(SomeType pk)
    {
        Content = new Content(pk); //use pk in the Content constructor to set other params
    }  
    public Content Content { get; set; }
    public bool UseRowKey { 
        get {
            return Content.PartitionKey.Substring(2, 2) == "05" ||
               Content.PartitionKey.Substring(2, 2) == "06";
        }
    }
    public string TempRowKey { get; set; }
}

一般考虑OOP和:不需要嵌套的属性,如果不必告诉对象应该做什么,而不是如何(让对象本身决定它)。< /P> < P>为什么不将参数传递给构造函数?

public class ContentViewModel
{
    public ContentViewModel(SomeType pk)
    {
        Content = new Content(pk); //use pk in the Content constructor to set other params
    }  
    public Content Content { get; set; }
    public bool UseRowKey { 
        get {
            return Content.PartitionKey.Substring(2, 2) == "05" ||
               Content.PartitionKey.Substring(2, 2) == "06";
        }
    }
    public string TempRowKey { get; set; }
}

一般考虑OOP和:不需要嵌套的属性,如果不需要告诉对象应该做什么,而不是如何(让对象本身决定它)。< > > P>代码>对象初始化器< /代码>是有用的:

var vm = new ContentViewModel {Content = new Content {PartitionKey = pk, Created = DateTime.Now}};

全部在一行中。

对象初始值设定项可能有用:

var vm = new ContentViewModel {Content = new Content {PartitionKey = pk, Created = DateTime.Now}};

一行完成。

当然。。。只需将这三条语句放入构造函数中,使用
this.Content
而不是
vm.Content
。当然。。。只需将这三条语句放入构造函数中,使用
this.Content
而不是
vm.Content
。谢谢。有没有一种方法可以扩展一点,使之成为我可以创建ContentViewModel的替代方法,并在其中传递内容对象的另一个构造函数?是的-只需添加另一个接受
Content
实例的构造函数,如您所说。你可以像重载方法一样重载构造函数。我必须在构造函数中使用“this”这个词吗?@Gemma:No-它只是作为一种约定,用来表示你使用的是实例变量而不是局部变量-只有当局部变量和实例变量具有相同的名称时才需要这个词。谢谢。有没有一种方法可以扩展一点,使之成为我可以创建ContentViewModel的替代方法,并在其中传递内容对象的另一个构造函数?是的-只需添加另一个接受
Content
实例的构造函数,如您所说。你可以像重载方法一样重载构造函数。我必须使用“this”这个词吗在构造函数中?@Gemma:No-它只是作为一种惯例来表示您使用的是一个实例变量而不是一个局部变量-只有当局部变量和实例变量的名称相同时才需要它。GlennFerrieLive-您能解释第二行的作用吗。对不起,我不明白?还有一种方法可以创建ContentViewModel并将内容对象作为构造函数传入吗?是的。第二个类是一个“空”构造函数,它使用一些默认参数调用另一个类构造函数。我将在我的示例中添加第三个构造函数,它接受“Content”GlennFerrieLive-您能解释第二行的作用吗。对不起,我不明白?还有一种方法可以创建ContentViewModel并将内容对象作为构造函数传入吗?是的。第二个类是一个“空”构造函数,它使用一些默认参数调用另一个类构造函数。我将在我的示例中添加第三个构造函数,该构造函数采用“Content”