C# Lists - can I override Add() ?

This is a discussion on C# Lists - can I override Add() ? within the C# Programming forums, part of the General Programming Boards category; I'm wondering whether it's possible to override the System.Collections.Generic.List.Add() method so that it refuses to add more elements to a ...

  1. #1
    Registered User
    Join Date
    Oct 2009
    Posts
    5

    C# Lists - can I override Add() ?

    I'm wondering whether it's possible to override the System.Collections.Generic.List.Add() method so that it refuses to add more elements to a list once the list's capacity has been reached.

    Is this possible?

    Or does similar functionality already exist?


    Thanks

  2. #2
    Registered User
    Join Date
    Mar 2009
    Location
    england
    Posts
    209
    One option available to you is inheriting the List class and then using "new" to override the Add method. I think something like this would work:

    Code:
        class CustomList<T> : List<T>
        {
            public new void Add(T obj)
            {
                if (base.Count < 10) // or whatever limit
                    base.Add(obj);
            }
        }

  3. #3
    Anti-Poster
    Join Date
    Feb 2002
    Posts
    1,399
    Quote Originally Posted by theoobe View Post
    One option available to you is inheriting the List class and then using "new" to override the Add method. I think something like this would work:

    Code:
        class CustomList<T> : List<T>
        {
            public new void Add(T obj)
            {
                if (base.Count < 10) // or whatever limit
                    base.Add(obj);
            }
        }
    Not really. Anything that uses List<T> instead of CustomList<T> will use List<T>.Add, which isn't really what you're intending. Instead, you might have a private List<T> and implement the IList<T> interface with a lot of jump code, inserting your custom Add logic in IList<T>.Add.

    Alternately, if you're not bound to List<T>, you can use Collection<T> and override InsertItem.

    Quote Originally Posted by gah_ribaldi
    ...so that it refuses to add more elements to a list once the list's capacity has been reached.
    If you know your capacity, why use a List at all? Why not just use an array?
    If I did your homework for you, then you might pass your class without learning how to write a program like this. Then you might graduate and get your degree without learning how to write a program like this. You might become a professional programmer without knowing how to write a program like this. Someday you might work on a project with me without knowing how to write a program like this. Then I would have to do you serious bodily harm. - Jack Klein

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Algorithm for multiplying linked lists?
    By p1kn1c in forum C Programming
    Replies: 2
    Last Post: 04-05-2006, 12:59 PM
  2. Help needed Please
    By jereland in forum C Programming
    Replies: 9
    Last Post: 03-18-2004, 05:30 AM
  3. Map file formats and linked lists
    By Spitball in forum Game Programming
    Replies: 2
    Last Post: 03-04-2004, 11:32 PM
  4. doubly linked lists
    By qwertiop in forum C++ Programming
    Replies: 3
    Last Post: 10-03-2001, 07:25 PM

Tags for this Thread


1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21