C
C#15mo 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
Kouhai15mo ago
Well, you'd acccess the first 5 items in the array and sum them up?
Blake
BlakeOP15mo 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
JakenVeina15mo ago
look into loops
Angius
Angius15mo ago
Or range operators
MODiX
MODiX15mo 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
BlakeOP15mo ago
This is exactly what I was looking for ty lol ❤️
Accord
Accord15mo 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