C# 类型 bool

类型总参考地址:http://msdn.microsoft.com/zh-cn/library/ya5y69ds(VS.80).aspx

Code
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace CSType
{
    
public class myBool
    {
        
public myBool()
        {
            
bool x = true;

            Console.WriteLine(x.GetType());
            
//输出:System.Boolean

            
// 类型转换使用Convert类.
            int x1 = Convert.ToInt16(x);
            Console.WriteLine(x1);
            
//输出1 

            x 
= false;
            x1 
= Convert.ToInt16(x);
            Console.WriteLine(x1);
            
//输出0

            
//x1 = (int)x; 错误转换方式.

            Console.WriteLine(x1.GetType());
            
//输出System.Int32 ,很奇怪哦。

            
// doesn't work
            
//if (x1)
            
//{
            
//    Console.WriteLine("True");
            
//}

            
// right way:
            if (x1 < 1)
            {
                Console.WriteLine(
"True");
            }
            
//也就是说表达式 :x1 < 1 ,会自动转换成bool型。

            Console.ReadLine();
        }
    }
}

posted @ 2009-01-06 14:43  无尽思绪  阅读(4665)  评论(0编辑  收藏  举报