具有下限的 .Net 数组 >0 [英] .Net arrays with lower bound > 0

查看:14
本文介绍了具有下限的 .Net 数组 >0的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

虽然可能想做一件奇怪的事情,但我需要在 .Net 中创建一个下限 > 0 的数组.这起初似乎是可能的,使用:

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:

Array.CreateInstance(typeof(Object), new int[] {2}, new int[] {9});

产生所需的结果(下限设置为 9 的对象数组).然而,创建的数组实例不能再传递给其他方法,期望 Object[] 给我一个错误说:

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 Object[] giving me an error saying that:

System.Object[*] 不能转换为 System.Object[].数组类型有什么区别,我该如何克服?

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

测试代码 =

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

失败并显示:无法将类型为 'System.Object[*]' 的对象转换为类型为 'System.Object[]'."

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

我还想指出,这种方法在使用多个维度时确实有效:

I would also like to note that this approach DOES work when using multiple dimensions:

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

哪个工作正常.

推荐答案

你不能从一个转换到另一个的原因是这是邪恶的.

The reason why you can't cast from one to the other is that this is evil.

假设您创建了一个 object[5..9] 数组,并将其作为 object[] 传递给函数 F.

Lets say you create an array of object[5..9] and you pass it to a function F as an object[].

函数如何知道这是一个 5..9 ?F 期待一个通用数组,但它得到了一个受约束的数组.你可以说它有可能知道,但这仍然是出乎意料的,人们不想每次想要使用一个简单的数组时都进行各种边界检查.

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.

数组是编程中最简单的结构,如果太复杂就无法使用.您可能需要其他结构.

An array is the simplest structure in programming, making it too complicated makes it unsusable. You probably need another structure.

你应该做的是一个类,它是一个模仿你想要的行为的受限集合.这样,该类的所有用户都会知道会发生什么.

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.

class ConstrainedArray<T> : IEnumerable<T> where T : new()
{
    public ConstrainedArray(int min, int max)
    {
        array = new T[max - min];
    }

    public T this [int index]
    {
        get { return array[index - Min]; }
        set { array[index - Min] = value; }
    }

    public int Min {get; private set;}
    public int Max {get; private set;}

    T[] array;

    public IEnumerator<T> GetEnumerator()
    {
        return array.GetEnumarator();
    }

    System.Collections.IEnumerator System.Collections.IEnumerable.GetEnumerator()
    {
        return array.GetEnumarator();
    }

}

这篇关于具有下限的 .Net 数组 >0的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆