Christof,
Thanks for the advice, it's a very clean solution - nice to know that I
wasn't going mad thinking there was a better way of doing things.
Also, you have to cast the returned "ReadOnlyCollection" property as a
"ICollection<T>" to get the Add value because it's an Explicit Interface
implementation (see below) - if you just use the property without the cast
you can't even see / use the Add method.
((ICollection<String>)listTest.Strings).Add("Test 2"); // << This will throw
the exception.
Thanks again,
- Paul.
"Christof Nordiek" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
> Hi,
>
> you could use the AsReadOnly method of the List<T> class. It returns a
> writeprotected wrapper around your list.
>
> your Strings property would look like this:
>
> public ReadOnlyCollection<string> Strings { get { return
> strings.AsReadOnly() } }
>
> The returned List will still have the Add method soice it implements
> IList<> and List<>, but they will cause exceptions.
> Not very nice, but atleast the caller can't change your internals.
>
> Christof
>
> "Paul" <(E-Mail Removed)> schrieb im Newsbeitrag
> news:(E-Mail Removed)...
>> Hi,
>>
>> I feel I'm going around circles on this one and would appreciate some
>> other points of view.
>>
>> From a design / encapsulation point of view, what's the best practise for
>> returning a private List<> as a property.
>>
>> Consider the example below, the class "ListTest" contains a private
>> "List<>" called "strings" - it also provides a public method to add to
>> that list, this method could contain all sorts of validation /
>> modification of the string to be added (encapsulation). However by then
>> exposing the private List<> as a public property you give the end user of
>> your class the ability to add to your List<> as show below.
>>
>> I understand why the above happens, I'm just trying to think what the
>> best course of action is if you always want your user to use the function
>> to add to your list, whilst retaining a property to access the list?
>> List<> doesn't contain a clone method - you could create this I know, but
>> then the amount of processing that involves implies it should be a method
>> rather than a parameter (parameters shouldn't really consider processor
>> intensive code).
>>
>> Like I say, I feel I'm going around in circles on this one, even to the
>> point of wondering if it actually matters.
>>
>> Regards,
>>
>> - Paul.
>> ====================================
>> using System;
>> using System.Collections.Generic;
>> using System.Text;
>>
>> class Program
>> {
>> static void Main(string[] args)
>> {
>> ListTest listTest = new ListTest();
>> listTest.AddString("Test 1"); // << Add the string the correct
>> way.
>> Console.WriteLine(listTest.Strings.Count);
>> listTest.Strings.Add("Test 2"); // << by-pass the AddString()
>> function.
>> Console.WriteLine(listTest.Strings.Count);
>> }
>> }
>>
>> class ListTest
>> {
>> private List<string> strings = null;
>>
>> public void AddString(string stringToAdd)
>> {
>> if (strings == null)
>> strings = new List<string>();
>> strings.Add(stringToAdd);
>> }
>> public List<string> Strings { get { return this.strings; } }
>> }
>> ====================================
>> Output is:
>> 1
>> 2
>>
>
>
|