7

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.

3
  • 3
    Use List<T> AddRange() and convert it to array. Commented Mar 19, 2016 at 7:54
  • from your example it looks like you are not looking for a range, just for a way to fill array in sequential chunks, and that's much easier I think. @vortex suggested right way for it Commented Mar 19, 2016 at 8:29
  • Possible duplicate of How to copy part of an array to another array in C#? Commented Mar 19, 2016 at 8:57

5 Answers 5

18

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);
Sign up to request clarification or add additional context in comments.

Comments

3

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 ints 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");

Comments

0

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();

Comments

0

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.

Comments

0

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];
}

1 Comment

Arrays in C++ start from zero, too.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.