Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Set array values for specific range in C#

Tags:

arrays

c#

I was looking for a way, how to set specific values for specific range in an array.

Something like this

Pseudocode:

var s = new uinit[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;
}

only thing I was able to find was System.Array.SetValue but this does not meet my requirements.

Am I missing something?

Thanks for any help in advance

like image 425
Kajiyama Avatar asked Mar 19 '16 07:03

Kajiyama


People also ask

Can you change array values in C?

We can change the contents of array in the caller function (i.e. test_change()) through callee function (i.e. change) by passing the the value of array to the function (i.e. int *array).

What is the range of array in C?

The indices for a 100 element array range from 0 to 99.

Can I slice an array in C?

Array-slicing is supported in the print and display commands for C, C++, and Fortran. Expression that should evaluate to an array or pointer type. First element to be printed. Defaults to 0.


1 Answers

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);
like image 117
Saeb Amini Avatar answered Sep 26 '22 14:09

Saeb Amini