#region 数据类型byte b =255;//byte范围:0-255short s =-32768;//short范围:-32768-32767int i =2;long p =1222222;float f =1.1F;//单精度的数值后面要加Fdouble d =1.11;//double类型不用加后缀。范围包括以上所有类型的值decimal de =1.1M;//银行项目用,精度高string st ="Hello World";
st =null;//st中什么都没有
st ="";//st中有个字符串,但这个字符串为空字符串
MessageBox.Show(st);#endregion
#region 一元表达式 自增量 自减量int a =2;--a;//a自减1
MessageBox.Show(a.ToString());int a1 =2;++a1;//a1自加1
MessageBox.Show(a1.ToString());int a2 =2;
a2++;//a2自加1
MessageBox.Show(a2.ToString());int a3 =2;
a--;//a3自减1
MessageBox.Show(a3.ToString());//区别: ++在前和++在后int b =1;
MessageBox.Show((b++).ToString());//1【在此句话执行结束后,再执行自加】int c =1;
MessageBox.Show((++c).ToString());//2 【先执行自加,再执行此句话】#endregion
#region 二元运算符 x??y//如果x为null,则为y,否则为xstring a, b, c;
a =null;
b ="hello";
c = a ?? b;
MessageBox.Show(c);string a1, b1, c1;
a1 ="ok";
b1 ="hello";
c1 = a1 ?? b1;
MessageBox.Show(c1);#endregion
#region 二元运算符 且&& 或||bool a;bool b1 =true, b2 =false, b3 =true, b4 =false;
a = b1 && b2;
MessageBox.Show(a.ToString());//false
a = b1 || b2;
MessageBox.Show(a.ToString());//true
a = b1 && b3;
MessageBox.Show(a.ToString());//true
a = b2 || b4;
MessageBox.Show(a.ToString());//false#endregion
#region 三元运算符 x?y:z//如果x为true,则y,否则为zstring x ="a", y ="hello", z ="world", m;
m = x =="a"? y : z;
MessageBox.Show(m);
m = x =="b"? y : z;
MessageBox.Show(m);#endregion
#region 非运算符 !bool b =true;
MessageBox.Show(b.ToString());
MessageBox.Show((!b).ToString());#endregion
#region foreach循环int[] a ={1,2,3,4,5,6};foreach(int item in a){
MessageBox.Show(item.ToString());}
List<int> b =newList<int>(){1,2,3,4,5};foreach(int item in b){
MessageBox.Show(item.ToString());}
Dictionary<string,string> c =newDictionary<string,string>(){{"A","a"},{"B","b"},{"C","c"}};foreach(KeyValuePair<string,string> item in c){
MessageBox.Show(item.Key);//打印字典的键
MessageBox.Show(item.Value);//打印字典的值}#endregion
#region 自定义类并调用,通过foeeach循环展示Person pp =newPerson();//实例化Person,命名为pp
List<Person> ppList = pp.GetUserList();//pp下的方法GetUserList的返回值,赋值给ppListforeach(Person item in ppList)//参考foreach(int item in a){
MessageBox.Show(item.Name +"的年龄是:"+ item.Age);}#endregion