Wednesday, 28 August 2013

Enumerations


C# provides all the data types that are available in Java, and adds support for unsigned numerals and a new 128-bit high-precision floating-point type.
For each primitive data type in Java, the core class library provides a wrapper class that represents it as a Java object. For example, the Int32 class wraps theint data type, and the Double class wraps the double data type.
On the other hand, all primitive data types in C# are objects in the System namespace. For each data type, a short name, or alias, is provided. For instance,int is the short name for System.Int32 and double is the short form of System.Double.
The list of C# data types and their aliases is provided in the following table. As you can see, the first eight of these correspond to the primitive types available in Java. Note, however, that Java's boolean is called bool in C#.
Short Name
.NET Class
Type
Width
Range (bits)
byte
Unsigned integer
8
0 to 255
sbyte
Signed integer
8
-128 to 127
int
Signed integer
32
-2,147,483,648 to 2,147,483,647
uint
Unsigned integer
32
0 to 4294967295
short
Signed integer
16
-32,768 to 32,767
ushort
Unsigned integer
16
0 to 65535
long
Signed integer
64
-9223372036854775808 to 9223372036854775807
ulong
Unsigned integer
64
0 to 18446744073709551615
float
Single-precision floating point type
32
-3.402823e38 to 3.402823e38
double
Double-precision floating point type
64
-1.79769313486232e308 to 1.79769313486232e308
char
A single Unicode character
16
Unicode symbols used in text
bool
Logical Boolean type
8
True or false
object
Base type of all other types
string
A sequence of characters
decimal
Precise fractional or integral type that can represent decimal numbers with 29 significant digits
128
±1.0 × 10e−28 to ±7.9 × 10e28
Because C# represents all primitive data types as objects, it is possible to call an object method on a primitive data type. For example:
static void Main()
{
    int i = 10;
    object o = i;
    System.Console.WriteLine(o.ToString());
}    
This is achieved with the help of automatic boxing and unboxing. For more information, see Boxing and Unboxing (C# Programming Guide).
Both Java and C# provide the ability to declare a variable whose value is specified at compile time and cannot be changed at runtime. Java uses the final field modifier to declare such a variable, while C# uses the const keyword. In addition to const, C# provides the readonly keyword to declare variables that can be assigned a value once at runtime--either in the declaration statement or else in the constructor. After initialization, the value of a readonly variable cannot change. One scenario in which readonly variables are useful is when modules that have been compiled separately need to share data such as a version number. If module A is updated and recompiled with a new version number, module B can be initialized with that new constant value without having to be recompiled.
Enumerations, or enums, are used to group named constants similar to how they are used in C and C++; they are available in Java beginning in Version 1.5. In C#, enums are value types, and enum constants must be integral numeric values. The ToString method can be used to print out string representations of the named constants. The following example defines a simple Color enumeration in C#.
public enum Color
{
    Green,   //defaults to 0
    Orange,  //defaults to 1
    Red,     //defaults to 2
    Blue     //defaults to 3
}  
Integral values can also be assigned to enumerations as shown in the following enum declaration:
public enum Color2
{
    Green = 10,
    Orange = 20,
    Red = 30,
    Blue = 40
}
The following code example calls the GetNames method of the Enum type to display the available constants for an enumeration. It then assigns a value to an enumeration and displays the value.
class TestEnums
{
    static void Main()
    {
        System.Console.WriteLine("Possible color choices: ");

        //Enum.GetNames returns a string array of named constants for the enum. 
        foreach(string s in System.Enum.GetNames(typeof(Color)))
        {
            System.Console.WriteLine(s);
        }

        Color favorite = Color.Blue;

        System.Console.WriteLine("Favorite Color is {0}", favorite);
        System.Console.WriteLine("Favorite Color value is {0}", (int) favorite);
    }
}
Beginning in C# 3.0, enumerations can be extended with user-defined extension methods. For more information, see How to: Create a New Method for an Enumeration (C# Programming Guide).
Possible color choices:
Green
Orange
Red
Blue
Favorite Color is Blue
Favorite Color value is 3

No comments:

Post a Comment