Click here to Skip to main content
15,899,661 members
Home / Discussions / C#
   

C#

 
GeneralRe: Collections, plus collection of base type - concept Pin
DaveyM6915-Dec-11 9:47
professionalDaveyM6915-Dec-11 9:47 
GeneralRe: Collections, plus collection of base type - concept Pin
PIEBALDconsult15-Dec-11 10:40
mvePIEBALDconsult15-Dec-11 10:40 
GeneralRe: Collections, plus collection of base type - concept Pin
DaveyM6915-Dec-11 11:27
professionalDaveyM6915-Dec-11 11:27 
GeneralRe: Collections, plus collection of base type - concept Pin
PIEBALDconsult15-Dec-11 11:51
mvePIEBALDconsult15-Dec-11 11:51 
GeneralRe: Collections, plus collection of base type - concept Pin
Luc Pattyn15-Dec-11 19:55
sitebuilderLuc Pattyn15-Dec-11 19:55 
GeneralRe: Collections, plus collection of base type - concept Pin
DaveyM6915-Dec-11 23:54
professionalDaveyM6915-Dec-11 23:54 
GeneralRe: Collections, plus collection of base type - concept Pin
BillWoodruff16-Dec-11 4:03
professionalBillWoodruff16-Dec-11 4:03 
GeneralRe: Collections, plus collection of base type - concept Pin
DaveyM6916-Dec-11 14:20
professionalDaveyM6916-Dec-11 14:20 
Well a simple collection (generic) that is immutable (so naturally read only) just needs to accept items at instanciation, optionaly an indexer with a getter only defined and a count property - and of course implemeting IEnumerable<T> so it can be enumerated. Therefore a simple wrapper around List<T> suffices:
C#
public class ImmutableCollection<T> : IEnumerable<T>
{
    private List<T> innerList;

    public ImmutableCollection(IEnumerable<T> collection)
    {
        innerList = new List<T>(collection);
    }

    public T this[int index]
    {
        get { return innerList[index]; }
    }
    public int Count
    {
        get { return innerList.Count; }
    }

    public IEnumerator<T> GetEnumerator()
    {
        return ((IEnumerable<T>)innerList).GetEnumerator();
    }
    IEnumerator IEnumerable.GetEnumerator()
    {
        return ((IEnumerable)innerList).GetEnumerator();
    }
}

As the innerList is never exposed it is read only Smile | :)

Usage:
C#
ImmutableCollection<string> collection = new ImmutableCollection<string>(new string[] { "A", "B", "C" });

Of course, the properties/fields of the the items contained will not be immutable, but that is the same as the ReadOnlyCollection<T> available from .AsReadOnly, but the minimum functionality only is exposed, and whatever you need to add can be created simply by accessing the inner list.

If necessary, the inner list could be made protected so it can be accessed directly in derived classes where concrete classes are desired or required.
Dave

Binging is like googling, it just feels dirtier.
Please take your VB.NET out of our nice case sensitive forum.
Astonish us. Be exceptional. (Pete O'Hanlon)

BTW, in software, hope and pray is not a viable strategy. (Luc Pattyn)



GeneralRe: Collections, plus collection of base type - concept Pin
BillWoodruff16-Dec-11 18:19
professionalBillWoodruff16-Dec-11 18:19 
AnswerRe: executive summary Pin
Luc Pattyn17-Dec-11 2:23
sitebuilderLuc Pattyn17-Dec-11 2:23 
GeneralRe: Collections, plus collection of base type - concept Pin
DaveyM6917-Dec-11 2:15
professionalDaveyM6917-Dec-11 2:15 
AnswerRe: Collections, plus collection of base type - concept Pin
Luc Pattyn17-Dec-11 2:32
sitebuilderLuc Pattyn17-Dec-11 2:32 
GeneralRe: Collections, plus collection of base type - concept Pin
Luc Pattyn17-Dec-11 2:42
sitebuilderLuc Pattyn17-Dec-11 2:42 
AnswerRe: Collections, plus collection of base type - concept Pin
Luc Pattyn17-Dec-11 2:48
sitebuilderLuc Pattyn17-Dec-11 2:48 
GeneralRe: Collections, plus collection of base type - concept Pin
DaveyM6917-Dec-11 3:10
professionalDaveyM6917-Dec-11 3:10 
AnswerRe: Collections, plus collection of base type - concept Pin
Luc Pattyn17-Dec-11 3:50
sitebuilderLuc Pattyn17-Dec-11 3:50 
AnswerRe: Collections, plus collection of base type - concept Pin
BillWoodruff16-Dec-11 20:48
professionalBillWoodruff16-Dec-11 20:48 
QuestionBoundColumns Not Working Pin
AmbiguousName15-Dec-11 6:45
AmbiguousName15-Dec-11 6:45 
AnswerRe: BoundColumns Not Working Pin
PIEBALDconsult15-Dec-11 7:02
mvePIEBALDconsult15-Dec-11 7:02 
AnswerRe: BoundColumns Not Working Pin
SilimSayo15-Dec-11 13:34
SilimSayo15-Dec-11 13:34 
QuestionC# help? Pin
Brian Reiber15-Dec-11 5:50
Brian Reiber15-Dec-11 5:50 
AnswerRe: C# help? Pin
Paladin200015-Dec-11 6:14
Paladin200015-Dec-11 6:14 
GeneralRe: C# help? Pin
harold aptroot15-Dec-11 6:28
harold aptroot15-Dec-11 6:28 
AnswerRe: C# help? Pin
Richard MacCutchan15-Dec-11 6:29
mveRichard MacCutchan15-Dec-11 6:29 
AnswerRe: C# help? Pin
PIEBALDconsult15-Dec-11 6:36
mvePIEBALDconsult15-Dec-11 6:36 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.