我可以在类中创建一个C#方法,我应该使用哪种方法?

时间:2012-07-11 05:41:26

标签: c#

我有以下课程:

public class Content {
      public string PartitionKey { get; set; }
      public string RowKey { get; set; }
}

以下视图模型:

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

我在ViewModel中创建了“UseRowKey”字段。但是,我是否也可以将其创建为类Content的一部分?

1 个答案:

答案 0 :(得分:0)

这样的东西
public class Content
{
    public string PartitionKey { get; set; }
    public string RowKey { get; set; }
    public bool UseRowKey
    {
        get
        {
            return PartitionKey.Substring(2, 2) == "05" ||
                   PartitionKey.Substring(2, 2) == "06";
        }
    }
}

public class ContentViewModel
{
    public ContentViewModel() { }
    public Content Content { get; set; }
    public bool UseRowKey
    {
        get
        {
            return this.Content.UseRowKey;
        }
    }
}

修改

要将其用作方法,您可以尝试

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

    }
}

public class ContentViewModel
{
    public ContentViewModel() { }
    public Content Content { get; set; }
    public bool UseRowKey
    {
        get
        {
            return this.Content.UseRowKey();
        }
    }
}