home *** CD-ROM | disk | FTP | other *** search
/ PC Pro 2005 June (DVD) / DPPRO0605DVD.iso / dotNETSDK / SETUP.EXE / netfxsd1.cab / FL_ValueEnum_cs________.3643236F_FC70_11D3_A536_0090278A1BB8 < prev    next >
Encoding:
Text File  |  2002-01-08  |  8.8 KB  |  205 lines

  1. /*=====================================================================
  2.   File:      ValueEnum.cs
  3.  
  4.   Summary:   Demonstrates things you can do with ValueType/Enum types.
  5.  
  6. ---------------------------------------------------------------------
  7.   This file is part of the Microsoft .NET Framework SDK Code Samples.
  8.  
  9.   Copyright (C) Microsoft Corporation.  All rights reserved.
  10.  
  11. This source code is intended only as a supplement to Microsoft
  12. Development Tools and/or on-line documentation.  See these other
  13. materials for detailed information regarding Microsoft code samples.
  14.  
  15. THIS CODE AND INFORMATION ARE PROVIDED "AS IS" WITHOUT WARRANTY OF ANY
  16. KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
  17. IMPLIED WARRANTIES OF MERCHANTABILITY AND/OR FITNESS FOR A
  18. PARTICULAR PURPOSE.
  19. =====================================================================*/
  20. using System;
  21.  
  22. class App{
  23.     public static void Main () {
  24.         DemoValueTypes();
  25.         DemoReferenceTypes();
  26.         DemoEnums();
  27.         DemoFlags();
  28.     }
  29.  
  30.     // This is a value type because of 'struct'
  31.     struct Point {
  32.         Int32 x, y;
  33.         public Point(Int32 x, Int32 y) {
  34.             this.x = x;
  35.             this.y = y;
  36.         }
  37.         public override String ToString() {
  38.             return("(" + x + "," + y + ")");
  39.         }
  40.     }
  41.  
  42.     private static void DemoValueTypes() {
  43.         Console.WriteLine("Demo start: Demo of value types.");
  44.  
  45.         Point p1 = new Point(5, 10);
  46.         Point p2 = new Point(5, 10);
  47.         Point p3 = new Point(3, 4);
  48.         // What type is this valuetype & what is it derived from
  49.         Console.WriteLine("   The " + p1.GetType() + " type is derived from " + p1.GetType().BaseType);
  50.  
  51.         // Value types compare for equality by comparing the fields
  52.         Console.WriteLine("   Does p1 equal p1: " + p1.Equals(p1));   // True
  53.         Console.WriteLine("   Does p1 equal p2: " + p1.Equals(p2));   // True
  54.         Console.WriteLine("   Does p1 equal p3: " + p1.Equals(p3));   // False
  55.         Console.WriteLine("   p1={0}, p3={1}", p1.ToString(), p3.ToString());
  56.  
  57.         Console.WriteLine("Demo stop: Demo of value types.");
  58.     }
  59.  
  60.     // This is a reference type because of 'class'
  61.     class Rectangle {
  62.         Int32 x, y, width, height;
  63.         public Rectangle(Int32 x, Int32 y, Int32 width, Int32 height) {
  64.             this.x = x;
  65.             this.y = y;
  66.             this.width = width;
  67.             this.height = height;
  68.         }
  69.         public override String ToString() {
  70.             return("(" + x + "," + y + ")x("+width+","+height+")");
  71.         }
  72.         public override bool Equals(Object o) {
  73.             // Change the symantics of this reference type so that it is
  74.             // equal to the same type of object if the fields are equal.
  75.             Console.WriteLine("   In Rectangle.Equals method");
  76.             Rectangle r = (Rectangle) o;
  77.             return(r.x == x && r.y == y && r.width == width && r.height == height);
  78.         }
  79.     public override int GetHashCode() {
  80.             // Attempting a minor degree of "hash-ness" here
  81.             return ((x^y)^width)^height;
  82.     }
  83.     }
  84.  
  85.     private static void DemoReferenceTypes() {
  86.         Console.WriteLine("\n\nDemo start: Demo of reference types.");
  87.         Rectangle r = new Rectangle(1, 2, 3, 4);
  88.         // What type is this reference type & what is it derived from
  89.         Console.WriteLine("   The " + r.GetType() + " type is derived from " + r.GetType().BaseType);
  90.         Console.WriteLine("   " + r);
  91.  
  92.         // Reference types are equal if they refer to the same object
  93.         Console.WriteLine("   Is r equivalent to (1, 2, 3, 4): " + (r == new Rectangle(1, 2, 3, 4)));       // False
  94.         Console.WriteLine("   Is r equal to (1, 2, 3, 4): " + (r.Equals(new Rectangle(1, 2, 3, 4))));  // True
  95.         Console.WriteLine("   Is r equivalent to (1, 1, 1, 1): " + (r == new Rectangle(1, 1, 1, 1)));       // False
  96.         Console.WriteLine("   Is r equal to (1, 1, 1, 1): " + (r.Equals(new Rectangle(1, 1, 1, 1))));  // False
  97.  
  98.         Console.WriteLine("Demo stop: Demo of reference types.");
  99.     }
  100.  
  101.     // This is an enumerated type because of 'enum'
  102.     enum Color { 
  103.         Red   = 111,
  104.         Green = 222,
  105.         Blue  = 333
  106.     }
  107.     private static void DemoEnums() {
  108.         Console.WriteLine("\n\nDemo start: Demo of enumerated types.");
  109.         Color c = Color.Red;
  110.  
  111.         // What type is this enum & what is it derived from
  112.         Console.WriteLine("   The " + c.GetType() + " type is derived from " + c.GetType().BaseType);
  113.  
  114.         // What is the underlying type used for the Enum's value
  115.         Console.WriteLine("   Underlying type: " + Enum.GetUnderlyingType(typeof(Color)));
  116.  
  117.         // Display the set of legal enum values
  118.         Color[] o = (Color[]) Enum.GetValues(c.GetType());
  119.         Console.WriteLine("\n   Number of valid enum values: " + o.Length);
  120.         for (int x = 0; x < o.Length; x++) {
  121.             Color cc = ((Color)(o[x]));
  122.             Console.WriteLine("   {0}: Name={1,7}\t\tNumber={2}", x, 
  123.                 cc.ToString("G"), cc.ToString("D"));
  124.         }
  125.  
  126.         // Check if a value is legal for this enum
  127.         Console.WriteLine("\n   111 is a valid enum value: " + Enum.IsDefined(c.GetType(), 111));   // True
  128.         Console.WriteLine("   112 is a valid enum value: " + Enum.IsDefined(c.GetType(), 112));   // False
  129.  
  130.         // Check if two enums are equal
  131.         Console.WriteLine("\n   Is c equal to Red: " + (Color.Red == c));  // True
  132.         Console.WriteLine("   Is c equal to Blue: "  + (Color.Blue == c)); // False
  133.  
  134.         // Display the enum's value as a string using different format specifiers
  135.         Console.WriteLine("\n   c's value as a string: " + c.ToString("G"));   // Red
  136.         Console.WriteLine("   c's value as a number: " + c.ToString("D"));   // 111
  137.  
  138.         // Convert a string to an enum's value
  139.         c = (Color) (Enum.Parse(typeof(Color), "Blue"));
  140.         try {
  141.             c = (Color) (Enum.Parse(typeof(Color), "NotAColor")); // Not valid, raises exception
  142.         }
  143.         catch (ArgumentException) {
  144.             Console.WriteLine("   'NotAColor' is not a valid value for this enum.");
  145.         }
  146.  
  147.         // Display the enum's value as a string
  148.         Console.WriteLine("\n   c's value as a string: " + c.ToString("G"));   // Blue
  149.         Console.WriteLine("   c's value as a number: " + c.ToString("D"));   // 333
  150.  
  151.         Console.WriteLine("Demo stop: Demo of enumerated types.");
  152.     }
  153.  
  154.  
  155.     // This is an enumerated type of flags
  156.     [Flags] 
  157.     enum ActionAttributes { 
  158.         Read   =  1,
  159.         Write  =  2,
  160.         Delete =  4,
  161.         Query  =  8,
  162.         Sync   = 16 
  163.     }
  164.     private static void DemoFlags() {
  165.         Console.WriteLine("\n\nDemo start: Demo of enumerated flags types.");
  166.         ActionAttributes aa = ActionAttributes.Read 
  167.             | ActionAttributes.Write | ActionAttributes.Query;
  168.  
  169.         // What type is this enum & what is it derived from
  170.         Console.WriteLine("   The " + aa.GetType() + " type is derived from " + aa.GetType().BaseType);
  171.  
  172.         // What is the underlying type used for the Enum's value
  173.         Console.WriteLine("   Underlying type: " + Enum.GetUnderlyingType(aa.GetType()));
  174.  
  175.         // Display the set of legal enum values
  176.         ActionAttributes[] o = (ActionAttributes[]) Enum.GetValues(aa.GetType());
  177.         Console.WriteLine("\n   Number of valid enum values: " + o.Length);
  178.         for (int x = 0; x < o.Length; x++) {
  179.             ActionAttributes aax = ((ActionAttributes)(o[x]));
  180.             Console.WriteLine("   {0}: Name={1,10}\tNumber={2}", x, 
  181.                 aax.ToString("G"), ((ActionAttributes) aax).ToString("D"));
  182.         }
  183.  
  184.         // Check if a value is legal for this enum
  185.         Console.WriteLine("\n   8 is a valid enum value: " + Enum.IsDefined(aa.GetType(), 8));   // True
  186.         Console.WriteLine("   6 is a valid enum value: "   + Enum.IsDefined(aa.GetType(), 6));   // False
  187.  
  188.         // Display the enum's value as a string
  189.         Console.WriteLine("\n   aa's value as a string: " + aa.ToString("G"));   // Read|Write|Query
  190.         Console.WriteLine("   aa's value as a number: "   + aa.ToString("D"));         // 11
  191.  
  192.         // Convert a string to an enum's value
  193.         aa  = (ActionAttributes) (Enum.Parse(typeof(ActionAttributes), "Write"));
  194.         aa |= (ActionAttributes) (Enum.Parse(typeof(ActionAttributes), "Sync"));
  195.         Console.WriteLine("\n   aa's value as a string: " + aa.ToString("G"));   // Write|Sync
  196.         Console.WriteLine("   aa's value as a number: "   + aa.ToString("D"));         // 18
  197.  
  198.         Console.WriteLine("Demo stop: Demo of enumerated flags types.");
  199.     }
  200. }
  201.  
  202.  
  203.  
  204. ///////////////////////////////// End of File /////////////////////////////////
  205.