c#入门学习笔记

  1. Hello World

     //打印语句
     Console.WriteLine("Hello World");
     //暂停
     Console.ReadKey();
    
  2. 数据类型
    1.值类型 byte,char,short,int,long,bool,decimal,float,double,sbyte,uint,ulong,ushort
    2.引用类型: 储存的不是值实际数据而是一个内存地址 object、dynamic 和 string。
    3.对象类型:Object 类型检查是在编译时发生的
    4.动态类型: 可以存储任何类型的值在动态数据类型变量中,类型检查在运行时
    5.字符串(String)类型
    6.指针类型(Pointer types)

  3. 类型转换

    1. 隐式转换
    2. 显式转换
    3. Toxxx方法
  4. 变量

  5. 常量

    1. 整数常量
      • 整数常量可以是十进制、八进制或十六进制的常量。前缀指定基数:0x 或 0X 表示十六进制,0 表示八进制,不带前缀则默认表示十进制。
      • 整数常量也可以带一个后缀,后缀是 U 和 L 的组合,U 表示无符号整数(unsigned),L 表示长整数(long)。后缀可以是大写,也可以是小写,U 和 L 的顺序任意。
    2. 浮点常量(小数必须包含整数)
    3. 字符常量
    4. 字符串常量
    5. 定义常量
  6. 运算符

    1. 算数运算符
    2. 关系运算符
    3. 逻辑运算符
    4. 位运算符
    5. 赋值运算符
    6. 其他运算符
      1. sizeof():数据类型的大小
      2. typeof():返回 class的类型。
      3. &: 返回变量的地址
      4. *:变量的指针
      5. ?: :三元表达式
      6. is:判断对象是否为某一类型
      7. as: 强制转换,即使失败也不会抛出异常
  7. 判断:

    1. if
    2. switch
  8. 循环:

    1. while循环
    2. for/foreach
    3. do…while
  9. 封装:

    1. public:允许一个类将其成员变量和成员函数暴露给其他的函数和对象。任何公有成员可以被外部的类访问
    2. private:允许一个类将其成员变量和成员函数对其他的函数和对象进行隐藏。只有同一个类中的函数可以访问它的私有成员。即使是类的实例也不能访问它的私有成员。
    3. protected:该类内部和继承类中可以访问。
    4. internal:同一个程序集的对象可以访问。
    5. protected internal:3 和 4 的并集,符合任意一条都可以访问。
  10. 可空类型

    • //默认值为0
      int a;
      //默认值为null
      int? b=123;
      //如果b为null就赋值2否则c=b
      int c=  b?? 2;
      Console.WriteLine("c的值为{0}", c);
      Console.ReadLine();
      
  11. 数组

    //初始化数组逐个赋值
    int[] arr = new int[10];
    arr[0] = 12312;
    //初始化数组并赋值
    int[] arr1 = { 321, 312, 12312, 12312312, 12312312 };
    //使用for循环赋值
    for (int i = 0; i < 10; i++)
    {
    arr[i] = i + 100;
    }
    //使用forEach取值
    foreach (int i in arr)
    {
    Console.WriteLine("元素的值为{0}", i);
    }
    
  12. 结构体

    struct Books{
    public string id;
    public string name;
    public string price;
    }
    static void Main()
    {
    Books book1;
    book1.id = "123";
    book1.name = "aaa";
    book1.price = "23131";
    Console.WriteLine("书信息:书id{0},书名{1},书价格{2}",book1.id,book1.name,book1.price);
    Console.ReadLine();
    }
    
    • 类与结构的不同点
      1. 类是引用类型,结构是值类型
      2. 结构不支持继承
      3. 结构不能声明默认的构造函数
      4. 结构体中无法实例属性或赋初始值
    • 类与结构的选择
      1. 当我们描述一个轻量级对象的时候,结构可提高效率,成本更低。数据保存在栈中,访问速度快
      2. 当堆栈的空间很有限,且有大量的逻辑对象或者表现抽象和多等级的对象层次时,创建类要比创建结构好一些;
  13. 枚举

    //枚举列表中的每个符号代表一个整数值,一个比它前面的符号大的整数值,可自定义每个符号
    enum Days { Sun, Mon, Tue, Wed, Thu, Fri, Sat };
    static void Enum()
    {
    int a = (int)Days.Wed;
    Console.WriteLine(a);
    Console.ReadLine();
    }
    
  14. 析构函数

    class Test
    {
        string id;
        public Test()
        {
            Console.WriteLine("构造函数");
        }
        ~Test()
        {
            Console.WriteLine("析构函数");
        }
        static void Main()
        {
            Test test = new Test { id = "123" };
            Console.WriteLine("id为{0}", test.id);
            Console.ReadLine();
        }
    
    }
    
  15. 多态性

    1. 通过在类定义前面放置关键字 sealed,可以将类声明为密封类。当一个类被声明为 sealed 时,它不能被继承。抽象类不能被声明为 sealed。
    2. 当有一个定义在类中的函数需要在继承类中实现时,可以使用虚方法。虚方法是使用关键字 virtual 声明的。虚方法可以在不同的继承类中有不同的实现。
  16. 运算符的重载

    class Test2
        {
            public int length;
            //运算符重载  
            public static Test2 operator +(Test2 a, Test2 b)
            {
                Test2 test = new Test2 { length = a.length - b.length };
                return test;
            }
            static void Main(string[] args)
            {
                Test2 test = new Test2 { length = 12 };
                Test2 test1 = new Test2 { length = 213 };
                Test2 t = test + test1;
                Console.WriteLine("t的值{0}", t.length);
                Console.ReadLine();
            }
        }
    
  17. 预处理器指令

    1. define 预处理器

    2. 条件指令
  18. 异常处理

    1. try catch finally
    2. 常见异常:
      1. IO异常
      2. 空对象
      3. 类型转换
      4. 除以0
  19. 文件的输入与输出

     //字节流读取文件
     static void Main(string[] args)
     {
     StreamReader streamReader =new StreamReader(@"D:Document	est.txt");
     string line;
     //读取文件内容
     while ((line = streamReader.ReadLine()) != null)
     {
     //打印出来
     Console.WriteLine(line);
     }
     Console.ReadLine();
     }
    
  20. windows文件系统操作

     static void Main(string[] args) {
            GetFile(@"D:ProgramFiles");
            Console.ReadLine();
        }
        //获得某文件夹下的文件名与大小
        static void GetFile(String path) {
            DirectoryInfo directoryInfo = new DirectoryInfo(path);
            DirectoryInfo[] directoryInfo1 = directoryInfo.GetDirectories();
            FileInfo[] files = directoryInfo.GetFiles();
            if(directoryInfo1!=null) {
                foreach(DirectoryInfo directoryInfo2 in directoryInfo1) {
                    if(directoryInfo2.Name!="app") {
                        GetFile(path+@""+directoryInfo2.Name);
                    }
                }
            }
            if(files!=null) {
                foreach(FileInfo file in files) {
                    Console.WriteLine("文件名:{0},文件大小{1}",file.Name,file.Length);
                }
            }
        }
    
  21. 特性

    1. 预定义特性
      1.AttributeUsage
      2.Conditional
      3.Obsolete: [Obsolete("过时了")]编译器会给出警告信息[Obsolete("过时了",true)]编译器会给出错误信息

    2. 自定义特性

  22. 委托

    delegate void ConsoleWrite1();
    namespace ConsoleApp1 {
        class Program {
    
            static void Main(string[] args) {
                ConsoleWrite1 consoleWrite1 = new ConsoleWrite1(ConsoleWrite);
                consoleWrite1();
                Console.ReadLine();
            }
            static void ConsoleWrite() {
                Console.WriteLine("测试");
            }
    
    • 委托的用途

               static FileStream fs;
        static StreamWriter sw;
        // 委托声明
        public delegate void printString(string s);
      
        // 该方法打印到控制台
        public static void WriteToScreen(string str) {
            Console.WriteLine("The String is: {0}",str);
        }
        // 该方法打印到文件
        public static void WriteToFile(string s) {
            fs=new FileStream(@"D:Document	est.txt",
            FileMode.Append,FileAccess.Write);
            sw=new StreamWriter(fs);
            sw.WriteLine(s);
            sw.Flush();
            sw.Close();
            fs.Close();
        }
        // 该方法把委托作为参数,并使用它调用方法
        public static void sendString(printString ps) {
            ps("Hello World");
        }
        static void Main(string[] args) {
            printString ps1 = new printString(WriteToScreen);
            printString ps2 = new printString(WriteToFile);
            sendString(ps1);
            sendString(ps2);
            Console.ReadKey();
        }
      
    1. 指针变量
       static unsafe void Test12() {
       int i = 0;
       int* p = &i;
       Console.WriteLine("i的值为{0},内存地址为{1}",i,(int)p);
       Console.WriteLine();
       }
      
    2. 传递指针作为方法的参数
           static unsafe void Main() {
           int var1 = 10;
           int var2 = 20;
           Console.WriteLine("var1:{0},var2:{1}",var1,var2);
           int* a = &var1;
           int* b = &var2;
           Swap(a,b);
           Console.WriteLine("var1:{0},var2:{1}",var1,var2);
           Console.ReadLine();
       }
       static unsafe void Swap(int* a,int* b) {
           int temp =*a;
           *a=*b;
           *b=temp;
       }
      
    3. 使用指针访问数组元素
                static unsafe void array() {
                       int[] list = { 10,100,200 };
                       fixed (int* ptr = list)
                         /* 显示指针中数组地址 */
                         for(int i = 0;i<3;i++) {
                             Console.WriteLine("Address of list[{0}]={1}",i,(int)(ptr+i));
                         Console.WriteLine("Value of list[{0}]={1}",i,*(ptr+i));
               }
                 Console.ReadKey();
      
  23. out参数:一个方法返回多个不同类型的参数

      static void Main() {
           string s="asd";
           int i=Test7(out s);
           Console.WriteLine(s);
           Console.WriteLine(i);
           Console.ReadLine();
    
       }
    
       public static int Test7(out string a) {
           a="asddddddddddd";
           return 1;
       }
    
  24. params参数
     static void Main() {
            Params(213,31231,12312,13231,123,1312,312,312,321,3,12,312,12);
        }
        static void Params(params int[] array) {
            int max = array[0];
            for(int i = 0;i<array.Length;i++) {
                if(array[i]>max) {
                    max=array[i];
                }
            }
            Console.WriteLine("最大值为{0}",max);
            Console.ReadLine();
        }
    
  25. ref参数
    static void Main() {
            int i = 30;
            Ref(ref i);
            Console.WriteLine(i);
            Console.ReadKey();
        }
        static void Ref(ref int a) {
            a+=500;
        }
    
  26. 连接数据库
        static void MysqlConnection() {
            //配置连接信息
            String connstr = "server=localhost;port=3306;user=root;password=123456;database=ztree";
            MySqlConnection mySqlConnection = new MySqlConnection(connstr);
            //打开连接
            mySqlConnection.Open();
            //sql语句
            string sql = "select * from city";
            MySqlCommand mySqlCommand = new MySqlCommand(sql,mySqlConnection);
            //执行sql语句
            MySqlDataReader mySqlDataReader = mySqlCommand.ExecuteReader();
            while(mySqlDataReader.Read()) {
                if(mySqlDataReader.HasRows) {
                    Console.WriteLine("id:{0};name:{1};pid:{2}",mySqlDataReader.GetString(0),mySqlDataReader.GetString(1),mySqlDataReader.GetString(2));
                }
            }
            mySqlConnection.Close();
        }
    
作者: JaminYe
版权声明:本文原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。
原文地址:https://www.cnblogs.com/JaminYe/p/11246738.html