0

I've just started learning C#, I'm good PHP developer and I'm trying to build a basic blackjack app for practice, I'm lost because arrays in PHP and arrays in C# are so different

I'm wondering how I can have the following array which is written in PHP to C#

$array = array("first_array" => array(), "second_array" => array());

I tried the following but it doesn't really seem to work

string[] array = ["first_array" => string[], "second_array" => string[]];

If anyone could help me or guide me, I'd be grateful.

Ali
  • 3,479
  • 4
  • 16
  • 31
  • Do you want string [] (array) of two Items ("First_Array" and "Second Array" ) ? – Rajnikant Jan 09 '15 at 12:34
  • It looks as though you are looking for a [multidimensional array](http://msdn.microsoft.com/en-us/library/2yd9wwz4.aspx), although these wouldn't have named parameters like it looks php has, What are you trying to achieve? there may be better c# alternatives – Sayse Jan 09 '15 at 12:34
  • Have a look at http://msdn.microsoft.com/de-de/library/2s05feca.aspx – Kᴀτᴢ Jan 09 '15 at 12:38
  • C# actually have a a way broader panel of data collections than PHP, so you might want to use another type than an array. Associative arrays don't exist in C# or at least not in that name. I would suggest you have a look at [this page](http://msdn.microsoft.com/en-us/library/ybcx56wz.aspx#BKMK_Generic) for example to have quick look at some collection types. Dictionary might be what you need but the choice really depend on what you plan to do (sorting, comparing, ...) – Laurent S. Jan 09 '15 at 12:38
  • "I'm lost because arrays in PHP and arrays in C# are so different" Yes, they are. It's very strange that PHP calls this an array. In most languages, this is called a HashTable or Dictionary. A C# Dictionary is NOT very different from a PHP array. – Dennis_E Jan 09 '15 at 12:42

4 Answers4

6

To declare a multi-dimensional string array in C#, it's a simple matter of writing this:

string[,] array = new string[4,4];//size of each array here

Initializing an array is done in the same way as it is in C(++), using curly brackets:

string[,] array = new string[,] { {"index[0][0]", "index[0][1]"}, {"index[1][0]", "index[1][1]"} };

basic tut on arrays in C# here

But you're not creating an array, you're using strings as keys, meaning you need a Dictionary:

Dictionary<string, string[]> dictionary = new Dictionary<string, string[]> {
    {"first_array", new string[4]},
    {"second_array", new string[4]}
};

In case of a Dictionary, owing to its declaration being quite verbose, it's quite common to see code that relies on C#'s implicit typing to abreviate things a bit:

var dictionary = new Dictionary<string, string[]> {
    {"first_array", new string[4]},
    {"second_array", new string[4]}
};

more on Dictionary here

Update:

Because you'd like to be able to append strings to the arrays as you go along (ie: you don't know the length of the array when you create it), you can't really use a regular string array. You'll have to use a dictionary of string lists:

var dictionary = new Dictionary<string, List<string>> {
    "first_list", new List<string>() { "first string", "second string" },
    "second_list", new List<string>() { "first string", "second string" }
};

So now, bringing it all together, and some examples of how you can add strings to lists, and add lists to the dictionary:

//add string to list in dictionary:
dictionary["first_list"].Add("third string");
//suppose we have an array, how to add to the dictionary?
string[] some_array = new string[2] {"string1", "string2"};
//simply initialize list using array of strings
var temp_list = new List<string>(some_array);
//add new key to dictionary
dictionary.Add("new_list", temp_list);

docs on the List<T> class

Note:
Arrays can be resized, so it is possible to use an array instead of a list, but in this case, it's better to use a list. Lists are designed to be resized, arrays are not the best tool for the job

Elias Van Ootegem
  • 74,482
  • 9
  • 111
  • 149
  • Thanks a lot, now I'm just wondering, if I wanted to add append another array inside the array in first_array, how will I be able to do that? How do I reference dictionaries and how do I append to them? – Ali Jan 09 '15 at 12:42
  • @Ali: If you don't know the length of the array beforehand, then you can't really use an array. C# does have a `List` for that. in C++, you'd use a `std::vector` for that, I'll check if there's a vector in C#, and update my answer – Elias Van Ootegem Jan 09 '15 at 12:45
  • @Ali, array *"can be"* (notice quotes) [resized](http://stackoverflow.com/a/19328266/1997232). But if you need `Add`/`Insert` or `Delete` options, then it's much better to use appropriate type (`IList`). – Sinatr Jan 09 '15 at 12:51
  • 1
    @Sinatr: I was trying to keep the OP in the dark, regarding resizing arrays because for this purpose, a list is the better option anyway. But yes, arrays aren't as fixed as perhaps my answer might seem to suggest – Elias Van Ootegem Jan 09 '15 at 12:55
0

I dont know very much about C#, but that's not the way that works. string[] in C# and in most programming languages indicates an array of strings. You may want to use Dictionary<string key, string[] array> to do that. Don't know if the Dictionary in C# is the same as HashMap in Java, but as I recall, it's the same.

Krisztián Dudás
  • 856
  • 10
  • 22
0
// Two-dimensional array.
int[,] array2D = new int[,] { { 1, 2 }, { 3, 4 }, { 5, 6 }, { 7, 8 } };
// The same array with dimensions specified.
int[,] array2Da = new int[4, 2] { { 1, 2 }, { 3, 4 }, { 5, 6 }, { 7, 8 } };
// A similar array with string elements.
string[,] array2Db = new string[3, 2] { { "one", "two" }, { "three", "four" },
                                        { "five", "six" } };

// Three-dimensional array.
int[, ,] array3D = new int[,,] { { { 1, 2, 3 }, { 4, 5, 6 } }, 
                                 { { 7, 8, 9 }, { 10, 11, 12 } } };
// The same array with dimensions specified.
int[, ,] array3Da = new int[2, 2, 3] { { { 1, 2, 3 }, { 4, 5, 6 } }, 
                                       { { 7, 8, 9 }, { 10, 11, 12 } } };

for more example http://msdn.microsoft.com/tr-tr/library/2yd9wwz4.aspx

geekido
  • 171
  • 5
0

C# is a static typed language, so its arrays can't contain elements of different types.

Your PHP array is a hash actually, so you can use .NET's hash, i.e. Dictionary.

But if your object has only two fixed fields "first_array" and "second_array", then it's better to use the simple class, like this:

public class MyObject
{
    public string[] first;
    public string[] second;
}

For simple pairs you can use tuples also:

var pair = new Tuple<string[], string[]>({"aa", "bb"}, {"cc", "dd"});
var first = pair.Item1;
var second = pair.Item2;
Mark Shevchenko
  • 7,937
  • 1
  • 25
  • 29