// ========================================================================
//
// Copyright (C) 2016-2017 MARC CHEVALDONNE
// marc.chevaldonne.free.fr
//
// Module : Program.cs
// Author : Marc Chevaldonné
// Creation date : 2016-09-27
//
// ========================================================================
using System;
using static System.Console;
namespace ex_022_003_Generics_syntaxe
{
///
/// C# propose 2 façons d'écrire du code "réutilisable" à travers des types différents :
/// l'utilisation de l'héritage ou l'utilisation du la généricité.
/// L'héritage propose la réutilisabilité à travers un type de base, alors que la généricité
/// se base sur un "patron" (template).
///
class Program
{
///
/// 1er exemple : un tableau de "choses" (inconnues à l'avance)
///
/// choses contenues par le tableau
public class Tableau
{
///
/// taille du tableau
///
public int Size
{
get { return mSize; }
}
int mSize = 0;
///
/// tableau d'object
///
T[] mData = new T[0];
///
/// ajoute un objet à la fin du tableau
///
/// objet à rajouter
public void Push(T objet)
{
mSize++;
T[] data = new T[mSize];
mData.CopyTo(data, 0);
data[mSize - 1] = objet;
mData = data;
}
///
/// indexer en lecture seule pour lire les éléments du tableau
///
/// index dans le tableau
/// la valeur à cet index
public T this[int index]
{
get
{
if (index < 0 || index >= mSize)
{
throw new IndexOutOfRangeException();
}
return mData[index];
}
}
}
public class Nounours
{
public Nounours(string nom)
{
Nom = nom;
}
public string Nom
{
get;
private set;
}
}
static void Main(string[] args)
{
//un tableau d'entiers
Tableau tab_int = new Tableau();
tab_int.Push(10);
tab_int.Push(20);
tab_int.Push(30);
tab_int.Push(40);
tab_int.Push(50);
for (int i = 0; i < tab_int.Size; i++)
{
WriteLine(tab_int[i]);
}
WriteLine("fin\n");
//un tableau de nounours
Tableau tab_nounours = new Tableau();
tab_nounours.Push(new Nounours("Hello Kitty"));
tab_nounours.Push(new Nounours("Pokemon"));
tab_nounours.Push(new Nounours("Pokoyo"));
tab_nounours.Push(new Nounours("Tigrou"));
tab_nounours.Push(new Nounours("PussInBoots"));
for (int i = 0; i < tab_nounours.Size; i++)
{
WriteLine(tab_nounours[i].Nom);
}
WriteLine("fin\n");
}
}
}