C#
  Home arrow C# arrow Page 8 - Behind the Scenes Look at C#: Operators
ASP Free Forums 
.NET  
ASP  
ASP Code  
ASP.NET  
ASP.NET Code  
BrainDump  
C#  
Code Examples  
Database  
Database Code  
IIS  
Microsoft Access  
MS SQL Server  
Visual Basic.NET  
Windows Scripting  
Windows Security  
XML  
ASP Web Hosting  
ASP.NET Web Hosting 
Mobile Linux 
App Generation ROI 
Windows Web Hosting
 
IBM® developerWorks 
Sun Developer Network 
Weekly Newsletter
 
Developer Updates  
Free Website Content 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us Get Paid 
Request Media Kit
Contact Us 
Site Map 
Privacy Policy 
Support 
 USERNAME
 
 PASSWORD
 
 
  >>> SIGN UP!  
  Lost Password? 
C#

Behind the Scenes Look at C#: Operators
By: Michael Youssef
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 4 stars4 stars4 stars4 stars4 stars / 32
    2005-06-29

    Table of Contents:
  • Behind the Scenes Look at C#: Operators
  • Arithmetic Operators
  • The Unary Arithmetic Operators
  • Relational Operators
  • Logical Operators
  • Assignment Operators
  • The Ternary Operator
  • Operator Precedence

  • Rate this Article: Poor Best 
      ADD THIS ARTICLE TO:
      Del.ici.ous Digg
      Blink Simpy
      Google Spurl
      Y! MyWeb Furl
    Email Me Similar Content When Posted
    Add Developer Shed Article Feed To Your Site
    Email Article To Friend
    Print Version Of Article
    PDF Version Of Article
     
     
    ADVERTISEMENT


    Behind the Scenes Look at C#: Operators - Operator Precedence


    (Page 8 of 8 )

    It's very common to have more than one operator in your expressions. For example, here is an expression with several operators: 2 + 3 * 4. What's the result of this expression? If we add 2 and 3 then multiply by 4, the product will be 20. If we multiply 3 by 4 and then add 2 the product will be 14. Which one is true? Logically both of them are true because 2 + 3 = 5 multiplied by 4 = 20 and 3 multipled by 4 = 12 added to 2 = 14. So we have two operators in the expression, but we need a way to tell which one will be evaluated first. There are two procedures for doing this. The first way is to use parentheses () to tell the C# compiler that you want to evaluate the enclosed expression first, as in the following code example:

    namespace Operators
    {
    class Class1
    {
    static void Main(string[] args)
    {
    Console.WriteLine((2 + 3) * 4);
    Console.WriteLine(2 + (3 * 4));
    Console.ReadLine();
    }
    }
    }

    The result in the console window will be:

    Our problem has been solved with the parentheses. The parentheses just determine the order of evaluation for the operators in the expression, so the enclosed expression evaluates first and then the outer expression. So for this example, (2 + 3) = 5 then multiplied by 4 = 20 because the compiler evaluates the enclosed expression first and multiplies the result.

    The process of the order of evaluation of operators is called operators precedence. If you don't want to use the parentheses, and would rather rely on the second procedure, it is the default C# operator precedence. The C# compiler evaluates operators based on the following table:

    The IntelliSense Operator .

    The Parentheses Operator ()

    Array Index Operator []
    The Post Increment Operator ++
    The Post Decrement Operator --
    The new Operator
    The typeof Operator
    The checked Operator
    the unchecked Operator
    The unary plus +
    The unary minus -
    the unary !
    The unary ~
    The Pre Increment Operator ++
    The Pre Decrement Operator --
    The multiplication Operator *
    The division Operator /
    The Module Operator %
    The Addition Operator +
    The Subtraction Operator -
    The Shift-Left Operator <<
    The Shift-Right Operator >>
    The Less Than Operator <
    The Less Than or Equal Operator <=
    The Greater Than Operator >
    The Greater Than or Equal Operator >=
    The is Operator
    The Equality Operator ==
    The Is Not Equal Operator !=
    The And Operator &
    The XOR Operator ^
    The Or Operator |
    The Short-Circuit And Operator &&
    The Short-Circuit Or Operator ||
    The Ternary Operator ?:
    The Assignment Operators as following

    =,*=,/=,+=,-=,<<=,>>=,&=,^= and |=

    Using this table you can tell that the C# compiler will do the following:

    1. Multiply 4 by 3 because the * operator has a higher precedence than the + operator (the operator precedence table lists the C# operators from the highest to lowest precedence).

    2. Add the product to 2, which equals 14, and that's all.

    In the next article we will examine the concepts behind operator overloading.


    DISCLAIMER: The content provided in this article is not warranted or guaranteed by Developer Shed, Inc. The content provided is intended for entertainment and/or educational purposes in order to introduce to the reader key ideas, concepts, and/or product reviews. As such it is incumbent upon the reader to employ real-world tactics for security and implementation of best practices. We are not liable for any negative consequences that may result from implementing any information covered in our articles or tutorials. If this is a hardware review, it is not recommended to open and/or modify your hardware.

     

    C# ARTICLES

    - C# and XML
    - Pointers and Arrays in C#
    - C# 3.0 Extension Methods
    - Overloading Operators in C#
    - Iterators and Nullable Types
    - Patterns and Iterators in C#
    - C# Exceptions
    - Methods in C#
    - Delegates and Events in C#
    - Advanced C#
    - Working with Regular Expressions in C#
    - Sending Simple E-Mail in C#
    - Building C# Comparable Objects: IComparable ...
    - Color Transformation Applications in C# GDI+...
    - Performing Color Transformation Operations i...





    © 2003-2008 by Developer Shed. All rights reserved. DS Cluster 3 hosted by Hostway
    Stay green...Green IT