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: