English

Learn C# – How To Add Values to a C# Array

The Array is used to store information in a structured manner. It is a data structure that helps store and retrieve data. If you are looking to add values to a C# array, you have come to the right place. You can do so by using the following code:

int [] add_arr = new int[15];
        for (int i=0; i < 400; i++){
               add_arr[i];
           }
       }

The above code is easy to understand. All you need to do is initialize a new array and then add values by using a loop.

However, using an Array data structure may not always be the best solution. For instance, if you later determine you need to expand the array size, you cannot just do that as it is an immutable data structure. To solve the problem, you can use Lists, a data structure that works similar to Array, but without the size limits. To utilize List, you can use the following code and get better results in terms of adding values later on:

List<int> add_list= new List<int>();
for (int i = 0; i < 400; i++)
{
   add_list.Add(value);
}

With List, you can add new elements by using the Add() function.

add_list.Add(55);

The above line of code will add 55 to the end of the list. In short, Arrays are immutable whereas Lists are a mutable data structure.

Sometimes, it is necessary to use Array. For example, if you are storing a numeric month value, there is no point in using List for the operation. But, if you have used List to create the month numbers, you can also easily convert it back to the Array by using the following code. This will add security to your code and also ensure that the app doesn’t break down due to bugs.

int[] terms = termsList.ToArray();

You can read more about Array Method here.

You can also check on our website videos about C#. Below are some examples:

  • Game Economy Simulation – #indie #gamedev (part 7) – C#
  • Working On Game Design Program!! (part 3) – C#

You can also follow some of our broadcasters who program in C#, as below:

 chubz

 AngryAlbino

Another cool way to find out interesting things about C# is to access our project page!

Dr. Michael J. Garbade

I, Dr. Michael J. Garbade is the co-founder of the Education Ecosystem (aka LiveEdu), ex-Amazon, GE, Rebate Networks, Y-combinator. Python, Django, and DevOps Engineer. Serial Entrepreneur. Experienced in raising venture funding. I speak English and German as mother tongues. I have a Masters in Business Administration and Physics, and a Ph.D. in Venture Capital Financing. Currently, I am the Project Lead on the community project -Nationalcoronalvirus Hotline I write subject matter expert technical and business articles in leading blogs like Opensource.com, Dzone.com, Cybrary, Businessinsider, Entrepreneur.com, TechinAsia, Coindesk, and Cointelegraph. I am a frequent speaker and panelist at tech and blockchain conferences around the globe. I serve as a start-up mentor at Axel Springer Accelerator, NY Edtech Accelerator, Seedstars, and Learnlaunch Accelerator. I love hackathons and often serve as a technical judge on hackathon panels.

View Comments

Recent Posts

Blockchain in Elections: A Leap Toward Transparent Democracy

In 2024 we're witnessing a critical point in democratic technology: the integration of blockchain and…

3 weeks ago

Win Big with Our Amazon Fire Max 11 & AirPods Pro Giveaway!

We’re thrilled to announce an exciting opportunity for you to win not one but two…

2 months ago

Unleashing Potential: How Education Ecosystem Transforms Learning into Real-World Success

Acquiring practical skills is crucial for career advancement and personal growth. Education Ecosystem stands out…

4 months ago

The Role of Artificial Intelligence in Modern Software Development

Artificial Intelligence (AI) has been making significant strides in various industries, and the software development…

7 months ago

Highest Stable Coin Yields – (W16 – 2024)

Another week to bring you the top yield platforms for three of the most prominent…

8 months ago

LEDU Token OTC Trading

If you hold a large volume of LEDU tokens above 1 million units and wish…

9 months ago