C#
  Home arrow C# arrow Page 5 - 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 - Logical Operators


    (Page 5 of 8 )

    C# has eight logical operators, which form logical expressions. Logical expressions are used with control structure statements to control the execution of your application's code. Logical expressions work on Boolean Arguments and produce Boolean value that will be used by the control structure statements like the if statement. C# logical operators include: the NOT operator (!), the XOR (^) or the Exclusive OR Operator, the Short Circuit And Operator (&&), the AND Operator (&), the Short Circuit OR Operator (||) and the OR Operator (|). Let's take a look at an example that use all of these operators.

    using System;
    namespace Operators
    {
    class Class1
    {
    static void Main(string[] args)
    {
    int x = 10;
    int y = 5;
    bool result;
    bool another;

    // using the NOT Operator
    Console.WriteLine("Using the ! Operator");
    result = true;
    Console.WriteLine("result before using ! equal to = {0}", result);
    result = !result;
    Console.WriteLine("result after using ! equal to = {0}", result);
    Console.WriteLine();
    Console.WriteLine("----------");
    // using the XOR Operator
    Console.WriteLine("using the ^ Operator");
    result = true;
    another = true;
    Console.WriteLine("using the ^ with 2 true arguments");
    Console.WriteLine("result ^ another equals to {0}", result ^ another);

    Console.WriteLine("using the ^ with 2 false arguments");
    result = false;
    another = false;
    Console.WriteLine("result ^ another equals to {0}", result ^ another);

    Console.WriteLine("using the ^ with 1 true and another false arguments");
    result = true;
    another = false;
    Console.WriteLine("result ^ another equals to {0}", result ^ another);

    // using the && operator
    Console.WriteLine();
    Console.WriteLine("----------");
    result = true;
    if(result && ReturnTrue())
    {
    Console.WriteLine("using the short circuit and (&&)"
    + " the ReturnTrue() method will be called only if the"
    + " result is true");
    }
    Console.WriteLine();
    Console.WriteLine("setting result to false");
    result = false;
    if(result && ReturnTrue())
    {
    Console.WriteLine("Do you see this line?");
    }
    Console.WriteLine("although that ReturnTrue() returns true it will not called" +
    " because result is false and the if block will not execute");

    // using the || operator
    Console.WriteLine();
    Console.WriteLine("---------");
    Console.WriteLine("using the || operator");
    Console.WriteLine("setting result to true");
    result = true;
    if(result || ReturnTrue())
    {
    Console.WriteLine("ReturnTrue() will not be called because" +
    " result = true");
    }

    Console.WriteLine("setting result to false and use the || again");
    result = false;
    if(result || ReturnTrue())
    {
    Console.WriteLine("ReturnTrue() is called because"
    + " result = false");
    }

    Console.WriteLine();
    Console.WriteLine("-----------");
    // using the full evaluation and operator &
    Console.WriteLine("setting result to true");
    result = true;
    Console.WriteLine("using the full evaluation and operator &");
    if(result & ReturnTrue())
    {
    Console.WriteLine("ReturnTrue() is called if result is true or false");
    }
    Console.WriteLine("-----------");
    Console.WriteLine("setting result to false");
    result = false;
    Console.WriteLine("using the full evaluation and operator &");
    if(result & ReturnTrue())
    {
    Console.WriteLine("ReturnTrue() is called if result is true or false");
    }

    Console.ReadLine();
    }

    static bool ReturnTrue()
    {
    Console.WriteLine("Inside the ReturnTrue method now");
    return true;
    }
    }
    }

    Yes it's a lot of code, but it's simple. The result that will be displayed in the console window will look like this:

    What happened? We have used some integer variables and some other Boolean variables in order to test the functionality that these operators offer us. The NOT operator just inverts the Boolean value of the variable; we have tested it with the result variable.

    The Exclusive OR Operator (^) as its name implies, is exclusive, which means that it will evaluate to true if only one of the two arguments is true. If both arguments are true it will evaluate to false, and if both arguments are false it will evaluate to false too.

    The Short Circuit And Operator (&&) is familiar to C++ developers (as is the Short Circuit Or ||). The Short Circuit And operator evaluates the second argument only if the first argument is true. If the first argument is false (this means that the expression will be evaluated to false, because with the And Operator both arguments must evaluates to true) it will not evaluate the second expression. The Short Circuit Or Operator (||) is similar, it will short circuit when the first argument evaluates to true. In other words, there's no need to evaluate the second expression, because with the OR Operator it will evaluate to true when one expression evaluates to true. And the above example illustrate this mechanism.

    The And (&) and Or (|) Operators are called full evaluation operators because they evaluate both of the expressions whether the first expression evaluates to true or false. Sometimes you need this behavior. In the above example, if(result & ReturnTrue()) you need the call to the method ReturnTrue() whether the first expression evaluated to true or false.

    More C# Articles
    More By Michael Youssef


     

    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