I. Using an Extension Method: here we're just packaging up what you'd do with a for-loop:
public static class ArrayExtensions
{
public static void Array2dInsert<T>(this T[,] ary, int rowndx, int startcol, params T[] values)
{
if (ary.GetLength(0) < rowndx
|| ary.GetLength(1) < startcol)
{
throw new ArgumentOutOfRangeException("rowndx or startcol", "Error in Array2dInsert: Out of range row or column index");
}
if(values.Length > ary.GetLength(1) - startcol)
{
throw new ArgumentOutOfRangeException("values", "Error in Array2dInsert: Too many values");
}
foreach (var value in values)
{
ary[rowndx, startcol] = value;
startcol++;
}
}
}
Test: execute this code in some Method or EventHandler
int[,] intary = new int[2,6];
intary.Array2dInsert(0,0,100,200,300);
Note that with this Extension Method you can specify the index in the "row" of the Array you wish to start inserting values at.
II. using a Class with custom Indexers:
I strongly suggest you study the following tutorials before you start reading this: [
^],[
^].
And, do review some of the many articles on Indexers here on CodeProject:[
^].
Keep in mind that, internally in .NET, an Indexer
is a Method.
First, let's look at what can be done, but, imho, should not be done:
using System.Collections.Generic;
namespace ArrayUtilities
{
public class Array2dEx<T>
{
private T[,] innerArray { set; get; }
public Array2dEx(int sz1, int sz2)
{
innerArray = new T[sz1, sz2];
}
public T this[int ndx1, int ndx2]
{
get
{
return innerArray[ndx1, ndx2];
}
set
{
innerArray[ndx1, ndx2] = value;
}
}
public IEnumerable<T> this[int ndx1, params T[] args]
{
set
{
for (int i = 0; i < args.Length; i++)
{
innerArray[ndx1, i] = args[i];
}
}
get
{
for (int j = 0; j < innerArray.GetLength(1); j++)
{
yield return innerArray[ndx1, j];
}
}
}
}
}
An example of using this Class:
Array2dEx<string> test1 = new Array2dEx<string>(4, 5);
test1[0, 0] = "hello";
test1[0, 1] = "goodbye";
string[] testary = new string[] { "what", "where" };
test1[2, testary] = null;
var row2 = test1[2];
var row2item2 = test1[2, 1];
I suggest you copy the above code into a .NET project, and put a break-point at the place indicated and then step through the code, inspecting the values with each step.
The Class 'Array2dEx is a generic Class that holds a 2d Array of a Type you specify, and provides two different indexing methods. One indexing method is a replacement for the standard indexing method, and is exactly what you'd expect from a 2d Array index.
Note 1: the second indexing method uses the fact that a custom indexer can take arbitrary parameters, including, as shown in this case, a list of parameters of any size (using the 'params argument facility).
Note that the setter of the second Indexer does not make use of the 'value parameter that any Property provides; we used 'null as a "throw-away" argument. More about that, later: a setter must have some value passed to it when it's called.
Note 2: here the 'getter part of the indexer returns an IEnumerable<T>
Why you should not do this:
1. It's weird, "hackish," and bypasses the usual syntax for dealing with Arrays.
2. someone else in the future may look at the code and wonder what's going on with what appears to be assigning 'null to the unusual "stuff" between brackets.
... to be continued, tomorrow ... what you could use if you used a jagged Array, that I think is much better.