C
C#11mo ago
Blake

❔ Array confusion..

I'm trying to figure out how to choose a set amount of values from an array without using all the values in it at once. for example if I have int[] X = new int[] { 1, 2, 3, 4, 5, 6, 7}; and I wanted to get the sum of only the first 5 values in the array, how do I do that? hopefully I explained it well enough cuz I wasn't able to put my problem into the correct words and find the answer through google lol...
7 Replies
Kouhai
Kouhai11mo ago
Well, you'd acccess the first 5 items in the array and sum them up?
Blake
Blake11mo ago
yeah but I'm not quite sure how to write the code in a way that selects for example the first 5 but not the 6th onwards
JakenVeina
JakenVeina11mo ago
look into loops
Angius
Angius11mo ago
Or range operators
MODiX
MODiX11mo ago
Angius
REPL Result: Success
var arr = new[]{ 1, 2, 3, 4, 5, 6, 7, 8, 9 };
arr[..5]
var arr = new[]{ 1, 2, 3, 4, 5, 6, 7, 8, 9 };
arr[..5]
Result: int[]
[
1,
2,
3,
4,
5
]
[
1,
2,
3,
4,
5
]
Compile: 466.819ms | Execution: 57.355ms | React with ❌ to remove this embed.
Blake
Blake11mo ago
This is exactly what I was looking for ty lol ❤️
Accord
Accord11mo ago
Was this issue resolved? If so, run /close - otherwise I will mark this as stale and this post will be archived until there is new activity.
Want results from more Discord servers?
Add your server
More Posts