How to Add to a C# String Array
C# is a programming language developed by Microsoft for use in programming applications for the Windows operating system. The language itself is type-safe and object-oriented. The syntax and style of the language make it easy to pick up if you are a programmer with experience in languages such as C++ and Java. This recognition will extend to the method for adding to an array in C#.
Instructions
-
-
1
Determine the length of your array of text strings. You would have set this parameter when you first declared the string with the "string[] some-string = new string[n];" where n is the number you chose.
-
2
Add a string to the first open slot by typing:
some-string[0] = "text you are about to add to the string.";
-
-
3
Add the other strings you desire by substituting the "0" with the next number until you hit the end of your array.
-
1
Tips & Warnings
When you are adding elements to your string array, remember not to exceed the number of slots you created. This will cause a compilation error.
Remember that array indexes start at zero. Consequently, as in the instructions, the first slot in the array is "some-array[0]," the second slot is "some-array[1]," until you hit the last entry which will be "some-array[n-1]" where n is the number you declared the string with.
If you declared the array with a number of pre-determined strings instead of typing in a number, then your array will only be as large as the number of items you included when you declared the array. You can still change the value of any array slot by following the same instructions as adding a string to it. This will simply over-write the previous value the slot held.
Remember that arrays are type-specific in C#. Consequently, when you create a string array you can only adds strings to it. Adding numbers or other variable types will cause a compilation error.
References
- Photo Credit Jupiterimages/Photos.com/Getty Images