1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66
|
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace VariableGlobale
{
class Program
{
static void Main(string[] args)
{
List<B> list = new List<B>(4);
for (int i = 0; i < 3; i++)
{
list.Add(new B(i));
}
foreach (B b in list)
{
foreach (A a in b.list)
{
Console.WriteLine(a.ToString());
}
Console.WriteLine();
}
Console.ReadKey(true);
}
}
class A
{
public int Id;
public A(int id)
{
Id = id;
}
public override string ToString()
{
// Ici, je veux pouvoir retrouver aussi le Id du B qui contient le A.
// return string.Format("{0} - {1}", parent.Id.ToString(), Id.ToString());
return Id.ToString();
}
}
class B
{
public List<A> list;
public int Id;
public B(int id)
{
Id = id;
list = new List<A>(id);
for (int i = 0; i <= Id; i++)
{
list.Add(new A(i));
}
}
}
} |
Partager