Increment Operator

Why Trust Techopedia

What Does Increment Operator Mean?

The increment operator, in C#, is a unary operator represented by the symbols "++". This operator is used in C# to increment the value of its operand by one. The type of the resulting value is the same as that of its operand. The operand in an increment operation can be a variable, a property access or an indexer access.

Advertisements

This operator is often used in loop constructs, such as the "for" loop, to increment the loop counter after executing the code within the loop. An increment operator is also used to change the pointer location by a value that is equal to the memory size of the pointer type used. Except for a pointer of type "void", the increment operator can be used for all other types of pointers. When used on a pointer, no exception is generated even when there is an overflow in the pointer’s domain.

Techopedia Explains Increment Operator

The increment operator comes in two forms:

  • Postfix: The operator appears after its operand. The increment operation occurs after the operand is evaluated and the result of this operation is the value of the operand before it is incremented.
  • Prefix: The operator appears before its operand. The increment operation occurs before the operand is evaluated and the result of this operation is the value of the operand after it is incremented.

For example, in the statement "v=i++", where the operator is in the postfix form, the value of "i" is assigned to "v" before the increment operation. In the statement "v =++i", where the operator is in the prefix form, the value of "i" is incremented first before being assigned to "v".

For numeric and enumeration types, the increment operator is predefined or built in. In the case of user-defined types, the increment operator can be overloaded to provide the customized implementation necessary for such types.

The increment operator can be used on a variable that can be set but cannot be used on a value (like the return value of a function).

Both the post- and pre-increment forms have to be carefully used as per the requirements and after understanding the implications of each of these forms. The precedence of the operation of the increment operator also has to be considered, because it plays a key role in evaluating an expression that contains an increment operator.

Advertisements

Related Terms

Margaret Rouse
Technology Specialist
Margaret Rouse
Technology Specialist

Margaret is an award-winning writer and educator known for her ability to explain complex technical topics to a non-technical business audience. Over the past twenty years, her IT definitions have been published by Que in an encyclopedia of technology terms and cited in articles in the New York Times, Time Magazine, USA Today, ZDNet, PC Magazine, and Discovery Magazine. She joined Techopedia in 2011. Margaret’s idea of ​​a fun day is to help IT and business professionals to learn to speak each other’s highly specialized languages.