C#获取和设置列表集合的属性 [英] C# get and set properties for a List Collection

查看:41
本文介绍了C#获取和设置列表集合的属性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

集合集的属性如何?

我创建了一个具有Collection属性的类.我想在每次设置新值时将其添加到列表中.在set方法中使用_name.Add(value)无效.

I've created a class with a Collection properties. I want to add to the List anytime I set a new value. Using _name.Add(value) within the set method doesn't work.

Section newSec = new Section();

newSec.subHead.Add("test string");
newSec.subHead.Add("another test string");

public class Section
{
    public String head { get; set; }
    private List<string> _subHead = new List<string>();
    private List<string> _content = new List<string>();

    public List<string> subHead
    {
        get
        { return _subHead; }
        set
        {
            _subHead.Add(value);
        }
    }
    public List<string> content
    {
        get
        { return _content; }
        set
        {
            _content.Add(value);
        }
    }
}

使用我的解决方案更新:

Update with my solution:

public class Section
{

    private List<string> _head = new List<string>();
    private List<string> _subHead = new List<string>();
    private List<string> _content = new List<string>();

    public List<string> Head
    {
        get
        { return _head; }
    }

    public List<string> SubHead
    {
        get
        { return _subHead; }
    }
    public List<string> Content
    {
        get
        { return _content; }
    }

    public void AddHeading(string line)
    {
        Head.Add(line);
    }

    public void AddSubHeading(string line)
    {
        SubHead.Add(line);
    }

    public void AddContent(string line)
    {
        Content.Add(line);
    }

}

推荐答案

将其作为设置器的一部分是不合适的-好像您不是真的要设置整个字符串列表一样-您只是尝试添加一个.

It would be inappropriate for it to be part of the setter - it's not like you're really setting the whole list of strings - you're just trying to add one.

有几种选择:

  • 在类中放入 AddSubheading AddContent 方法,并且仅公开列表的只读版本
  • 将可变列表仅与getter一起公开,并让调用者将其添加到其中
  • 放弃封装的所有希望,仅使它们具有读/写属性
  • Put AddSubheading and AddContent methods in your class, and only expose read-only versions of the lists
  • Expose the mutable lists just with getters, and let callers add to them
  • Give up all hope of encapsulation, and just make them read/write properties

在第二种情况下,您的代码可以是:

In the second case, your code can be just:

public class Section
{
    public String Head { get; set; }
    private readonly List<string> _subHead = new List<string>();
    private readonly List<string> _content = new List<string>();

    // Note: fix to case to conform with .NET naming conventions
    public IList<string> SubHead { get { return _subHead; } }
    public IList<string> Content { get { return _content; } }
}

这是相当实用的代码,尽管它确实是 ,这意味着调用者可以按自己想要的任何方式对您的集合进行突变,这可能不是理想的选择.第一种方法可以保持最大的控制权(只有您的代码才能看到可变列表),但对于调用者而言可能不太方便.

This is reasonably pragmatic code, although it does mean that callers can mutate your collections any way they want, which might not be ideal. The first approach keeps the most control (only your code ever sees the mutable list) but may not be as convenient for callers.

使集合类型的设置器实际上只是向现有集合中添加单个元素既不可行也不令人愉悦,因此我建议您放弃该想法.

Making the setter of a collection type actually just add a single element to an existing collection is neither feasible nor would it be pleasant, so I'd advise you to just give up on that idea.

这篇关于C#获取和设置列表集合的属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆