简体   繁体   English

C#获取并设置List Collection的属性

[英]C# get and set properties for a List Collection

How are properties for a Collection set? Collection的属性如何设置?

I've created a class with a Collection properties. 我创建了一个带有Collection属性的类。 I want to add to the List anytime I set a new value. 我想在设置新值时随时添加到List中。 Using _name.Add(value) within the set method doesn't work. 在set方法中使用_name.Add(value)不起作用。

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. 将它作为setter的一部分是不合适的 - 它不像你真正设置整个字符串列表 - 你只是想添加一个。

There are a few options: 有几个选择:

  • Put AddSubheading and AddContent methods in your class, and only expose read-only versions of the lists 在您的类中放置AddSubheadingAddContent方法,并且只显示列表的只读版本
  • Expose the mutable lists just with getters, and let callers add to them 只使用getter公开可变列表,然后让调用者添加它们
  • 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. 使集合类型的setter实际上只是将一个元素添加到现有集合既不可行也不愉快,所以我建议你放弃这个想法。

If I understand your request correctly, you have to do the following: 如果我理解您的请求,您必须执行以下操作:

public class Section 
{ 
    public String Head
    {
        get
        {
            return SubHead.LastOrDefault();
        }
        set
        {
            SubHead.Add(value);
        }

    public List<string> SubHead { get; private set; }
    public List<string> Content { get; private set; }
} 

You use it like this: 你这样使用它:

var section = new Section();
section.Head = "Test string";

Now "Test string" is added to the subHeads collection and will be available through the getter: 现在,“测试字符串”被添加到subHeads集合中,并将通过getter提供:

var last = section.Head; // last will be "Test string"

Hope I understood you correctly. 希望我理解正确。

Or 要么

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

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

  public void AddContent(String argValue)
  {
    _content.Add(argValue);
  }

  public void AddSubHeader(String argValue)
  {
    _subHead.Add(argValue);
  }
}

All depends on how much of the implementaton of content and subhead you want to hide. 全部取决于您要隐藏的内容和子标题的实现程度。

Your setters are strange, which is why you may be seeing a problem. 你的安装人员很奇怪,这就是你可能会遇到问题的原因。

First, consider whether you even need these setters - if so, they should take a List<string> , not just a string : 首先,考虑一下你是否需要这些setter - 如果是这样,他们应该使用List<string> ,而不仅仅是一个string

set
{
    _subHead = value;
}

These lines: 这些线:

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

Are calling the getter and then call Add on the returned List<string> - the setter is not invoked. 调用getter然后在返回的List<string>上调用Add - 不调用setter。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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