作者:阿里西西  文章来源:http://www.alixixi.com/  更新时间:2005-12-15
     1。4 预定义类型(predefined types)
  
  c#提供了一系列预定义类型。它们与c/c++有不少相似的地方。预定义引用类型有object和string。
  object类型是所有其他类型的基础。
  
  预定义类型包括符号数、无符号数、浮点、布尔、字符和十进制数。符号数有:sbyte、short、
  int和long;无符号数有:byte、ushort、uint和ulong;浮点数有:float和double。
  
  布尔类型就像一个开关,只有两种状态:true或false。c#对布尔的要求比c/c++严格,与java类似。
  在c#中false不等于0,true也不等于1;false和true都是单独分离出来的一个值。学过c/c++的网友
  都知道:*/
  int i = 0;
  if (i = 0) { // bug: 应该是 (i == 0)
  ....
  }
  /* 是没有问题的。但在c#中会引发一个编译错误(error cs0029: cannot implicitly convert
  type 'int' to 'bool')。当然,这样牺牲了一点没有必要的灵活性。我们再也不能这样:*/
  string str;
  ....
  if(str = console.readline()) {
   console.writeline("your comments are: {0}",str);
  ....
  /* 而必须:*/
  using system;
  class booltest
  {
   static void main() {
   string str = console.readline();//也可以:string str;
   if(str == "") // if((str = console.readline()) == "")
   console.writeline("i can't read your comments. please tell me something! o.k.?");
   else
   console.writeline("your comments are: {0}",str);
   }
  }
  /*
  我抄了一张预定义类型的简表供大家参考。
  
  type description examples
  
  object the ultimate base type of all other types object o = new stack();
  
  string string type; a string is a sequence of string s = "hello";
   unicode characters
  
  sbyte 8-bit signed integral type sbyte val = 12;
  
  short 16-bit signed integral type short val = 12;
  
  int 32-bit signed integral type int val = 12;
  
  long 64-bit signed integral type long val1 = 12;
   long val2 = 34l;
  
  byte 8-bit unsigned integral type byte val1 = 12;
   byte val2 = 34u;
  
  ushort 16-bit unsigned integral type ushort val1 = 12;
   ushort val2 = 34u;
  
  uint 32-bit unsigned integral type uint val1 = 12;
   uint val2 = 34u;
  
  ulong 64-bit unsigned integral type ulong val1 = 12;
   ulong val2 = 34u;
   ulong val3 = 56l;
   ulong val4 = 78ul;
  
  float single-precision floating point type float value = 1.23f;
  
  double double-precision floating point type double val1 = 1.23
   double val2 = 4.56d;
  
  bool boolean type; a bool value is either bool value = true;
   true or false
  
  char character type; a char value is a unicode char value = 'h';
   character
  
  decimal precise decimal type with 28 significant digits decimal value = 1.23m;
  
  你也可以自定义自己的预定义类型,可以这样:*/
  using system;
  struct digit
  {...}
  class test
  {
   static void testint() {
   int a = 1;
   int b = 2;
   int c = a + b;
   console.writeline(c);
   }
   static void testdigit() {
   digit a = (digit) 1;
   digit b = (digit) 2;
   digit c = a + b;
   console.writeline(c);
   }
   static void main() {
   testint();
   testdigit();
   }
  }
  /*
  这一节有点沉闷。:(
  
  

网友评论
相关搜索
阿里西西Baidu.com搜索