# Answer to Question #61732 in C# for Kennedy Mutua

Question #61732

I need a piece of c# snippet explained

while (read_Email.Read())

{

email = read_Email.GetValue(i).ToString();

list_emails.Add(email);

i = i + 1 - 1;

}

what does the i = i + 1 - 1; mean? I know of +=, but i++,++i part is puzzling

while (read_Email.Read())

{

email = read_Email.GetValue(i).ToString();

list_emails.Add(email);

i = i + 1 - 1;

}

what does the i = i + 1 - 1; mean? I know of +=, but i++,++i part is puzzling

Expert's answer

Provided snippet of code reading from email values with index of i.

i = i + 1 - 1; // This one is doing nothing. (variable i doesn't change it's value at every iteration)

What about difference between i++ and ++i:

Each of them is iteration of variable.

i++ gives same result as i = i + 1 or i += 1;

++i gives same result with only one difference: CALCULATION of it will be done before variable i will be getted or returned.

This means for example:

void Main()

{

int i;

Console.WriteLine(GetIteration(out i));

Console.WriteLine(i);

}

public int GetIteration(out int i)

{

i = 4;

return i++;

}

Result of this code above will be:

4

5

--------------------------------------------------------------

void Main()

{

int i;

Console.WriteLine(GetIteration(out i));

Console.WriteLine(i);

}

public int GetIteration(out int i)

{

i = 4;

return ++i;

}

Result of this code above will be:

5

5

--------------------------------------------------------------

So, as you can see returned variable at second option was incremented before it was returned to invocation method.

i = i + 1 - 1; // This one is doing nothing. (variable i doesn't change it's value at every iteration)

What about difference between i++ and ++i:

Each of them is iteration of variable.

i++ gives same result as i = i + 1 or i += 1;

++i gives same result with only one difference: CALCULATION of it will be done before variable i will be getted or returned.

This means for example:

void Main()

{

int i;

Console.WriteLine(GetIteration(out i));

Console.WriteLine(i);

}

public int GetIteration(out int i)

{

i = 4;

return i++;

}

Result of this code above will be:

4

5

--------------------------------------------------------------

void Main()

{

int i;

Console.WriteLine(GetIteration(out i));

Console.WriteLine(i);

}

public int GetIteration(out int i)

{

i = 4;

return ++i;

}

Result of this code above will be:

5

5

--------------------------------------------------------------

So, as you can see returned variable at second option was incremented before it was returned to invocation method.

Need a fast expert's response?

Submit orderand get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

## Comments

## Leave a comment