Reputation: 3442
I was looking for a way, how to set specific values for specific range in an array.
Something like this
Pseudocode:
var s = new uint[64];
s[ 0..15] := { 2, 4, 6, 3, 1, 7, 8, 9, 7, 11, 37, 32, 19, 16, 178, 2200 }
s[16..31] := ...
I was trying to find something like this in C#, but with no luck. I am trying to come with something like this:
public void SetArrayValues(int startIndex, uint[] values)
{
var length = values.Length;
this.array[startIndex, startIndex + length] = values;
}
The only thing I was able to find was System.Array.SetValue but this does not meet my requirements.
Upvotes: 7
Views: 15108
Reputation: 109567
You could write an extension method to make this simpler:
public static class ArrayExt
{
public static T[] Set<T>(this T[] self, int index, params T[] values)
{
Array.Copy(values, 0, self, index, values.Length);
return self;
}
}
Using it would look like this:
var s = new uint[64];
s.Set<uint>(0, 2, 4, 6, 3, 1, 7, 8, 9, 7, 11, 37, 32, 19, 16, 178, 2200);
Because Set()
returns the array, you can also chain calls like this:
s.Set<uint>( 0, 2, 4, 6, 3, 1, 7, 8, 9, 7, 11, 37, 32, 19, 16, 178, 2200)
.Set<uint>(16, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16);
Note: The requirement to explicitly put the type into the call comes from the fact that we're using int
s in the list of values.
You should really use uint
since the destination is uint
, then there is no need to specify the type:
var s = new uint[64];
s.Set(0, 2u, 4u, 6u, 3u, 1u, 7u, 8u, 9u, 7u, 11u, 37u, 32u, 19u, 16u, 178u, 2200u);
And of course it works with all types, e.g. string
:
var x = new string[64];
x.Set(10, "A", "B", "C");
Upvotes: 3
Reputation: 1633
You can do it in a couple ways. Because I do not know you requirements and your code, I know you would need to modify this to suit you. But C# arrays are unlike C++ arrays, so the index always start from zero.
UInt64[] list = {2, 4, 6, 3, 1, 7, 8, 9, 7, 11, 37, 32, 19, 16, 178, 2200,..};
//Length of the list must be 31. That is contain 31 numbers.
UInt64[] s= new UInt64[31];
// You can have all the collection
for (int i = 0; i < list.Length; i++)
{
s[i] = list[i];
}
// Or you can modify as below
for (int i = 0; i < 15; i++)
{
s[i] = list[i];
}
for (int i = 16; i < 31; i++)
{
s[i] = list[i];
}
Upvotes: 0
Reputation: 1936
First you define your Arrays
s1 := { ... }
s2 := { ... }
s3 := { ... }
...
sn := { ... }
Then just concat them in a linq method chain
var result = s1.Concat(s2).Concat(s3)....Concat(sn
);
You can do it in whatever order you like and get the exact result. It's easy to change the resulting sequence just by altering the chain slightly.
Upvotes: 0
Reputation: 24390
I think the closest you can do is via Array.Copy
:
var s = new uint[64];
uint[] values = { 2, 4, 6, 3, 1, 7, 8, 9, 7, 11, 37, 32, 19, 16, 178, 2200 };
int sourceIndex = 0;
int destinationIndex = 0;
Array.Copy(values, sourceIndex , s, destinationIndex , values.Length);
Upvotes: 18
Reputation: 2800
Not an exact match to the method you described, but you could create a List<int>
and use the AddRange()
method. Once you've added all the values, you can use to ToArray()
method to turn it to an array.
var numbers = new List<int>();
numbers.AddRange(new List<int>{ 2, 4, 6, 3, 1, 7, 8, 9, 7, 11, 37, 32, 19, 16, 178, 2200 });
//numbers.AddRange(new List<int> { ... });
var asArray = numbers.ToArray();
Upvotes: 0