Herong's Tutorial Notes On C# - Part B
Dr. Herong Yang, Version 2.02

Data Types

The "decimal" Type

C# introduces a very interesting new date type called: decimal.

Precision comparison:

  • float: 7 decimal digits
  • double: 15 decimal digits
  • decimal: 30 decimal digits

The following code will show you this:

class Decimals {
   public static void Main() {
      float f;
      double d;
      decimal m;
      f = 10.0f/3.0f;
      d = 10.0d/3.0d;
      m = 10.0m/3.0m;
      System.Console.WriteLine("Float 10/3 = {0}", f);
      System.Console.WriteLine("Double 10/3 = {0}", d);
      System.Console.WriteLine("Decimal 10/3 = {0}", m);
   }
}

Output:

Float 10/3 = 3.333333
Double 10/3 = 3.33333333333333
Decimal 10/3 = 3.3333333333333333333333333333

Simple Types

Simple types are predefined, primary, and value based types, like int, long, float, and double. Unlike any other languages, C# has predefined "struct" type for each simple type, and provides the simple type as alias to the "struct" type. For example, int is aliased to System.Int32; double is aliased to System.Double.

In Java, there is one predefined Class for to each primary type. For example, Integer is defined for int; Double is defined for double. But int is not an alias to Integer.

If int is aliased to System.Int32, which is struc, an variable of int may actually occupy more 32 bytes in memory. Can anyone approve that?

Some interesting features of simple types are shown in the following program:

using System;
class SimpleTypes {
   public static void Main() {
      double d;
      System.Double dd;

      d = 10.0d/3.0d;
      dd = d;
      Console.WriteLine("d = {0}", d);
      Console.WriteLine("d.ToString() = {0}",d.ToString());
      Console.WriteLine("1.000.ToString() = {0}", 1.000.ToString());
      Console.WriteLine("System.Single.MaxValue = {0}",float.MaxValue);
      Console.WriteLine("System.Double.MaxValue = {0}",double.MaxValue);
      Console.WriteLine("System.Decimal.MaxValue = {0}",decimal.MaxValue);
   }
}

Output:

d = 3.33333333333333
d.ToString() = 3.33333333333333
1.000.ToString() = 1
System.Single.MaxValue = 3.402823E+38
System.Double.MaxValue = 1.79769313486232E+308
System.Decimal.MaxValue = 79228162514264337593543950335

"1.000.ToString() = 1" shows that literals can be used as a variable to invoke its member method.

Type System Unification

In C#, all types, value based or reference based, predefined or user defined, are all derive from "object". The following program shows some interesting elements of this feature:

using System;
class Boxing {
   static void Main() {
      int i = 123;
      object o = i;
      int j = (int) o;
      Console.WriteLine("object = {0}",o.ToString());
   }
}

Output:

object = 123

Jagged Arrays

A Jagged array is an array that contains arrays as elements. Here are some examples:

using System;
class JaggedArrays {
   static void Main() {
      int[] a1 = new int[9]; // array of int
      int[,] a2 = new int[3,9]; // two dimensional array of int
      int[][] j1 = new int[9][]; // array of array of int
      int[][,] j2 = new int[9][,]; // array of two dimensional array
      int[,][] j3 = new int[3,9][]; // two dimensional array of array
      a1[5] = 5;
      a2[1,5] = 15;
      j1[0] = a1;
      j2[0] = a2;
      j3[0,0] = a1;
      Console.WriteLine("a1[5] = {0}", a1[5]);
      Console.WriteLine("a2[1,5] = {0}", a2[1,5]);
      Console.WriteLine("j1[0][5] = {0}", j1[0][5]);
      Console.WriteLine("j2[0][1,5] = {0}", j2[0][1,5]);
      Console.WriteLine("j3[0,0][5] = {0}", j3[0,0][5]);
   }
}

Output:

a1[5] = 5
a2[1,5] = 15
j1[0][5] = 5
j2[0][1,5] = 15
j3[0,0][5] = 5
Dr. Herong Yang, updated in 2002
Herong's Tutorial Notes On C# - Part B - Data Types