<p>I am facing a problem with .NET generics. The thing I want to do is saving an array of generics types (GraphicsItem):</p>

<pre><code>public class GraphicsItem&lt;T&gt;
{
    private T _item;

    public void Load(T item)
    {
        _item = item;
    }
}
</code></pre>

<p>How can I save such open generic type in an array?</p>

[{'answer_id': 58401, 'author': 'aku', 'author_id': 1196, 'author_profile': 'https://Stackoverflow.com/users/1196', 'pm_score': 0, 'selected': False, 'text': "<p>If you want to store heterogeneous GrpahicsItem's i.e. GraphicsItem&lt; X> and GrpahicsItem&lt; Y> you need to derive them from common base class, or implement common interface. Another option is to store them in List&lt; object></p>\n"}, {'answer_id': 58406, 'author': 'Alex Duggleby', 'author_id': 5790, 'author_profile': 'https://Stackoverflow.com/users/5790', 'pm_score': 0, 'selected': False, 'text': "<p>Are you trying to create an array of GraphicsItem in a non-generic method?</p>\n\n<p>You cannot do the following:</p>\n\n<pre><code>static void foo()\n{\n  var _bar = List&lt;GraphicsItem&lt;T&gt;&gt;();\n}\n</code></pre>\n\n<p>and then fill the list later. </p>\n\n<p>More probably you are trying to do something like this?</p>\n\n<pre><code>static GraphicsItem&lt;T&gt;[] CreateArrays&lt;T&gt;()\n{\n    GraphicsItem&lt;T&gt;[] _foo = new GraphicsItem&lt;T&gt;[1];\n\n    // This can't work, because you don't know if T == typeof(string)\n    // _foo[0] = (GraphicsItem&lt;T&gt;)new GraphicsItem&lt;string&gt;();\n\n    // You can only create an array of the scoped type parameter T\n    _foo[0] = new GraphicsItem&lt;T&gt;();\n\n    List&lt;GraphicsItem&lt;T&gt;&gt; _bar = new List&lt;GraphicsItem&lt;T&gt;&gt;();\n\n    // Again same reason as above\n    // _bar.Add(new GraphicsItem&lt;string&gt;());\n\n    // This works\n    _bar.Add(new GraphicsItem&lt;T&gt;());\n\n    return _bar.ToArray();\n}\n</code></pre>\n\n<p>Remember you are going to need a generic type reference to create an array of a generic type. This can be either at method-level (using the T after the method) or at class-level (using the T after the class).</p>\n\n<p>If you want the method to return an array of GraphicsItem and GraphicsItem, then let GraphicsItem inherit from a non-generic base class GraphicsItem and return an array of that. You will lose all type safety though.</p>\n\n<p>Hope that helps.</p>\n"}, {'answer_id': 58462, 'author': 'Omer van Kloeten', 'author_id': 4979, 'author_profile': 'https://Stackoverflow.com/users/4979', 'pm_score': 3, 'selected': True, 'text': '<p>Implement a non-generic interface and use that:</p>\n\n<pre><code>public class GraphicsItem&lt;T&gt; : IGraphicsItem\n{\n    private T _item;\n\n    public void Load(T item)\n    {\n        _item = item;\n    }\n\n    public void SomethingWhichIsNotGeneric(int i)\n    {\n        // Code goes here...\n    }\n}\n\npublic interface IGraphicsItem\n{\n    void SomethingWhichIsNotGeneric(int i);\n}\n</code></pre>\n\n<p>Then use that interface as the item in the list:</p>\n\n<pre><code>var values = new List&lt;IGraphicsItem&gt;();\n</code></pre>\n'}]
['https://Stackoverflow.com/questions/58384', 'https://Stackoverflow.com', 'https://Stackoverflow.com/users/2078/']