Multidimensional array in C # equal to PHP?

0

First time here in this forum, I usually use stack overflow but come on.

My question is about array in C #: I'm more familiar with PHP and in php we can name our array indexes for example:

$array = ["nome"=>"Pedro", "Idade"=>18];

My question is, is it possible to name the indexes of an array in C #?

If not, taking advantage of the post, question 2: in an array built like this:

string[,] array = new string[,]
{
   {"nome0", "idade1"},
   {"nome0", "idade1"}
};

How would I do in a foreach to always pick up the corresponding name and age at each pass?

    
asked by anonymous 20.08.2016 / 05:53

2 answers

6

I'll show you several ways to do all this, this one scraping by being more of a question.

We started by creating a dictionary that is equivalent to PHP associative array .

var dict = new Dictionary<string, string> {["nome"] = "Pedro", ["Idade"] = "18"};
foreach (var item in dict) {
    WriteLine($"{item.Key} => {item.Value}");
}

Since C # is a typed language, it is necessary to indicate the type of key and value. So it can not mix types like it was done in the PHP example. It has a way of solving this, but it is not usually suitable in C #. Just use the type object that accepts anything. It loses the security of language types. So:

var dict2 = new Dictionary<string, object> {["nome"] = "Pedro", ["Idade"] = 18};
foreach (var item in dict2) {
    WriteLine($"{item.Key} => {item.Value}");
}

There are other ways to get a similar effect, but again, better avoid.

Using a multi-dimensional array can iterate through all items without worrying about the dimension. Remembering that typing counts here too, but the question example seems to already consider this.

var array = new string[,] {
   {"nome0", "idade1"},
   {"nome1", "idade2"}
};
foreach (var item in array) {
    WriteLine($"{item}");
}

But if you want to scan the array considering the dimensions you can not use foreach . Here's how:

for (var i = 0; i < array.GetLength(0); i++) { //pega o tamanho da dimensão 0
    for (var j = 0; j < array.GetLength(1); j++) { //pega o tamanho da dimensão 1
        WriteLine($"{array[i, j]}");
    }
}

You can even abstract access and use foreach . For this I would need to create a class with an iterator method that conveniently returns the dimensions to use in foreach . I will not give an example because I think this solution is not the most appropriate.

In C # it's almost always more interesting to use jagged array array than an array multidimensional.

var jaggedArray = new string[2][] {new string[2] {"nome0", "idade1"}, new string[2] {"nome1", "idade2"}};
foreach (var subArray in jaggedArray) {
    foreach (var item in subArray) {
        WriteLine($"{item}");
    }
}

But it looks like you really want to create a class with a specific object and create a list. In PHP we use array for everything. C # is more optimized and has a mechanism for every need. For example a list seems to be better than an array . And that the other dimension seems to be just a different way to use the associative array that is common in PHP (although the language accepts classes as well, but by nature it is not usually used for this).

var listaPessoas = new List<Pessoa> {
    new Pessoa {Nome = "Pedro", Idade = 18},
    new Pessoa {Nome = "João", Idade = 15}
};
foreach (var item in listaPessoas) {
    WriteLine($"{item.Nome} => {item.Idade}");
}

public class Pessoa {
    public string Nome { get; set; }
    public int Idade { get; set; }
}

In some cases a struct may be better than a class.

In fact it would be possible to use a tuple to avoid creating a class that is not always desirable (I do not think that is the case) until version 6 was not convenient. In C # 7 it is much more convenient in some cases other than that.

See running on dotNetFiddle .

Obviously I've used more modern techniques throughout the code.

Documentation:

20.08.2016 / 13:15
1

Hello, good morning.

For your second question, you can do a foreach like this:

 class Program
{
    static void Main(string[] args)
    {
        string[,] array = new string[,]
       {
           {"nome0", "idade1"},
           {"nome1", "idade2"}
       };

        foreach (var item in array)
        {
            Console.WriteLine(item);
        }
        Console.ReadKey();
    }
}

For the first question, I do not know if I understand, but if it is a list with key and value, you could do this:

class Program
{
    static void Main(string[] args)
    {
        Dictionary<string, string> array = new Dictionary<string, string>();
        array.Add("nome", "Joao");
        array.Add("nome2", "Akame");

        foreach (var item in array)
        {
            Console.WriteLine(item.Value);
        }
        Console.ReadKey();
    }
}

To learn more about dictionary: link

    
20.08.2016 / 08:42