Storing different types inside a list?

后端 未结 6 1043
广开言路
广开言路 2021-02-19 03:34

Related: A list of multiple data types?

I want to know how to store different array types (including system types) inside an array.

The above question covered ho

6条回答
  •  予麋鹿
    予麋鹿 (楼主)
    2021-02-19 03:49

    You can create a custom collection in which you implement Add() method which only accepts doubles and string, something like:

    void Add(object toAdd)
    {
         if (toAdd is string)
             // add into inner collection ... 
          ... (same for double)
    }
    

    But, to be honest, I really can't think of any way that you would need a collection that accepts only these two types. You can probably solve the problem some other way...

提交回复
热议问题