List that only accepts a few types

Is there any System namespace in a C # container that can only accept some types? For example, I want to create my list in which I will have only objects with type Class1 and int:

//accept only type Class1 and int;
MYLIST lst = new MYLIST(typeof(Class1), typeof(int));
lst.Add( 23 ); // OK
lst.Add( new Class1() ); // OK
lst.Add( "text" ); // wrong, not accepted type

      

Something like this in .NET, or should I write this myself? Thank.

+3


source to share


5 answers


The answer is NO, there is NO such list in C # and for a VERY GOOD reason.

You can do a wrapper, but I would suggest doing it.

 public class CustomListWrapper< T, F>
    {
        private readonly List<object> internalList;
        public CustomListWrapper()
        {
            this.internalList = new List<object>();
        }

        public void Add(object item)
        {
            if(!(item is T || item is F))
                throw new Exception();

            this.Add(item);
        }

    }

      



PS: before downvote, how to get the object ... well that's why it's a pretty bad idea, but you will need to do "is" on the type you choose to be able to cast it to the appropriate type.

Again, not exactly exactly why you need this.

+1


source


The C # type system doesn't allow you to express something like "either Class1

or int

". Having said that, you can use overloads to get half way:

class MyClass
{
    private List<object> _items = new List<object>();
    public void Add(int value) { _items.Add(value); }
    public void Add(Class1 value) { _items.Add(value); }
    ...
}

      



The real tricky question is how you parse things, not how you insert things. There are several possibilities: get everything as object

(by implementation IEnumerable<object>

) and possibly special methods such as GetInt(int index)

and GetClass1(int index)

.

+3


source


You cannot achieve this directly. The element type List<T>

must be basic, common to all types that you want to add to the list.

You may have List<object>

or a wrapper around the reason List<object>

. However, you will need to check at runtime if the items added to it are of the correct types and you will need to map the items you retrieve from the list to the correct type.

If you want to store different types in the same list, it is a good idea to create an interface that all these types must implement.

public interface ICommonInterface
{
    int Number { get; }
    string Text { get; }
}

public Class1 : ICommonInterface
{
    public int Number { get; set; }
    public string Text { get; set; }

    public string AnAdditionalProperty { get; set; }
}

public NumberWrapper : ICommonInterface
{
    public NumberWrapper (int number)
    {
        this.Number = number;
        this.Text = number.ToString();
    }

    public int Number { get; private set; }
    public string Text { get; private set; }
}

public TextWrapper : ICommonInterface
{
    public TextWrapper (string text)
    {
        this.Text = text;
        int i;
        Int32.TryParse(text, out i);
        this.Number = i;
    }

    public int Number { get; private set; }
    public string Text { get; private set; }
}

      

Then you can declare your list as

List<ICommonInterface> lst = new List<ICommonInterface>();
lst.Add(new Class1());
lst.Add(new NumberWrapper(77));
lst.Add(new TextWrapper("hello"));

Console.WriteLine(lst[0].Text);

      

+1


source


Not. You will have to create your own. You can implement ICollection or IEnumerable or IList or whatever. You have a lot of flexibility here. But the bottom line of the answer is no, no such collection exists, which allows you to automatically restrict the types in the collection to specific types.

+1


source


why not just wrap the List <> and create two add methods, one that accepts an int, the other that accepts Class1

0


source







All Articles