1.10 Performing Relational Operations

 <  Day Day Up  >  

You need to perform a relational comparison on two values or objects.


Technique

C# relational operators compare the actual values of value types but compare references to objects rather than what those objects contain. The format of a relational operation in C# is expr1 operator expr2 as in x == y . Listing 1.5 demonstrates different relational operations by comparing both value types and objects.

Listing 1.5 Using Relational Operators on Value and Reference Types
 using System; namespace _10_Relations {     /// <summary>     /// Summary description for Class1.     /// </summary>     class ComparingRelations     {         /// <summary>         /// The main entry point for the application.         /// </summary>          [STAThread]         static void Main(string[] args)         {             // compare 2 values for equality             int a = 12;             int b = 12;             Console.WriteLine( a == b );             Console.WriteLine( (object)a == (object)b );             // compare 2 objects which contains overloaded == operator             string c = "hello";             string d = "hello";             Console.WriteLine( (object) c==(object) d );             // compare 2 objects for equality             ClassCompare x = new ClassCompare();             ClassCompare y;             x.val = 1;             y = x;             Console.WriteLine( x == y );             // changing 1 object also changes the other             x.val = 2;             Console.WriteLine( y.val.ToString() );         }     }     class ClassCompare     {         public int val = 0;     } } 

Comments

Testing relationships between values or objects is an important concept in programming. It provides the foundation for program flow and is used extensively in human/computer interaction. You use relational operations for determining how two values or objects relate to one another to decide what the next step in your program flow will be. Some of these include testing to see whether two values are equal, testing whether one value is greater than another, or testing whether one object is the same as another object, a process known as type-testing .

One key thing you need to keep in mind when creating relational operations is the difference between testing two value types and testing two objects. When you test value types, such as two int values, you are testing the value that those two types contain. Compare it to testing two different objects, shown later in the code, in which testing them means you are testing to see whether they are the same objects. In other words, when you are comparing two objects, you are comparing their locations in memory. If they both point to the same location, then they are considered equal. To test this equality, the last test in the code changes the value in one object and the next line prints a member variable in the second object. When you run this code, you'll notice that even though the variable was changed in one object, the other changed as well because you modified the same memory location that the second one pointed to. Additionally, when creating classes, you might want to overload the Equals method so that any comparisons done on your object will compare its internal values rather than the object references.

One particular thing to watch for is a reference type overloading a relational operator. Overloading an operator, discussed later in this chapter, is a technique to change the behavior of a built-in operator so that it makes better sense given the context of the objects you are working with. As an example, code in Listing 1.5 evaluates to true even though the two objects being compared are different. The string class within the .NET Framework has overloaded the relational operators so that the comparison happens on the internal string rather than the objects themselves . Comparing the strings of two string objects makes more sense than comparing two actual string objects themselves.

 <  Day Day Up  >  


Microsoft Visual C# .Net 2003
Microsoft Visual C *. NET 2003 development skills Daquan
ISBN: 7508427505
EAN: 2147483647
Year: 2003
Pages: 440

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