在 C# 中的任意起始索引上初始化数组
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/82943/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Initializing an array on arbitrary starting index in c#
提问by juan
Is it possible in c# to initialize an array in, for example, subindex 1?
是否可以在 c# 中初始化数组,例如,子索引 1?
I'm working with Office interop, and every property is an object array that starts in 1 (I assume it was originally programed in VB.NET), and you cannot modify it, you have to set the entire array for it to accept the changes.
我正在使用 Office 互操作,每个属性都是一个以 1 开头的对象数组(我假设它最初是在 VB.NET 中编程的),并且您无法修改它,您必须设置整个数组才能接受变化。
As a workaround I am cloning the original array, modifying that one, and setting it as a whole when I'm done.
作为一种解决方法,我克隆原始数组,修改该数组,并在完成后将其设置为一个整体。
But, I was wondering if it was possible to create a new non-zero based array
但是,我想知道是否可以创建一个新的基于非零的数组
采纳答案by expedient
回答by Dested
You can write your own array class
您可以编写自己的数组类
回答by Patrick Desjardins
In VB6 you could change the array to start with 0 or 1, so I think VBScript can do the same. For C#, it's not possible but you can simply add NULL value in the first [0] and start real value at [1]. Of course, this is a little dangerous...
在 VB6 中,您可以将数组更改为以 0 或 1 开头,因此我认为 VBScript 也可以这样做。对于 C#,这是不可能的,但您可以简单地在第一个 [0] 中添加 NULL 值并从 [1] 开始实际值。当然,这有点危险……
回答by Aaron
Not simply. But you can certainly write your own class. It would have an array as a private variable, and the user would think his array starts at 1, but really it starts at zero and you're subtracting 1 from all of his array accesses.
不简单。但是您当然可以编写自己的类。它将有一个数组作为私有变量,用户会认为他的数组从 1 开始,但实际上它从 0 开始,并且您从他的所有数组访问中减去 1。
回答by Adam Vigh
I don't think if it's possible to modify the starting index of arrays.
我不认为是否可以修改数组的起始索引。
I would create my own array using generics and handle it inside.
我会使用泛型创建我自己的数组并在内部处理它。
回答by Joel Coehoorn
Just keep of const int named 'offset' with a value of one, and always add that to your subscripts in your code.
只需保留名为 'offset' 的值为 1 的 const int,并始终将其添加到代码中的下标中。
回答by xan
I don't think you can create non-zero based arrays in C#, but you could easily write a wrapper class of your own around the built in data structures.This wrapper class would hold a private instance of the array type you required; overloading the [] indexing operator is not allowed, but you can add an indexer to a class to make it behave like an indexable array, see here. The index function you write could then add (or subtract) 1, to all index's passed in.
我不认为您可以在 C# 中创建基于非零的数组,但是您可以轻松地围绕内置数据结构编写自己的包装类。这个包装类将保存您需要的数组类型的私有实例;不允许重载 [] 索引运算符,但您可以向类添加索引器,使其表现得像可索引数组,请参见此处。然后,您编写的索引函数可以对传入的所有索引加(或减)1。
You could then use your object as follows, and it would behave correctly:
然后,您可以按如下方式使用您的对象,它的行为将正确无误:
myArrayObject[1]; //would return the zeroth element.
回答by James Boother
It is possible to do as you request see the code below.
可以按照您的要求进行操作,请参阅下面的代码。
// Construct an array containing ints that has a length of 10 and a lower bound of 1
Array lowerBoundArray = Array.CreateInstance(typeof(int), new int[1] { 10 }, new int[1] { 1 });
// insert 1 into position 1
lowerBoundArray.SetValue(1, 1);
//insert 2 into position 2
lowerBoundArray.SetValue(2, 2);
// IndexOutOfRangeException the lower bound of the array
// is 1 and we are attempting to write into 0
lowerBoundArray.SetValue(1, 0);