The Integral Data Types |
|
Introduction |
The C# language (actually the .NET Framework) treats each primitive data type as a class. In fact, each data type was created as a structure. Some characteristics are common to many of these structures while some other aspects are unique to some others. To support the routine operations of regular variables, each structure that represents a primitive type is equipped with some member variables and methods. |
With a value of a primitive type, at one time or another, you may need to convert the value of a variable from its type to a string. To support this, each structure of a primitive type is equipped with a method named ToString. This method is overloaded with various versions. One of the versions of this method takes no argument. The syntax of this method is: public override string ToString() Another version of this method takes as argument a string. This string holds an expression used to format the value of the variable that called the method. The syntax of this method is: public string ToString(string format) In Lesson 5, we saw how you can pass the desired string to format the value to be displayed.
In Lesson 5, we saw how to retrieve a value from the console and convert it to the desired value. To support this operation, each structure of a primitive data type is equipped with a static method named Parse. The syntaxes of this method are:
Here is an example of calling this method: using System; class Program { static int Main() { double value = 0; Console.Write("Enter a Value: "); value = double.Parse(Console.ReadLine()); Console.WriteLine("Value Entered: {0}", value); return 0; } } Here is an example of executing the program: Enter a Value: 245.85 Value Entered: 245.85 Press any key to continue . . . You can also parse a value that is not primarily known. To support values other than byte types, you can use another version of the Equals() method that takes as argument an object type. The syntaxes of this version of the method are:
When calling this method, if a bad value is passed to the Parse() method, for example if the user enters an invalid value to a double.Parse(Console.ReadLine()) call, the program produces an error (in Lesson 17, we will learn that the program throws an exception). To assist you with this type of problem, each structure of a primitive type is equipped with a method named TryParse. This method is overloaded with two versions that each returns a bool. One of the versions of this method takes two arguments: a string and a variable passed as an out reference. The syntaxes of this method are:
Based on this, if a double variable calls this method, the first argument is passed a string and the second argument is passed as an out double. This means that the second argument is returned from the method also. When the TryParse() method is called:
Here is another example of running the above program: Enter a Value: 506GH False: Value Entered = 0 Press any key to continue . . . Notice that the compiler didn't produce (throw) an error (an exception).
In Lesson 1, we saw that, when you declare a variable, the compiler reserves an amount of memory space preparing to store its value. As different variables have different requirements, some of them use less or more memory than others. When you declare a variable, the data type you specify allows the compiler to know how mush space would be needed to store the value of that variable. There is a minimum and a maximum values that can be stored in the memory space reserved for a variable. Based on this, a value such as 224855 can be stored in space reserved for an int variable but it is beyond the space reserved for a Byte or a short. To help you find out the minimum value that a data type can hold, each structure of a primitive type is equipped with a constant member named MinValue. In the same way, the maximum value that a data type can support is represented by a constant field named MaxValue. You can check these minimum and maximum with the following program: |
using System; class Program { static int Main() { Console.WriteLine("======================================================================="); Console.WriteLine("C# Type .NET Structure Minimum Maximum"); Console.WriteLine("======================================================================="); Console.WriteLine("char Char {0}\t\t\t{1}", char.MinValue, char.MaxValue); Console.WriteLine("-----------------------------------------------------------------------"); Console.WriteLine("byte Byte {0}\t\t\t{1}", byte.MinValue, byte.MaxValue); Console.WriteLine("-----------------------------------------------------------------------"); Console.WriteLine("sbyte SByte {0}\t\t\t{1}", sbyte.MinValue, sbyte.MaxValue); Console.WriteLine("-----------------------------------------------------------------------"); Console.WriteLine("short Int16 {0}\t\t\t{1}", short.MinValue, short.MaxValue); Console.WriteLine("-----------------------------------------------------------------------"); Console.WriteLine("ushort UInt16 {0}\t\t\t{1}", UInt16.MinValue, UInt16.MaxValue); Console.WriteLine("-----------------------------------------------------------------------"); Console.WriteLine("int Int32 {0}\t\t{1}", int.MinValue, int.MaxValue); Console.WriteLine("-----------------------------------------------------------------------"); Console.WriteLine("uint UInt32 {0}\t\t\t{1}", UInt32.MinValue, UInt32.MaxValue); Console.WriteLine("-----------------------------------------------------------------------"); Console.WriteLine("long Int64 {0}\t{1}", long.MinValue, long.MaxValue); Console.WriteLine("-----------------------------------------------------------------------"); Console.WriteLine("uint64 UInt64 {0}\t\t\t{1}", UInt64.MinValue, UInt64.MaxValue); Console.WriteLine("-----------------------------------------------------------------------"); Console.WriteLine("float Single {0}\t\t{1}", float.MinValue, float.MaxValue); Console.WriteLine("-----------------------------------------------------------------------"); Console.WriteLine("double Double {0}\t{1}", double.MinValue, double.MaxValue); Console.WriteLine("-----------------------------------------------------------------------"); Console.WriteLine("decimal Decimal {0} {1}", decimal.MinValue, decimal.MaxValue); Console.WriteLine("============================================================="); return 0; } }
This would produce: ======================================================================= C# Type .NET Structure Minimum Maximum ======================================================================= char Char ? ----------------------------------------------------------------------- byte Byte 0 255 ----------------------------------------------------------------------- sbyte SByte -128 127 ----------------------------------------------------------------------- short Int16 -32768 32767 ----------------------------------------------------------------------- ushort UInt16 0 65535 ----------------------------------------------------------------------- int Int32 -2147483648 2147483647 ----------------------------------------------------------------------- uint UInt32 0 4294967295 ----------------------------------------------------------------------- long Int64 -9223372036854775808 9223372036854775807 ----------------------------------------------------------------------- uint64 UInt64 0 18446744073709551615 ----------------------------------------------------------------------- float Single -3.402823E+38 3.402823E+38 ----------------------------------------------------------------------- double Double -1.79769313486232E+308 1.79769313486232E+308 ----------------------------------------------------------------------- decimal Decimal -79228162514264337593543950335 79228162514264337593543950335 ============================================================= Press any key to continue . . .
One of the most common operations performed on variables consists of comparing their values: to find out whether they are equal or to know whether one is higher than the other. These operations can be performed using the Boolean operators we reviewed in Lesson 8. The Boolean operations are part of the C# language. To formally implement them, each structure of a data type is equipped with a method named CompareTo that is overloaded with two versions. One of the implementations of the CompareTo() method compares a value or the value of a variable of the same type, with the variable that calls the method. This method takes one argument that is the same type as the variable that is calling it. The syntaxes of this method are:
The method returns an int value. For example, imagine you have two int variables named Variable1 and Variable2, you can call the CompareTo() method of the first variable to compare its value to that of the second variable. This would be done as in: int result = Variable1.CompareTo(Variable2); The end result would be as follows:
Here is an example: using System; class Program { static int Main() { int Variable1 = 248; int Variable2 = 72937; int result = Variable1.CompareTo(Variable2); Console.WriteLine("{0} compared to {1} produces {2}", Variable1, Variable2, result); return 0; } } This would produce: 248 compared to 72937 produces -1 Press any key to continue . . . Another version of the CompareTo() method allows you to compare the value of a variable with a variable whose type is not the same as the variable that called it. Since all types and classes of C# are based on object, this second version takes as argument an object object. The method return an int value. The syntax of this version is: public int CompareTo(object value) Notice that you can use the CompareTo() method to test for equality. Another method used to check the equality of two variables is the Equals() method. The Equals() method is overloaded in two versions and each takes one argument. The Equals() method returns a Boolean value. One of the versions of this method takes as argument a value of the same type as the variable that called it. The syntaxes of this version are:
The method compares the values of the variable that called it and the argument, as in bool result = Variable1.Equals(Variable2); The comparison is performed as follows:
Here is an example: using System; class Program { static int Main() { int Variable1 = 1407; int Variable2= 59266; bool result = value1.Equals(value2); Console.WriteLine("{0} = {1} produces {2}", Variable1, Variable2, result); return 0; } } This would produce: 1407 = 59266 produces False Press any key to continue . . . The other version of the Equals() method takes as argument an object value. The syntax of this version is: public override bool Equals(object obj) Here is an example: using System; class Program { static int Main() { int value1 = 824; object value2 = 824; bool result = value1.Equals(value2); Console.WriteLine("{0} = {1} produces {2}", value1, value2, result); return 0; } } This would produce: 824 = 824 produces True Press any key to continue . . . |
|
||
Home | Copyright © 2006-2016, FunctionX, Inc. | |
|