Home

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.

 

Conversion to a String

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.

Parsing a String

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:

byte
public static byte Parse(string s)
sbyte
public static sbyte Parse(string s)
short
public static short Parse(string s)
ushort <=> UInt16
public static ushort Parse(string s)
int <=> Int32
public static int Parse(string s)
uint <=> UInt32
public static uint Parse(string s)
long <=> Int64
public static long Parse(string s)
unsigned long <=> uint64
public static ulong Parse(string s)

 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:

byte
public static byte Parse(string s)
sbyte
public static sbyte Parse(string s)
short
public static short Parse(string s)
ushort <=> UInt16
public static ushort Parse(string s)
int <=> Int32
public static int Parse(string s)
uint <=> UInt32
public static uint Parse(string s)
long <=> Int64
public static long Parse(string s)
unsigned long <=> uint64
public static ulong Parse(string s)

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:

byte
public static bool TryParse(string s, out byte result)
sbyte
public static bool TryParse(string s, out sbyte result)
short
public static bool TryParse(string s, out short result)
ushort <=> UInt16
public static bool TryParse(string s, out ushort result)
int <=> Int32
public static bool TryParse(string s, out int result)
uint <=> UInt32
public static bool TryParse(string s, out uint result)
long <=> Int64
public static bool TryParse(string s, out long result)
unsigned long <=> uint64
public static bool TryParse(string s, out ulong result)

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:

  • If the first argument passed to the method is valid, the method returns two values: true and the entered value is stored in the out argument
  • If the value of the first argument is invalid, the method returns false and the default value of the data type is stored in the out argument. For example, if the variable that called the method is on type int but the user entered the wrong value, the method returns two values: false and 0 (because 0 is the default value of an int. If the variable that called the method is on type char but the user entered a bad value, the method returns two values: false and an empty character (because the default value of a char is empty). 

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).

The Minimum and Maximum Values of a Primitive Type

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 . . .

Value Comparisons

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:

byte
public int CompareTo(byte value)
sbyte
public int CompareTo(sbyte value)
short
public int CompareTo(short value)
ushort <=> UInt16
public int CompareTo(ushort value)
int <=> Int32
public int CompareTo(int value)
uint <=> UInt32
public int CompareTo(uint value)
long <=> Int64
public int CompareTo(long value)
unsigned long <=> uint64
public int CompareTo(ulong value)

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:

  • If the value of Variable1 is greater than the value of Variable2, the method returns 1
  • If the value of Variable1 is less than the value of Variable2, the method returns -1
  • If the values of both variables are equal, the method returns 0

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:

byte
public bool Equals(byte obj)
sbyte
public bool Equals(sbyte obj)
short
public bool Equals(short obj)
ushort <=> UInt16
public bool Equals(ushort obj)
int <=> Int32
public bool Equals(int obj)
uint <=> UInt32
public bool Equals(uint obj)
long <=> Int64
public bool Equals(long obj)
unsigned long <=> uint64
public bool Equals(ulong obj)

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:

  • If the value of the first variable is the same as that of the second variable, the method returns true
  • If the values of the variables are different, the method returns false

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 FunctionX, Inc.