<p>Although perhaps a bizare thing to want to do, I need to create an Array in .Net with a lower bound > 0. This at first seems to be possible, using:</p>

<pre><code>Array.CreateInstance(typeof(Object), new int[] {2}, new int[] {9});
</code></pre>

<p>Produces the desired results (an array of objects with a lower bound set to 9). However the created array instance can no longer be passed to other methods expecting <code>Object[]</code> giving me an error saying that:</p>

<p><code>System.Object[*]</code> can not be cast into a <code>System.Object[]</code>. What is this difference in array types and how can I overcome this?</p>

<p>Edit: test code = </p>

<pre><code>Object x = Array.CreateInstance(typeof(Object), new int[] {2}, new int[] {9});
Object[] y = (Object[])x;
</code></pre>

<p>Which fails with: "Unable to cast object of type 'System.Object[*]' to type 'System.Object[]'."</p>

<p>I would also like to note that this approach <strong>DOES</strong> work when using multiple dimensions:</p>

<pre><code>Object x = Array.CreateInstance(typeof(Object), new int[] {2,2}, new int[] {9,9});
Object[,] y = (Object[,])x;
</code></pre>

<p>Which works fine.</p>

[{'answer_id': 56457, 'author': 'Martin Marconcini', 'author_id': 2684, 'author_profile': 'https://Stackoverflow.com/users/2684', 'pm_score': 1, 'selected': False, 'text': '<p>I\'m not sure about why that can\'t be passed as Object[], but wouldn\'t be easy if you just create a real class to wrap an array and handle your "weird logic" in there?</p>\n\n<p>You\'d get the benefits of using a real reference object were you could add "intelligence" to your class.</p>\n\n<p>Edit: How are you casting your Array, could you post some more code? Thanks.</p>\n'}, {'answer_id': 56495, 'author': 'Coincoin', 'author_id': 42, 'author_profile': 'https://Stackoverflow.com/users/42', 'pm_score': 2, 'selected': False, 'text': "<p>The reason why you can't cast from one to the other is that this is evil.</p>\n\n<p>Lets say you create an array of object[5..9] and you pass it to a function F as an object[].</p>\n\n<p>How would the function knows that this is a 5..9 ? F is expecting a general array but it's getting a constrained one. You could say it's possible for it to know, but this is still unexpected and people don't want to make all sort of boundary checks everytime they want to use a simple array.</p>\n\n<p>An array is the simplest structure in programming, making it too complicated makes it unsusable. You probably need another structure.</p>\n\n<p>What you chould do is a class that is a constrained collection that mimics the behaviour you want. That way, all users of that class will know what to expect.</p>\n\n<pre><code>class ConstrainedArray&lt;T&gt; : IEnumerable&lt;T&gt; where T : new()\n{\n    public ConstrainedArray(int min, int max)\n    {\n        array = new T[max - min];\n    }\n\n    public T this [int index]\n    {\n        get { return array[index - Min]; }\n        set { array[index - Min] = value; }\n    }\n\n    public int Min {get; private set;}\n    public int Max {get; private set;}\n\n    T[] array;\n\n    public IEnumerator&lt;T&gt; GetEnumerator()\n    {\n        return array.GetEnumarator();\n    }\n\n    System.Collections.IEnumerator System.Collections.IEnumerable.GetEnumerator()\n    {\n        return array.GetEnumarator();\n    }\n\n}\n</code></pre>\n"}, {'answer_id': 56529, 'author': 'Joel Coehoorn', 'author_id': 3043, 'author_profile': 'https://Stackoverflow.com/users/3043', 'pm_score': 1, 'selected': False, 'text': '<p>Just store your lower bound in a const offset integer, and subtract that value from whatever your source returns as the index.</p>\n\n<p>Also: this is an old VB6 feature.  I think there might be an attribute to help support it.</p>\n'}, {'answer_id': 57501414, 'author': 'SoLaR', 'author_id': 1011436, 'author_profile': 'https://Stackoverflow.com/users/1011436', 'pm_score': 0, 'selected': False, 'text': '<p>Know it\'s old question, but to fully explain it.</p>\n\n<p>If type (in this case a single-dimension array with lower bound > 0) can\'t be created by typed code, simply reflected type instance can\'t be consumed by typed code then.</p>\n\n<p>What you have noticed is already in documentation:</p>\n\n<p><a href="https://learn.microsoft.com/en-us/dotnet/framework/reflection-and-codedom/specifying-fully-qualified-type-names" rel="nofollow noreferrer">https://learn.microsoft.com/en-us/dotnet/framework/reflection-and-codedom/specifying-fully-qualified-type-names</a></p>\n\n<blockquote>\n  <p>Note that from a runtime point of view, MyArray[] != MyArray[*], but\n  for multidimensional arrays, the two notations are equivalent. That\n  is, Type.GetType("MyArray [,]") == Type.GetType("MyArray[*,*]")\n  evaluates to true.</p>\n</blockquote>\n\n<p>In c#/vb/... you can keep that reflected array in object, pass around as object, and use only reflection to access it\'s items. </p>\n\n<p>-</p>\n\n<p>Now you ask "why is there LowerBound at all?", well COM object aren\'t .NET, it could be written in old VB6 that actually had array object that has LowerBound set to 1 (or anything VB6 had such freedom or curse, depends whom you ask). To access first element of such object you would actually need to use \'comObject(1)\' instead of \'comObject(0)\'. So the reason to check lower bound is when you are performing enumeration of such object to know where to start enumeration, since element functions in COM object expects first element to be of LowerBound value, and not Zero (0), it was reasonable to support same logic on such instances. Imagine your get element value of first element at 0, and use some Com object to pass such element instance with index value of 1 or even with index value of 2001 to a method, code would be very confusing.</p>\n\n<p>To put it simply: it\'s mostly for legacy support only!</p>\n'}, {'answer_id': 69082566, 'author': 'usr', 'author_id': 122718, 'author_profile': 'https://Stackoverflow.com/users/122718', 'pm_score': 1, 'selected': False, 'text': "<p>The .NET CLR differentiates between two internal array object formats: <em>SZ arrays</em> and <em>MZ arrays</em>. MZ arrays can be multi-dimensional and store their lower bounds in the object.</p>\n<p>The reason for this difference is two-fold:</p>\n<ol>\n<li>Efficient code generation for single-dimensional arrays requires that there is no lower bound. Having a lower bound is incredibly uncommon. We would not want to sacrifice significant performance in the common case for this rarely used feature.</li>\n<li>Most code expects arrays with zero lower bound. We certainly don't want to pollute all of our code with checking the lower bound or adjusting loop bounds.</li>\n</ol>\n<p>These concerns are solved by making a separate CLR type for SZ arrays. This is the type that almost all practically occurring arrays are using.</p>\n"}]
['https://Stackoverflow.com/questions/56446', 'https://Stackoverflow.com', 'https://Stackoverflow.com/users/1111/']