简体   繁体   中英

Can I create a C# method inside a class and what type of method should I use?

I have the following class:

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

The following View Model:

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";
            }
        }
    }

I created the field "UseRowKey" in the ViewModel. However can I also create this as a method that's part of the class Content?

How about something like

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;
        }
    }
}

EDIT

To use it as a method, you can try

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();
        }
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM