C# —— (一)变量、常量、类型转换、运算符、循环
命名法:
Pascal 帕斯卡命名法:所有单词首字母大写
camel 驼峰命名法:从第二个单词开始首字母大写
1.主函数 Main
static void Main(string[] args){}
- 必须定义为static
 - M必须大写,C#区分大小写
 - 返回值只能是 void 或 int
 
2.变量
| 类型 | 详解 | 
| decimal | 
  | 
| bool | 值只能为true/false | 
| object | 所有类的基类 | 
3.常量
| 名称 | 类型 | 详解 | 
| 编译时常量 | const | 可以在只声明未赋值的前提下,赋值给其他常量,只要在其后赋了值,就能通过编译。 | 
| 运行时常量 | readonly | 必须在定义时或构造函数中初始化,不能在方法中声明 | 
4.类型转换
| 隐式转换 | bool<char<int<float<double 只能向右转换 窄==>广 | 
| 显示转换 | 
  | 
5.运算符
| 算术运算符 | % | 返回结果的符号与左操作数相同 | 
| 赋值运算符 | += | 复合赋值运算符可以自动完成类型转换 | 
| 逻辑运算符 | 
 && || & |  | 
 又叫短路运算符,只要 &&左边为负,||左边为正,则不继续判断。 需先计算运算符左右两边再判断  | 
| 特殊运算符 | 
 is 
 @ 
 $ ? ??  | 
 对象 is 类型 bool result = i is int; 
 代替string.Format() 可空类型修饰符,int = null; ×报错,int? = null; 编译器编译时会把int? 编译成System.Nullable的方式 空兼并运算符,a??b, if (a==null) return b;else return a; 返回不为null的那个操作数  | 
6.循环
| switch | switch(不能是浮点数) | 
| while | while(C#表达式是bool值,不能是0/1,而是false/true) | 
| foreach | 
 foreach(类型 迭代变量名 in 集合表达式) string[] strNames = {"小明","小花","红红"}; foreach(string str in strNames) { Console.WriteLine(str); }  | 
                    
                
                
            
        
浙公网安备 33010602011771号