Welcome!

By registering with us, you'll be able to discuss, share and private message with other members of our community.

SignUp Now!
  • 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.
    • You can also use markdown to share your code. When using markdown your code will be automatically converted to BBCode. For help with markdown check out the markdown guide.
    • 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:
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.
 
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
 

Buy us a coffee!

Back
Top Bottom