Domanda

I was reading through some of the MSDN documentation in C# and found a piece of code i can use had these brackets between the string constructor and the string itself like this

string[] stringname;

What does this mean, or what does it do?

È stato utile?

Soluzione

It's just an array declaration. That means stringname holds an array of strings (or rather, it declares an array-of-strings variable, since it doesn't actually hold anything yet).

There are a few variations in C# for declaring arrays and initializing. There's a good rundown here.

var arr1 = new string[5];                  // empty array of length 5
string[] arr2 = { "a", "b", "c" };         // pre-populated array with 3 items

Altri suggerimenti

It's an array declaration. When you declare an array, you don't specify the size, like this:

string[] stringname;

To actually initialize it, you have to either specify the size or pre-initialize it with actual data.

string[] stringname = new string[3]; // would allocate an array for 3 strings, but without setting any value on it.
string[] stringname = new [] { "andré", "joseph" }; // would allocate the 2 strings in the array, with values on it.

To understand more about arrays, please refer to: http://msdn.microsoft.com/en-us/library/9b9dty7d.aspx

This defines an array of strings

This is declaring an Array of Strings. If you want to read more on Arrays in C# I would recommend...

Info on Arrays

"Arrays in General

C# arrays are zero indexed; that is, the array indexes start at zero. Arrays in C# work similarly to how arrays work in most other popular languages There are, however, a few differences that you should be aware of. When declaring an array, the square brackets ([]) must come after the type, not the identifier. Placing the brackets after the identifier is not legal syntax in C#."---MSDN Link

Any book on C# should have more information on the Array subject.

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top