C#
  Home arrow C# arrow Page 4 - 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 
Dedicated Servers 
Moblin 
JMSL Numerical Library 
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 / 31
    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 - Relational Operators


    (Page 4 of 8 )

    Relational operators are those operators that compare two operands and return a Boolean value indicating the relationship between them. We have six relational operators: the less than operator (<), the greater than operator (>), the greater than or equal (>=), the less than or equal (<=), the equal operator (==) and the not equal operator (!=). Don't confuse the equal operator with the assignment operator (=). In C# you can't write code like the following:

    bool z = x = y;

    In C++ you can, but the C# compiler will issue an error. The relational operators work exactly as you expect with the primitive types. Let's take a look at an example:

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

    result = (x > y);
    Console.WriteLine("x > y = {0}", result);
    result = (x < y);
    Console.WriteLine("x < y = {0}", result);
    result = (x <= y);
    Console.WriteLine("x <= y = {0}", result);
    result = (x >= y);
    Console.WriteLine("x >= y = {0}", result);
    result = (x == y);
    Console.WriteLine("x == y = {0}", result);
    result = (x != y);
    Console.WriteLine("x != y = {0}", result);
    Console.ReadLine();
    }
    }
    }

     
    The result that will be printed to the console window is:

    Exactly as we expected, x greater than y is equal to true, and so on, but what if we need to use the relational operators with objects? Let's see an example:

    using System;
    namespace Operators
    {
    class Class1
    {
    static void Main(string[] args)
    {
    Employee MichaelYoussef = new Employee();
    MichaelYoussef.Age = 22;
    Employee SteveAnderson = new Employee();
    SteveAnderson.Age = 22;

    Console.WriteLine("Michael == Steven? = {0}",
    MichaelYoussef == SteveAnderson);
    Console.ReadLine();
    }
    }

    class Employee
    {
    public int Age;
    }
    }

     
    Although both Michael and Steve are 22 years old, you will get the following result to the console window:

    With objects, equality is different from value-types, because the equal operator tests the value itself when used with Value-Types. It tests for REFERENCES when used with objects. We have false in the above code because both MichaelYoussef and SteveAnderson refer to different objects on the heap, although the value of the age field is the same in both the objects. We will learn how to handle this issue in the last article of the series, titled "C# Objects Manipulation."

    More C# Articles
    More By Michael Youssef


     

    C# ARTICLES

    - 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...
    - Color Transformation in C# GDI+ Programming
    - Exceptions in C#
    - Overriding versus Overloading
    - Value Types and Reference Types
    - Defining Member and Type Visibility
    - Managing Files in C#
    - Working with Windows Registry in C#
    - Lossless Image Resizing in C#
    - Lossless Image Converting in C#
    - Creating an RSS Feed with ASP.Net Written in...





    © 2003-2008 by Developer Shed. All rights reserved. DS Cluster 1 hosted by Hostway