You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
129 lines
3.8 KiB
129 lines
3.8 KiB
// ========================================================================
|
|
//
|
|
// 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_002_Generics_EtPourquoiPasObject
|
|
{
|
|
/// <summary>
|
|
/// première solution (mauvaise) : faire un tableau d'Object
|
|
/// </summary>
|
|
class Program
|
|
{
|
|
/// <summary>
|
|
/// un tableau d'objet (donc n'importe quoi)
|
|
/// </summary>
|
|
public class ObjectArray
|
|
{
|
|
/// <summary>
|
|
/// taille du tableau
|
|
/// </summary>
|
|
public int Size
|
|
{
|
|
get { return mSize; }
|
|
}
|
|
int mSize = 0;
|
|
|
|
/// <summary>
|
|
/// tableau d'object
|
|
/// </summary>
|
|
object[] mData = new object[0];
|
|
|
|
/// <summary>
|
|
/// ajoute un objet à la fin du tableau
|
|
/// </summary>
|
|
/// <param name="objet">objet à rajouter</param>
|
|
public void Push(object objet)
|
|
{
|
|
mSize++;
|
|
object[] data = new object[mSize];
|
|
mData.CopyTo(data, 0);
|
|
data[mSize - 1] = objet;
|
|
mData = data;
|
|
}
|
|
|
|
/// <summary>
|
|
/// indexer en lecture seule pour lire les éléments du tableau
|
|
/// </summary>
|
|
/// <param name="index">index dans le tableau</param>
|
|
/// <returns>la valeur à cet index</returns>
|
|
public object this[int index]
|
|
{
|
|
get
|
|
{
|
|
if (index < 0 || index >= mSize)
|
|
{
|
|
throw new IndexOutOfRangeException();
|
|
}
|
|
return mData[index];
|
|
}
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// class Nounours
|
|
/// </summary>
|
|
public class Nounours
|
|
{
|
|
/// <summary>
|
|
/// constructeur
|
|
/// </summary>
|
|
/// <param name="nom">nom du nounours</param>
|
|
public Nounours(string nom)
|
|
{
|
|
Nom = nom;
|
|
}
|
|
|
|
/// <summary>
|
|
/// nom du nounours
|
|
/// </summary>
|
|
public string Nom
|
|
{
|
|
get;
|
|
private set;
|
|
}
|
|
}
|
|
|
|
|
|
static void Main(string[] args)
|
|
{
|
|
OutputEncoding = System.Text.Encoding.UTF8;
|
|
|
|
ObjectArray tab_int = new ObjectArray();
|
|
tab_int.Push(10);
|
|
tab_int.Push(20);
|
|
tab_int.Push(30);
|
|
tab_int.Push(40);
|
|
tab_int.Push(50);
|
|
WriteLine("début de tab_int");
|
|
for (int i = 0; i < tab_int.Size; i++)
|
|
{
|
|
WriteLine(tab_int[i]);
|
|
}
|
|
WriteLine("fin\n");
|
|
|
|
ObjectArray tab_nounours = new ObjectArray();
|
|
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"));
|
|
WriteLine("début de tab_nounours");
|
|
for (int i = 0; i < tab_int.Size; i++)
|
|
{
|
|
WriteLine((tab_nounours[i] as Nounours).Nom); // il faut caster !
|
|
}
|
|
WriteLine("fin\n");
|
|
}
|
|
}
|
|
}
|