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??

Dani AI

Generated

Short answer: the constructor version that "sets the field to positive one" does not add to the existing value — it overwrites it. That is why your first object shows 1 and the second also shows 1. and were right to point out that one form assigns a literal +1, while the increment/add forms increase the current value.

A small example to see how pre- and post-increment behave (different variable names from your post):

int a = 0;
int b = a++; // after: b == 0, a == 1

a = 0;
b = ++a;    // after: b == 1, a == 1

Use a standalone increment expression when you just want to add one to the existing shared counter; using assignment with a unary plus resets the counter each time the constructor runs.

Note about the static field: a static integer is shared by all instances and is initialized once. If the constructor always assigns the value 1, you will never see the accumulated count. If multiple threads can create instances concurrently, simple increments are not atomic and can lost updates — consider using thread-safe incrementing.

References and quick guidance:

Recommendation: keep the counter update as an increment (or use Interlocked.Increment in multithreaded code). This preserves the running total instead of overwriting it.

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.