代码改变世界

C# Data Types

2012-05-21 19:08  libiver  阅读(552)  评论(0编辑  收藏  举报

     声明:欢迎任何人和组织转载本blog中文章,但必须标记文章原始链接和作者信息。

     本文链接: http://www.cnblogs.com/leezhm/archive/2012/05/21/2512079.html

     开拓进取的小乌龟———->cnBlogs 点滴点点滴滴 Blog

       In C-Sharp, there are two general kinds of Build-In Data Types: Value Type and Reference Type.So what is the difference between that two types?In brief, for a value type, a variable holds an actual value, but for a reference type, a variable holds a reference to the value.

        We can show all data types used a chart as below .C# defines 16 simple value types and more than 6 reference types.

                               

          Do you know what is the relationship between bool and System.Boolean, int and System.Int64? Sometimes you will find this tips “struct System.Boolean Represents a Boolean value.” when we move the cursor on to a bool variable.For example, here is a code snippet that show you how to used typeof and sizeof .

   1:  Console.WriteLine("{0}\t{1}\t\t{2}", "Type In C#", "Type In .NET", "Meaning");
   2:  Console.WriteLine("{0}\t\t{1}\t\t{2} bits", "char",    typeof(char),    sizeof(char) * 8);
   3:  Console.WriteLine("{0}\t\t{1}\t\t{2} bits", "byte",    typeof(byte),    sizeof(byte) * 8);
   4:  Console.WriteLine("{0}\t\t{1}\t\t{2} bits", "sbyte",   typeof(sbyte),   sizeof(sbyte) * 8);
   5:  Console.WriteLine("{0}\t\t{1}\t\t{2} bits", "short",   typeof(short),   sizeof(short) * 8);
   6:  Console.WriteLine("{0}\t\t{1}\t\t{2} bits", "ushort",  typeof(ushort),  sizeof(ushort) * 8);
   7:  Console.WriteLine("{0}\t\t{1}\t\t{2} bits", "int",     typeof(int),     sizeof(int) * 8);
   8:  Console.WriteLine("{0}\t\t{1}\t\t{2} bits", "uint",    typeof(uint),    sizeof(uint) * 8);
   9:  Console.WriteLine("{0}\t\t{1}\t\t{2} bits", "long",    typeof(long),    sizeof(long) * 8);
  10:  Console.WriteLine("{0}\t\t{1}\t\t{2} bits", "ulong",   typeof(ulong),   sizeof(ulong) * 8);
  11:  Console.WriteLine("{0}\t\t{1}\t\t{2} bits", "float",   typeof(float),   sizeof(float) * 8);
  12:  Console.WriteLine("{0}\t\t{1}\t\t{2} bits", "double",  typeof(double),  sizeof(double) * 8);
  13:  Console.WriteLine("{0}\t\t{1}\t\t{2} bits", "decimal", typeof(decimal), sizeof(decimal) * 8);
  14:  Console.WriteLine("{0}\t\t{1}\t\t{2} bits", "bool",    typeof(bool),    sizeof(bool) * 8);
  15:  Console.WriteLine("{0}\t\t{1}\t\t{2} bits", "null",    typeof(Nullable), "");
  16:  Console.WriteLine("{0}\t\t{1}\t\t{2} bits", "enum",    typeof(Enum),    ""); 
 
     Run it with “csc.exe example.cs /out example.exe” and you will get the output from the program like below. 
                                                         

           Clearly, according to the result, char == System.Char, byte == System.Byte, short == System.Int16 and so on.Type in C-Sharp and Type in .NET are corresponding.

All that types like System.Char, System.Int16 System.Boolean and so on are structs, they will be found on MSDN.So what am I meaning or what can we get basic on those result?Below is the trust.

                                      

               Ahaha, It is clearly!