0

I have these c# class and I need to iterate in a foreach loop, so it should be IEnumerable.

How can I implement IEnumerable interface?

Thankyou

Alessio


[Serializable]
public class Libro
{
    private string _titolo;
    private string _autore;
    private DateTime _dataCreazione;
    private string _tipologia;
    private List<Capitolo> _capitoli;
    private List<Personaggio> _personaggi;

    #region Intestazione
    public string Titolo { get => _titolo; set => _titolo = value; }

    public string Autore { get => _autore; set => _autore = value; }

    public DateTime DataCreazione { get => _dataCreazione; set => _dataCreazione = value; }

    public string Tipologia { get => _tipologia; set => _tipologia = value; }

    public List<Capitolo> Capitoli { get => _capitoli; set => _capitoli = value; }

    public List<Personaggio> Personaggi { get => _personaggi; set => _personaggi = value; }

    #endregion Intestazione

    #region Struttura

    [Serializable]
    public class Capitolo
    {
        private string _titoloCapitolo;
        private string _sinossiCapitolo;
        private string _scopoCapitolo;
        private string _conflittiCapitolo;
        private string _eventiCapitolo;
        private string _tempoCapitolo;
        private string _meteoCapitolo;
        private List<Luogo> _luoghiCapitolo;
        private List<Scena> _sceneCapitolo;
        private List<Personaggio> _personaggiCapitolo;


        public string TitoloCapitolo { get => _titoloCapitolo; set => _titoloCapitolo = value; }
        public string Sinossi { get => _sinossiCapitolo; set => _sinossiCapitolo = value; }
        public string ScopoCapitolo { get => _scopoCapitolo; set => _scopoCapitolo = value; }
        public string ConflittiCapitolo { get => _conflittiCapitolo; set => _conflittiCapitolo = value; }
        public string EventiCapitolo { get => _eventiCapitolo; set => _eventiCapitolo = value; }
        public string TempoCapitolo { get => _tempoCapitolo; set => _tempoCapitolo = value; }
        public string MeteoCapitolo { get => _meteoCapitolo; set => _meteoCapitolo = value; }
        public List<Scena> SceneCapitolo { get => _sceneCapitolo; set => _sceneCapitolo = value; }
        public List<Personaggio> PersonaggiCapitolo { get => _personaggiCapitolo; set => _personaggiCapitolo = value; }
        public List<Luogo> LuoghiCapitolo { get => _luoghiCapitolo; set => _luoghiCapitolo = value; }
    }


    #endregion Struttura

    #region Oggetti

    [Serializable]
    public class Personaggio
    {
        private string _nome;
        private string _cognome;
        private string _soprannome;
        private string _professione;
        private DateTime _dataNascita;
        private string _nazionalità;
        private string _cittaNascita;
        private string _cittaResidenza;
        private Aspetto _aspetto;

        public string Nome { get => _nome; set => _nome = value; }
        public string Cognome { get => _cognome; set => _cognome = value; }
        public string Soprannome { get => _soprannome; set => _soprannome = value; }
        public string Professione { get => _professione; set => _professione = value; }
        public DateTime DataNascita { get => _dataNascita; set => _dataNascita = value; }
        public string Nazionalità { get => _nazionalità; set => _nazionalità = value; }
        public string CittaNascita { get => _cittaNascita; set => _cittaNascita = value; }
        public string CittaResidenza { get => _cittaResidenza; set => _cittaResidenza = value; }
        public Aspetto Aspetto { get => _aspetto; set => _aspetto = value; }
    }

    [Serializable]
    public class Scena
    {
        private string _titoloScena;
        private string _primaStesura;
        private string _secondaStesura;
        private string _terzaStesura;
        private string _note;
        private List<Personaggio> _personaggiScena;

        public string TitoloScena { get => _titoloScena; set => _titoloScena = value; }
        public string PrimaStesura { get => _primaStesura; set => _primaStesura = value; }
        public string SecondaStesura { get => _secondaStesura; set => _secondaStesura = value; }
        public string TerzaStesura { get => _terzaStesura; set => _terzaStesura = value; }
        public string Note { get => _note; set => _note = value; }
        public List<Personaggio> PersonaggiScena { get => _personaggiScena; set => _personaggiScena = value; }
    }

    [Serializable]
    public class Luogo
    {
        private string _nomeLuogo;
        private string _coordinate;
        private string _note;

        public string NomeLuogo { get => _nomeLuogo; set => _nomeLuogo = value; }
        public string Coordinate { get => _coordinate; set => _coordinate = value; }
        public string Note { get => _note; set => _note = value; }
    }

    [Serializable]
    public class Aspetto
    {
        private string _occhi;
        private string _capelli;
        private string _altezza;
        private string _corporatura;
        private string _note;

        public string Occhi { get => _occhi; set => _occhi = value; }
        public string Capelli { get => _capelli; set => _capelli = value; }
        public string Altezza { get => _altezza; set => _altezza = value; }
        public string Corporatura { get => _corporatura; set => _corporatura = value; }
        public string Note { get => _note; set => _note = value; }
    }

    #endregion Oggetti


    #region Metodi

    #endregion Metodi
}

Pikoh
  • 7,582
  • 28
  • 53
Alessio
  • 25
  • 4
  • 8
    Question very unclear. An `IEnumerable` interface over what? And better use auto-generated properties instead of the current way you are doing - much more readable – Gilad Green Oct 16 '17 at 10:50
  • Your book class already has a list of chapters. Why not just use a `List` instead? – Wolfgang Radl Oct 16 '17 at 10:55
  • 1
    A `List` is already an `IEnumerable` so you can already iterate over all the `List` properties. – Corak Oct 16 '17 at 10:55
  • 2
    The [IEnumerable](https://msdn.microsoft.com/en-us/library/system.collections.ienumerable(v=vs.110).aspx) interface only have one method you need to implement. You can see a best practice demo code in the link in my comment. – Zohar Peled Oct 16 '17 at 10:56
  • By implementing its `MoveNext`-method? Your question is farily unclear. What exactly is your problem? Do you get an error? Moreover: which class should implement that interface? – MakePeaceGreatAgain Oct 16 '17 at 10:57
  • A List object is Enumerable. You only need IEnumerable under certain cases. – jdweng Oct 16 '17 at 11:02
  • btw, Why would you want to implement it anyway? The .Net class library contains many collections you can use out of the box, most if not all implements IEnumerable. Why write your own? – Zohar Peled Oct 16 '17 at 11:07

0 Answers0