Search code examples
c#asp.netasp.net-mvcrazorrazorengine

Adding a new element to the end of a C# array


Im trying to do exactly what the title says

    int[] weeks = {};
    weeks[weeks.Length]=1;

this doesnt work. There is also no .Add Method.

Any ideas? or is this not possible


Solution

  • Arrays in C# by definition are of a fixed size, and cannot be dynamically expanded.

    I would suggest using a List<>, as they can be dynamically expanded, much like vectors in other programming languages.

    List<int> weeks = new List<int>();
    weeks.add(1);
    

    See more here.