C# Tutorials - Herong's Tutorial Examples - v3.32, by Herong Yang
Simple Types Are "struct" Types
This section describes simple types like 'int' and 'double'. Simple types are actually aliases of predefined 'struct' types which offers properties and methods.
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.
Back in C#, if "int" is aliased to System.Int32, which is a "struct" type, an variable of "int" may actually occupy more 32 bytes in memory because of "struct" overhead. Can anyone approve that?
Some interesting features of simple types are shown in the following program:
// SimpleTypes.cs // Copyright (c) 2006 HerongYang.com. All Rights Reserved. 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.
Table of Contents
Logical Expressions and Conditional Statements
►Simple Types Are "struct" Types
Visual C# 2010 Express Edition
C# Compiler and Intermediate Language
Compiling C# Source Code Files
MSBuild - Microsoft Build Engine
System.Diagnostics.FileVersionInfo Class
WPF - Windows Presentation Foundation