C#基础点滴

2009年4月25日星期六

C#基础点滴

1.命名空间的使用


      1.1 命名空间是.net中对代码逻辑意义上的划分,类似于java中的包,但是,命名空间不像包对应与物理上的文件路径。


      1.2 命名空间使用using关键字来引出。关于using的更多用法可以参考anytao的using使用。


      1.3 命名空间的嵌套使用及别名


 


namespace LevelOne
{
    using LT = LevelTwo; // 定义别名


    public class LevelOneClass
    {
        static string LevelOneName = "OneName";
       
        LevelOne.LevelTwo.LevelTwoClass twoClass1; //通过完全路径应用
        LevelTwo.LevelTwoClass twoClass2; // 由于LevelTwo在LevelOne内部,所以可以直接引用LevelTwo路径
        LT.LevelTwoClass twoClass3; // 通过别名方式引用
    }


    namespace LevelTwo
    {
        using LO = LevelOne; // 定义别名
        using LOC = LevelTwoClass;


        public class LevelTwoClass
        {
            static string LevelTwoName = "TwoName";


            LevelOne.LevelOneClass oneClass1; // 通过完全路径访问
            LevelOneClass oneClass2; // LevelOneClass在LevelTwo外层,可以直接访问
            LO.LevelOneClass oneClass3; // 通过别名访问


            LOC loc = new LOC(); // 通过别名使用类
        }
    }
}


 


2.运算符优先级


      优先级有高到底:


      ++,--(前缀);+,-(一元)


      *,/,%


      +,-


      =,*=,/=,%=,+=,-=


      ++,--(后缀)


 


3.循环中断


      C#提供了四个命令:


      1.break---立即终止循环


      2.continue---立即停止当前的循环,继续执行下一次循环


      3.goto---跳出循环到已知标记(不建议使用goto)


      4.return---跳出循环及其包含的函数


 


4. %运算符和/运算符


      %:为取余运算符,如 5 % 2 = 1            


      / :整除运算,如 5 % 2 = 2


 


5. 常见类型及类型转换


      1. .net类型分支类型和引用类型


      2. 值类型:简单类型,结构体,枚举;引用类型:字符串,类,接口等


      3. 简单类型:数值类型,布尔类型,字符类型


      4. 数值类型:整型和浮点型。整型包括:byte(非负),short,int,long 和 sbyte(含负数),ushort,uint,ulong


          其上类型在.net中有如下别名与之对应:System.Byte,System.Int16,System.Int32,System.Int64和System.SByte,System.UInt16,System.UInt32,System.UInt64


          浮点型包括:float,double,decimal,其别名分别为:System.Single,System.Double,System.Decimal


       5. 布尔类型和字符类型:bool(System.Boolean),char(System.Char,一个Unicode字符,两个字节)


       6. 字符串类型是引用类型,但是其使用过程又像是值类型。


       7. 字符类型和数值类型之间的转换


            char ch = 'a';
            ushort b = ch;
            Console.WriteLine("ch:{0}", ch);
            Console.WriteLine("b:{0}", b);


      8. 转义字符


            \a:警告声


            \r:回车:Console.Write("rrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrr\rkkkkkkkkkkkkkk");


                  结果:kkkkkkkkkkkkkkrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrr


            \n:换行:


      9. @对于字符串的作用


            @+字符串:忽略字符串中的转义字符,如:@"c:\window\a.txt" ,等效于 "c:\\window\\a.txt"


            甚至可以这样使用:string str = @"a   


                                          bcd"; 空格和回车直接作为字符串的一部分,否则回车必须使用/r/n;


      10. 类型转换:隐式转换和显式转换。


            隐式转换:使用=操作符,直接将小范围类型的变量赋值给大范围变量,或子类变量赋值给父类变量,转换操作有编译器完成。


            显示转换:当需要将大范围的变量转换为小范围的变量,或将父类赋值给子类,或者执行不同类型的转换(如:IntToString 或 StringToInt)


            Animal animal = new Animal();
            Bird bird = new Bird();


            animal = bird; // 隐式转换
            bird = (Bird)animal; // 显示转换


            byte b = 7;
            int myi = 33333;
            b = (byte)myi;// 显示转换 ----------该显示转换涉及到两个关键字checked和unchecked方法。


                  b = checked((byte)myi); 当转换溢出时会抛出异常,执行溢出检测,默认使用unchecked开关。
            myi = b; // 隐式转换
            b = checked((byte)myi);


            string str = "100";
            myi = Convert.ToInt32(str);// 显示转换


       


      11. 枚举


            1.枚举默认情况下是int类型,但可以是任意的数值类型,包括byte,sbyte,short,ushort.....


                  定义一个byte类型的枚举,枚举值可以相同


                      enum typeName : byte
                      {
                          value1 = 1,
                          value2 = 1,
                          value3 = 3,
                          value4 = 1
                      }


            2.枚举的几种类型转换操作 byte,enum,string


                  typeName myEnum = typeName.value1;
                  string myEnumStr = myEnum.ToString(); // EnumToString
                  byte myEnumByte = (byte)myEnum;  // EnumToByte
                  typeName myEnum2 = (typeName)Enum.Parse(typeof(typeName), "value2"); // StringToEnum
      


      12. ref,out,params区别


            ref:引用类型参数,通常方法调用使用的参数为值类型,当使用ref关键字显示作为参数前缀时,传递的是参数的引用,则其修改会直接反映到实参。引用类型参数使用前必须先初始化。


            out:输出类型参数,也是引用类型,但是与ref的区别是,使用前不必先初始化。允许多个输出参数,这样,对于需要多个返回值时,out方式是很有用的。


                  ref和out类型的参数,如果光只有这两个关键字作为区分,不能构成方法的重载。


            params:数组类型参数,一个方法只允许使用一个params关键字,并且不允许其他关键字一起使用。


0 评论:

发表评论