Comment : initialiser un tableau (Visual C#)
Mise à jour : novembre 2007
Cet exemple montre trois manières spécifiques d'initialiser différents types de tableaux : les tableaux unidimensionnels, multidimensionnels et en escalier.
Exemple
// Single-dimensional array (numbers).
int[] n1 = new int[4] {2, 4, 6, 8};
int[] n2 = new int[] {2, 4, 6, 8};
int[] n3 = {2, 4, 6, 8};
// Single-dimensional array (strings).
string[] s1 = new string[3] {"John", "Paul", "Mary"};
string[] s2 = new string[] {"John", "Paul", "Mary"};
string[] s3 = {"John", "Paul", "Mary"};
// Multidimensional array.
int[,] n4 = new int[3, 2] { {1, 2}, {3, 4}, {5, 6} };
int[,] n5 = new int[,] { {1, 2}, {3, 4}, {5, 6} };
int[,] n6 = { {1, 2}, {3, 4}, {5, 6} };
// Jagged array.
int[][] n7 = new int[2][] { new int[] {2,4,6}, new int[] {1,3,5,7,9} };
int[][] n8 = new int[][] { new int[] {2,4,6}, new int[] {1,3,5,7,9} };
int[][] n9 = { new int[] {2,4,6}, new int[] {1,3,5,7,9} };
Compilation du code
Copiez le code et collez-le dans la méthode Main d'une application console.
Programmation fiable
Les membres d'un tableau sont automatiquement initialisés avec la valeur initiale par défaut correspondant au type tableau, si le tableau n'est pas initialisé au moment de sa déclaration. Si la déclaration de tableau est un champ d'un type, lorsque le type est instancié, le tableau est défini avec sa valeur par défaut, null.
Voir aussi
Concepts
Tableaux et collections (Visual C# Express)