Pregunta

Me estoy confundiendo al entender la matriz de múltiples dimensiones. Tengo tres datos (strFname, strLname, strMname).

Necesito poner estos datos en una matriz multidimensional. Podría haber un número n de filas. Pero para cada fila estos tres datos necesito agregar.

Cualquier referencia útil es bienvenida.

¿Fue útil?

Otros consejos

Eso podría ser un string[,]:

    string[,] data = new string[4, 3] {
        {"a","b","c"},
        {"d","e","f"},
        {"g","h","i"},
        {"j","k","l"}
    };

Sin embargo, le aconsejaría que cree un tipo con las propiedades esperadas y que tenga un List<T> de ese tipo, mucho más fácil de entender:

class Person
{
    public string FirstName { get; set; }
    public string LastName { get; set; }
    public string MiddleName { get; set; }
}
...
List<Person> people = new List<Person>();
people.Add(new Person {FirstName = "Fred", ... });

No creo que deba usar una matriz multidimensional aquí, creo que debería encapsular los tres valores en un tipo, algo como esto (suponiendo que haya interpretado los nombres correctamente):

public sealed class Name
{
    private readonly string firstName;
    private readonly string lastName;
    private readonly string middleName;

    // Consider changing the ordering here?
    public Name(string firstName, string lastName, string middleName)
    {
        this.firstName = firstName;
        this.lastName = lastName;
        this.middleName = middleName;
    }

    public string FirstName
    {
        get { return firstName; }
    }

    public string LastName
    {
        get { return lastName; }
    }

    public string MiddleName
    {
        get { return middleName; }
    }
}

Luego puede usar un List<Name> o un Name[], los cuales aclaran el propósito.

(Sí, esto es básicamente lo mismo que la respuesta de Marc, excepto que mi tipo es inmutable :)

Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top