namespace Empleados
{
    class Empleados
    {
        private int edad;

        static private int numeroemp = 0;




        public Empleados(int edad)
        {

            this.edad = edad;
            Empleados.numeroemp   ++;

 
        
        }

        public void mostrarInfo()
        {
            Console.WriteLine("el empleado tiene {0} años",edad );
            Console.WriteLine("Numero de empleados creados {0} ", Empleados.obtenernumeEmpledos() );
        }

        static void Main(string[] args)
        {
            
              int edad = 46;

     Empleados emp1 = new Empleados (edad);
     emp1.mostrarInfo();
     Empleados emp2 = new Empleados(45);
     emp2.mostrarInfo();


         
           
        }

        public static int obtenernumeEmpledos()
        {

            return numeroemp;
        }
    }
}

this mi question if I run this program the results are

el empleado 1 tiene 46 años
numero de empleados creados 1
el empleado 2 tiene 45 años
numero de empleados creados 2

its ok but if i change
this code

public Empleados(int edad)
        {

            this.edad = edad;
            Empleados.numeroemp   ++;

 
        
        }

for this

public Empleados(int edad)
        {

            this.edad = edad;
            Empleados.numeroemp  = + 1;

 
        
        }

the results are

el empleado 1 tiene 46 años
numero de empleados creados 1
el empleado 2 tiene 45 años
numero de empleados creados 1

why??

is no the same using ++ than + 1???
or i am wrong??

Recommended Answers

All 4 Replies

in your case it doesn't matter, which way you use to add 1. I suppose that you was talking about "+=" operator?
Let's see:
1.

Empleados.numeroemp++;

increasing numeroemp by 1.
2.

Empleados.numeroemp += 1;

which is the short version of this:

Empleados.numeroemp = Empleados.numeroemp + 1;

3.

Empleados.numeroemp  = + 1;

here you assign numeroemp by +1.

I was going to add that the two outputs are the same . ..

The difference is the

= +1
is the same as =1
where as

+= 1

will add one and store it.

Typos FTW

use this Empleados.numeroemp += 1;

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.