jeudi 23 mai 2019

Is it possible to make a foreach loop for fields in a class using IEnumerable?

I was wondering if it would be possible to make the fields of a class enumerable so you could iterate over them with a for or foreach loop. I have seen a few versions describing how to make a structure containing other structures be enumerated in that way.

I am also aware I may have completely misread the MSDN article on the subject.

So my question is, is it possible to iterate over fields in a class using IEnumerable or a similar Interface?

I would like to do something like this:

private class Integers
{
    public int int1;
    public int int2;
    public int int3;
    public int int4;
    public int int5;
    public int int6;
    public int int7;
    public int int8;
}

foreach(int I in Integers){
    Console.WriteLine(I); 
}





Aucun commentaire:

Enregistrer un commentaire