• Guest, before posting your code please take these rules into consideration:
    • It is required to use our BBCode feature to display your code. While within the editor click < / > or >_ and place your code within the BB Code prompt. This helps others with finding a solution by making it easier to read and easier to copy.
    • Don't share a wall of code. All we want is the problem area, the code related to your issue.
    To learn more about how to use our BBCode feature, please click here.

    Thank you, Code Forum.

C# How to make an array in C#

D

Deleted member 613

Guest
C#:
string[] Superpowers = { "water", "earth", "fire", "wind", "ice" };

To do this, type string with square brackets, the name of the array, an equals sign, curly brackets, and then inside put the info seperated with commas.

To print this do:
C#:
Console.WriteLine(Superpowers);
 
Last edited by a moderator:

Mathematical

Active Coder
GameZee said:
This is a 2D array
I'd just like to correct you but, you got that one wrong. What you have shown others is a single-dimensional array. A multi-dimensional array(Two-Dimensional, three-dimensional, etc.), would be like this:

C:
int array[20][2] = {
                                {...},
                                {...}
                           };
That's a multi-dimensional array(Two-Dimensional in this instance)

Regarding this: Console.WriteLine(Superpowers);

Never have I seen that done in any language before and I know this as I have worked with more than just one language. C, Java, Lua, Python, they all require you to use a for-loop if you want to print out the elements of an array. As seen here:
C:
int Array[20];

for(int I = 0; I <= 20; I++)
     // Note: Because there hasn't been any values initialized in the array itself, the program will simply print garbage values.
     printf("%d\n", Array[I]);

The same would in fact, be required for C#(Of course, you'd use the syntax for C#. My example was done in C, which was where C# descended from).

I would like to suggest one thing and it's that before you go around writing tutorials, actually make sure you are proficient in the language. That includes it's syntax, it's built-in standard library functions, the rules, etc. Because if you're actually proficient in the language, you won't be spreading around code that doesn't even work to beginners.
 
D

Deleted member 613

Guest
I'd just like to correct you but, you got that one wrong. What you have shown others is a single-dimensional array. A multi-dimensional array(Two-Dimensional, three-dimensional, etc.), would be like this:

C:
int array[20][2] = {
                                {...},
                                {...}
                           };
That's a multi-dimensional array(Two-Dimensional in this instance)

Regarding this: Console.WriteLine(Superpowers);

Never have I seen that done in any language before and I know this as I have worked with more than just one language. C, Java, Lua, Python, they all require you to use a for-loop if you want to print out the elements of an array. As seen here:
C:
int Array[20];

for(int I = 0; I <= 20; I++)
     // Note: Because there hasn't been any values initialized in the array itself, the program will simply print garbage values.
     printf("%d\n", Array[I]);

The same would in fact, be required for C#(Of course, you'd use the syntax for C#. My example was done in C, which was where C# descended from).

I would like to suggest one thing and it's that before you go around writing tutorials, actually make sure you are proficient in the language. That includes it's syntax, it's built-in standard library functions, the rules, etc. Because if you're actually proficient in the language, you won't be spreading around code that doesn't even work to beginners.
ok thanks that was the only one i was confused on - the rest im fine
 
Top