2011-11-19 61 views
9

Collection集的屬性如何?C#獲取和設置List Collection的屬性

我創建了一個具有集合屬性的類。我想隨時添加到列表中,我設置了一個新的值。在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); 
     } 
    } 
} 

更新我的解決方案:

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

} 

回答

17

這將是不合適的它是二傳手的一部分 - 它不喜歡你真的設置字符串的整個列表 - 你」只是試圖之一。

有幾個選項:

  • 把你的類AddSubheadingAddContent方法,只露出只讀版本的名單
  • 揭露可變的名單只是干將,讓來電者添加他們
  • 放棄封裝的希望,只是讓他們讀/寫性能

在第二種情況下,你的代碼可以只是:

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

這是合理務實的代碼,雖然它確實意味着呼叫者可以變異的集合,他們想要的任何方式,這可能並不理想。第一種方法保持最佳控制(只有你的代碼能夠看到可變列表),但對於調用者來說可能並不方便。

讓一個集合類型的setter實際上只添加一個元素到現有集合既不可行也不愉快,所以我建議你放棄這個想法。

+0

感謝您的幫助!我試着和#1門一起去。 – SharpBarb

0

您的設置者很奇怪,這就是您可能會看到問題的原因。

首先,考慮你是否更需要這些制定者 - 如果是這樣,就應該採取List<string>,不只是一個string

set 
{ 
    _subHead = value; 
} 

這些行:

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

呼叫的getter和然後在返回的List<string>上調用Add - 調用者未被調用。

4

如果我正確理解你的要求,你必須做到以下幾點:

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

你使用這樣的:

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

現在「測試串」被添加到副標題收集和將通過獲取者提供:

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

希望我能正確理解你。

+1

+1,因爲即使LastOrDefault可能不是正確答案,但我不知道該方法存在,所以今天我學到了一些東西,一切都很好。 –

+6

這不公平,我沒有機會反對Skeet:D – LueTm

+0

@LueTm,你爲什麼定義SubHead公開? –

3

或者

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

一切都取決於你想要多少內容implementaton和小標題隱藏。