List overwriting data on all positions

Posted by elvispt on Stack Overflow See other posts from Stack Overflow or by elvispt
Published on 2010-04-30T12:33:33Z Indexed on 2010/04/30 12:37 UTC
Read the original article Hit count: 219

Filed under:
|

I created a class with 3 fields:

class Gente
    {
        int _ID;
        string _nome, _sexo;


        public Gente()
        {
            _ID = 0;
            _nome = "";
            _sexo = "";
        }

        public int ID
        {
            set { _ID = value; }
            get { return _ID; }
        }

        public string Nome
        {
            set { _nome = value; }
            get { return _nome; }
        }

        public string Sexo
        {
            set { _sexo = value; }
            get { return _sexo; }
        }
    }

Then I declared a List from that class and an object from that class so that I can be able to add to the List.

List<Gente> Alunos = new List<Gente>();        

        Gente professor = new Gente();

The first time I do the Alunos.Add(professor); to the list it correctly sends the information to the 0 position.

But when I do it second time it overwrites everything on position 0 with the new data besides adding the new data to a new position, 1.

© Stack Overflow or respective owner

Related posts about c#

Related posts about lists