How to update only one element by index in a list of numbers with C#

1 Answer

0 votes
using System;
using System.Collections.Generic;
   
class Program
{
    static void Main() {
        List<int> list = new List<int> { 3, 7, 8, 91, 99, 10, -5, 1, 2 };
        int indexToUpdate = 2;
        int newValue = 8497;

        list[indexToUpdate] = newValue;

        Console.WriteLine(string.Join(", ", list));
    }
}
   
   
   
   
/*
run:
      
3, 7, 8497, 91, 99, 10, -5, 1, 2
    
*/

 



answered Oct 18, 2023 by avibootz
...