C# - Basics - Operators - Prefix and Postfix Increment-Decrement Operators
Related: C# Tutorial
This topic contains and tries to cover following subjects
- Brief explanation of Prefix and Postfix Increment-Decrement Operators in C#
- Difference between Prefix and Postfix Operators in C#
- Example to indicate difference of Prefix and Postfix Operators in C#
Articles tries to provide answer to following questions and issues:
- What is Prefix increment Operator in C#?
- What is Postfix increment Operator in C#?
- What is difference between prefix and Postfix Operators in C#?
- How to increment a value of variable in expression before or after it is evaluated in C#?
Brief explanation of Prefix and Postfix Operators in C#
Prefix Operators are the Operators which are added-placed before the variables to perform tasks. Postfix Operators opposite of it. They are placed after the variable. They are as follows:
Difference between Prefix and Postfix Operators in C#
What is difference between them. Well.. "++" Operator increases value by 1. When we add prefix before the variable, behind the scene, we are saying to compiler "first increase value then use it in expression". When we used Postfix, we say: "First use it in expression, than increase value by 1 of x".
To understand it better, lets demonstrate it in a IF condition expression.
x = 7;
if (++x == 7) //we placed prefix increment Operator to x, x will be increased first before comparison
(expression evaluation): if condition will not be executed as result...
if (x++ == 7) //we placed Postfix to increment Operator x, x will be increased after test condition
is checked. As result, in check time x is 7: that if condition will be executed....
Idea behind is to tell compiler perform arithmetic tasks within expressions according to the requirement of programmer. Since programmer may need to increase value of variable before the expression is evaluated or after it.
Example to indicate difference of Prefix and Postfix Operators in C#
Following example demonstrates how prefix and Postfix Operators work, and their behavior in expressions. Example is a console application, created with Visual Studio 2008 compiler.
static void Main(string  args)
//lets test PREFIX...
int x = 7;
if (++x == 8)//Returns TRUE: x will be incremented first, than comparison
Console.WriteLine("1st if block is executed, x is: " + x);
//lets test POSTFIX...
int y = 7;
if (y++ == 8)//Returns FALSE: y will compare first, and y will be considered
as 7 at compare time...
Console.WriteLine("2nd if block is executed, x is: " + x);
As it is seen in above application output, 1st if block is executed. 2nd is skipped by compiler.
Reference resources: http://msdn.microsoft.com/en-us/library/aa691363%28v=VS.71%29.aspx
Tags: Increment-Decrement Operators C#
Catagory layer 1: Programming
Catagory layer 2: Languages
Code layer 1: Languages
Code layer 2: csharp
Code layer 3: Basics
Code layer 4: operators
Code layer 5: prefix-and-postfix-increment-decrement-operators
Reader layer 1: beginner
Sector layer 1: C# programmer
Fuzzy layer 1: How increment and decrement operators work in C#?
Fuzzy layer 2: how to increase a value in a test condition before it is checked in C#?
Nodes: Languages\C# (1)