So I wrote a fonction called Afficher(int [,] m) which prints out in a table like manner the contents of a multidimensional array. The problem is that I would like the fonction to return a string instead of being just a void so that I can combine it to another string later on. Is there a possible way of doing this, maybe by using StringBuilder? Thanks.
static void Afficher(int[,] m)
{
string lignePleine = new string('-', m.GetLength(1) * 6 + 1);
for (int ligne = 0; ligne < m.GetLength(0); ++ligne)
{
Console.WriteLine(lignePleine);
Console.Write("|");
for (int colonne = 0; colonne < m.GetLength(1); ++colonne)
{
{
Console.Write($"{m[ligne, colonne],3} |");
}
}
Console.WriteLine();
}
Console.WriteLine(lignePleine);
Console.WriteLine();
}
public int[,] CreationMatrice()
{
Random generateur = new Random();
List<int> NewValeur = new();
for (int i = 1; i <= (Trajet.NB_LIGNES * Trajet.NB_COLONNES); i++)
{
listeValeurs.Add(i);
}
int nombreValeur = listeValeurs.Count;
for (int i = 0; i < nombreValeur; i++)
{
int indexRandom = generateur.Next(listeValeurs.Count);
NewValeur.Add(listeValeurs[indexRandom]);
listeValeurs.RemoveAt(indexRandom);
}
int index = 0;
for (int ligne = 0; ligne < Trajet.NB_LIGNES; ligne++)
{
for (int colonne = 0; colonne < Trajet.NB_COLONNES; colonne++)
{
matrice[ligne, colonne] = NewValeur[index];
index++;
}
}
return matrice;
}