Associativity

   


If you look at the precedence of the multiplication and division operators in Table 7.1, you will find them to have equal precedence. Now, consider the following expression:


graphics/07infig05.gif

How does C# determine which operator is applied first? It cannot get any help from the precedence rules because the two operators have identical precedence. C# then resorts to the rules of associativity. An operator can either have left-to-right or right-to-left associativity. When two operators (such as (/) and (*)) with the same precedence can be applied to the same operand (such as 5 in the previous expression), left-to-right associativity will cause the leftmost operator to be applied first; whereas right-to-left associativity will cause the rightmost operator to be applied first. According to Table 7.1, all the binary operators presented here have left-to-right associativity. Consequently, C# will apply the division operator first in the previous expression, returning a result of 40.


graphics/07infig06.gif


   


C# Primer Plus
C Primer Plus (5th Edition)
ISBN: 0672326965
EAN: 2147483647
Year: 2000
Pages: 286
Authors: Stephen Prata

Similar book on Amazon

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net