12表达式的概念
表达式由操作数和运算符构成。运算符的示例包括+ - * /和new。操作数单位示例包括文本、字段、局部变量和表达式。
13作用域简介
变量名不能重复,不管什么类型
在一个大括号内,必须声明变量以后才能使用
可以声明一个变量在整个类内使用,但是如果在方法内声明了同一个名称的变量,那么在整个方法内,在这个变量声明之后才能使用。
14自增-自减
++x
x++
统称为自增量
—x
x—
统称为自减量
int x = 1;MessageBox.Show(x.ToString());MessageBox.Show((++x).ToString());MessageBox.Show((--x).ToString());MessageBox.Show((x++).ToString());MessageBox.Show(x.ToString());MessageBox.Show((x--).ToString());MessageBox.Show(x.ToString());
15-20四则运算-取余、字符串也有加法运算、判断相等、字符串比较、且或运用、三元-复合赋值-非运算
四则运算-取余
字符串也有加法运算
判断相等
bool b;b = 1 > 2;MessageBox.Show(b.ToString());b = 2 == 1;MessageBox.Show(b.ToString());b = 2 != 1;MessageBox.Show(b.ToString());
字符串比较
bool b;b = "hello" == "world";MessageBox.Show(b.ToString());b = "hello" != "world";MessageBox.Show(b.ToString());
且或运用
bool b;bool b1 = true, b2 = false, b3 = true, b4 = false;b = b1 && b2;MessageBox.Show(b.ToString());b = b1 || b2;MessageBox.Show(b.ToString());b = b1 && b3;MessageBox.Show(b.ToString());b = b2 || b4;MessageBox.Show(b.ToString());
x && y 仅当x为true时,才对y求值
x || y 仅当x为false时,才对y求值
输出:
false
true
true
false
string s1, s2, s3;s1 = null;s2 = "hello";s3 = s1 ?? s2;MessageBox.Show(s3);
s1 ?? s2:如果s1为null则结果为s2,否则结算结果为s1。
输出:
hello
string s1, s2, s3;s1 = "world";s2 = "hello";s3 = s1 ?? s2;MessageBox.Show(s3);
三元-复合赋值-非运算
x?y:z 如果x为true,对y求值;如果x为false则对z求值;
string x = "a", y = "hello", z = "world",m;m = x == "a" ? y : z;MessageBox.Show(m);m = x == "b" ? y : z;MessageBox.Show(m);
输出:
hello
world
int i = 1,j=2;i = i +j;MessageBox.Show(i.ToString());
输出:
3
bool b = true;MessageBox.Show((!b).ToString());string x = "西瓜";MessageBox.Show((!(x == "西瓜")).ToString());
21-24分支语句(条件语句)和循环语句
分支语句
//if语句if(i > 1){}//if-else语句int i = 1;if (i == 0){MessageBox.Show("i确实是0啊!");}else if(i>0 && i < 2){MessageBox.Show("i在1到2之间");}else{MessageBox.Show("i不是0!");}
//switch语句int i = 3;switch (i){case 1:MessageBox.Show("这是1号门");break;case 2:MessageBox.Show("这是2号门");break;case 3:MessageBox.Show("这是3号门");break;default:MessageBox.Show("你全都对不上");break;}
循环语句
//for,注意按tab键位可以自动生成对应基础循环代码for (int i = 0; i < 5; i++){MessageBox.Show($"这是第{i+1}次循环,此时i为{i}");}//while循环int i = 1;while (i < 5){MessageBox.Show($"这是第{i}次循环,此时i为{i}");i++;}i = 1;//do-while循环do{MessageBox.Show($"这是第{i}次循环,此时i为{i}");i++;}while (i < 5);
25数组
- 数组是一组相同类型的数据
- 数组中的数据需要通过数字索引来访问
-
数组的声明
数组的声明需要使用 new 关键字。
- 在声明数组时,可以使用{}来初始化数组中的元素。
- 如果在数组声明之初没有使用大括号来初始化数组中的元素,则需要指定数组的大小。
- 在声明初始化有元素的数组时,也可以指定数组的大小。 ```csharp //声明没有元素的数组 int[] ints = new int[6]; //注意当没有给数组赋值的时候,数组内的值默认为0
//声明初始化有元素的数组 int[] jnts = new int[] { 1, 3, 4, 5 }; //在声明初始化有元素的数组时,也可以指定数组的大小 //这种情况可以在大括号中给数组赋予任意数量的值
string[] strings = new string[5]{“H”,”E”,”L”,”L”,”O”}; //注意中括号有数组大小的时候,后面给数组赋值的数量不能超过这个
var strings = new string[] { “1”, “2”, “3”, “4” };
<a name="lnhZX"></a>## 通过索引获取数组中的元素- 给数组指定长度时,数组准备存放多少元素,长度就设置为多少。- 用索引获取数组内的元素时,索引从0开始获取。- 所以数组中的最大的索引数字,比指定数组长度小1。```csharpint[] ints = new int[] { 1, 3, 4, 5 };int i1 = ints[0];ints[0] = 5;i1 = ints[0];
获取数组信息
int[,] i = new int[4,5];Console.WriteLine(i.Length);//获取元素总数Console.WriteLine(i.Rank);//获取数组维数Console.WriteLine(i.GetLength(0));//获取指定维数中的元素数Console.WriteLine(i.GetUpperBound(0));//获取数组中指定维度最后一个元素的索引Console.WriteLine(i.GetUpperBound(1));Console.WriteLine(i.GetLowerBound(0));
清空数组
string[] str = new string[2];for (int i = 0; i < str.Length; i++)str[i] = i.ToString();Array.Clear(str, 0, str.Length);for (int i = 0; i < str.Length; i++)Console.WriteLine(string.IsNullOrEmpty(str[i]) ? "null" : str[i]);//输出结果://null//null
初始化数组
//只是用于初始化,如果数组子项已经被初始化,那么不会改变它的值.//(删除下面代码 x[1] = 6; 试试)int[] x = new int[2];int[] y = new int[2];x[0] = 5; x[1] = 6;x.Initialize();y.Initialize();for (int i = 0; i < x.Length; i++)Console.WriteLine("x:{0} y:{1}", x[i], y[i]);//输出结果://x:5 y:0//x:6 y:0
动态修改数组大小
//处理流程是:先创建一个新的数组,然后把旧数组赋值过去.int[] x = new int[2];x[0] = 5; x[1] = 6;Array.Resize(ref x, x.Length + 2);for (int i = 0; i < x.Length; i++)Console.WriteLine("x[{0}]={1}",i, x[i]);//输出结果://x[0]=5//x[1]=6//x[2]=0//x[3]=0
如果数组较大,性能影响明显,建议用List<>的AddRange方法. [去MSDN查看].aspx)
List<int> lst = new List<int>();lst.Add(3); lst.Add(4);int[] x = new int[2];x[0] = 5; x[1] = 6;lst.AddRange(x);for (int i = 0; i < lst.Count; i++)Console.WriteLine("lst[{0}]={1}", i, lst[i]);//输出结果://lst[0]=3//lst[1]=4//lst[2]=5//lst[3]=6
关于Array数组复制拷贝,倒叙,排序等,详情参见MSDN.aspx)
26-27总结提升-案例1

double price = 1.9;//每斤单价int count = 6;//购买数量double discount = 0.75;//折扣double totalprice;//存放总价的变量//计算折扣前总价totalprice = price* count;//判断总价有没有超过10元if (totalprice >10){//超过10元折价处理totalprice = totalprice * discount;//复合赋值}MessageBox.Show(totalprice.ToString());
double d = totalprice / 10;//隐式转换//显示强制转换,不遵循四舍五入,只截取整数部分,如(int)5.21输出5//Int.Parse():只支持将string类型转成int,Parse就是把string类型转换成int,char,double....等//也就是*.Parse(string)括号中一定要是string类型。string st = "5.21";double n = 5.21;int.Parse(st);//输出5int.Parse(n);//报错//而且只能是数字,才能转换成int类型。//Convert.ToInt32(double value),不完全遵循四舍五入//如果value为两个整数中间的数字,则返回二者中的偶数。Console.WriteLine(Convert.ToInt32(4.3));//四舍五入,输出4Console.WriteLine(Convert.ToInt32(4.5));//4.5为4和5中间的数字,所以输出5Console.WriteLine(Convert.ToInt32(4.53));//四舍五入,输出5Console.WriteLine(Convert.ToInt32(5.3));//5Console.WriteLine(Convert.ToInt32(5.5));//6Console.WriteLine(Convert.ToInt32(5.53));//6//注意,convert.toint32()和int.parse()对于空值(null)的处理不同//convert.toint32(null)会返回0而不会产生任何异常//但是,int.parse(null)则会产生异常
如果题中要求满几个10元,就在几个10元上打7.5折该如何计算?
double price = 1.9;//每斤单价int count = 16;//购买数量double discount = 0.75;//折扣double totalprice;//存放总价的变量//计算折扣前总价totalprice = price* count;int d = (int)(totalprice / 10);double totalten = 0;for(int i = 0; i < d; i++){totalten =totalten+10*discount;}double y = totalprice % 10;totalprice = totalten + y;MessageBox.Show(totalprice.ToString());
28-30案例2
ctrl+K+C快捷键快速注释代码

//for循环string[] strings = new string[] { "张三", "李四", "王五","赵六", "田七", "周八" };int stringscount = strings.Length;//获取数组长度for (int i = 0; i < stringscount; i++){if (strings[i] == "赵六"){continue;//continue可以直接执行下一个循环Console.WriteLine("找到了" + strings[i] + "送她回家," + strings[i] + $"是第{i + 1}位同学");break;//退出循环}}
//while循环string[] strings = {"张三","李四","王五","赵六","田七","周八"};int i = 0;string studnet = "";bool isfind = true;while (studnet != "赵六"){if (i >= strings.Length){Console.WriteLine("没有找到人");isfind = false;break;}studnet = strings[i++];}if (isfind) Console.WriteLine("找到了" + studnet + ",送她回家");
31-34函数初识
函数的命名规范
函数命名使用大驼峰命名,即首字母大写。
- 多个单词拼接时,所有单词首字母大写。
函数的参数设置&传参行为
- 参数可认为是外部需要函数 帮忙处理的数据 。
-
函数返回值的设置
函数返回值可以认为是外部调用某种行为后得到的一种反馈
private void Form1_Load(object sender, EventArgs e){SendMessage("你好,好久不见?");}public void SendMessage(string message){MessageBox.Show(message);//注意定义什么类型就只能传递什么类型的参数//如果传递的数值是1就会出错}
private void Form1_Load(object sender, EventArgs e){string res = SellHouse(100, 10000);MessageBox.Show(res);}public string SellHouse(int area,int price){//string表示返回的值的类型return "张三想买这套房,愿意出价" + (area*price- 1) + "万";}
参数修饰符
```csharp
private void Form1_Load(object sender, EventArgs e)
{
string message = “你好李四!”;
SendMessage(out message);
MessageBox.Show(message);
}
public void SendMessage(out string message)
{
message = “好久不见,你还好吗”;
MessageBox.Show(message);
}
//与无修饰符不同,out可以返回多个值 //out的变量必须在对应方法内给它赋初值,不然会出错。 //而且就算之前给对应变量赋初值,不在对应方法内赋值也会出错。 private void Form1_Load(object sender, EventArgs e) { string res = SellHouse(100, 10000, out string wantprice); MessageBox.Show(res); MessageBox.Show(wantprice); } public string SellHouse(int area,int price,out string wantprice) { wantprice = “愿意出价” + (area * price - 1); return “张三想买这套房”; }
<a name="qjaic"></a>## 封装案例一```csharpprivate void Form1_Load(object sender, EventArgs e){double totalprice = Buy(1.9, 16, 0.75);MessageBox.Show(totalprice.ToString());}public double Buy(double price, int count, double discount){double totalprice;totalprice = price * count;int d = (int)(totalprice / 10);double totalten = 0;for (int i = 0; i < d; i++){totalten = totalten + 10 * discount;}double y = totalprice % 10;totalprice = totalten + y;return totalprice;}
36-39类和对象
面对对象编程-Object Orient Programming 简写OOP
类和对象是面向对象编程的两个核心概念。
类是对一群具有相同特征或者行为的事物的一个统称,是抽象的。不能直接使用
- 特征被称为属性
- 行为被称为方法
类就相当于制造汽车的图纸,是一个模板,是负责创建对象的
对象是由类创造出来的一个具体存在,可以直接使用
由哪一个类创造出来的对象,就拥有在哪一个类中定义的属性和方法
对象就相当于用图纸制造汽车
类是模板,对象是根据类这个模板创建出来的,先有类后有对象
类只有一个,而对象可以有很多个
不同对象之间属性的具体内容可能各不相同
类中定义了什么属性和方法,对象中就有什么属性和方法,不可能多,也不可能少
类的设计
- 类名 这类事物的名称,满足大驼峰命名法
- 属性
- 方法
类名的确定:名词提炼法 分析整个业务流程,出现的名词,通常就是找到的类
类和对象的使用
声明类
//右键当前项目——添加,加一个public修饰符public class person{}
声明属性
- 依旧遵循大驼峰命名法
- 最常用的书写方法:public int age {get;set;}
- get关键字,说明可以获取该属性的值。
set关键字,说明可以向该属性设置值
public class person{public string Name { get; set; }public int Age{ get; set; }public int Height{ get; set; }public void Eat(){MessageBox.Show("11");}public void Run(){MessageBox.Show("12");}}
声明方法
实例化
类使用关键字new实例化对象
- 一个类可以实例化多个对象
- 对象可以使用类定义的属性和方法 ```csharp new person(); new person().Eat();
//string person = new person(); //因为string也是一个类型,C#中万物皆对象,对象都是有一个类型的,所以string本身就是一个类型 //string可以接受string类型的字符串,所以要接受person类型要用person类型 person zhangsan = new person(); zhangsan.Eat(); zhangsan.Name = “张三”; zhangsan.Run(); zhangsan.Age = 18; zhangsan.Height = 175;
person wangwu = new person() { Age = 18, Height = 170, Name = “张三” }; wangwu.Eat(); wangwu.Run();
<a name="pDcgI"></a>## 拓展```csharp//新类中public static string ID { set; get; }public string mess { get; set; }//原类中调用静态属性或方法person.ID = "1234";/*public void static Run(){//静态方法中只能使用静态字段MessageBox.Show(mess + "12");}*/public void static Run(){//静态方法中只能使用静态字段MessageBox.Show(ID + "12");}//另外一个类中person.ID = "你好呀";person.Run();
40-45数组&字典
ArrayList
using System.Collections;ArrayList arraylist = new ArrayList();//将数据新增到结尾处arraylist.Add("abc");arraylist.Add(123);//改数据arraylist[2] = 345;//移除指定索引数据arraylist.RemoveAt(0);//移除指定数据arraylist.Remove(123);//指定位置插入数据arraylist.Insert(0, "hello world");
ArrayList劣势
- 在存储数据时使用object类型进行存储的。
- 不是类型安全的,使用时很可能出现类型不匹配的错误。
- 就算都有插入了同一类型的数据,但在使用的时候,我们也需要将他们转化为对应的原类型来处理。
- 存储存在装箱和拆箱操作,导致其性能低下
正是因为ArrayList存在不安全类型与装箱拆箱的缺点,所以在C#2.0后出现了泛型的概念。//装箱int 1 - 123;object o = i;//拆箱object o = 123;int i = (int)o;
泛型:限制集合只能够存储单一类型数据的一种手段List
```csharp Listintlist = new List (); //list集合中的中括号表明必须给list集合指明数据类型 int[] ints = new int[5]; //数组中的中括号是表明必须给数组指定长度
intlist.Add(123); intlist[0] = 345; //注意当集合的大小没有那么大的时候,超出范围的赋值会出错 intlist.RemoveAt(0); intlist.Remove(123); intlist.Insert(0, 6668); intlist.Clear();
//可加可不加小括号
List
List
<a name="62VJP"></a>## List-自定义类型```csharpclass NewClass{public int Age { get; set; }public int Height { get; set; }public string Name { get; set; }}List<Person> people = new List<Person>();Person person1= new Person(){Age = 18,Height = 175,Name = "张三"};people.Add(person1);people.Add(new Person{Age = 18,Height = 179,Name = "李四"});Person person2 = people[0];people.RemoveAt(0);people.Remove(person2);
字典
- 在声明字典时,需要同时为其声明字典内键与值的类型。
```csharp
Dictionary
dictionary = new Dictionary (); //键与值可以是任何类型,但是键必须在设置时是唯一的,而值可以不唯一 //就好比每个学生的学号必须必须是唯一的,而所有成绩可以不唯一
//两种赋值方式
//方式1:Add方法赋值
dictionary.Add(1, “98分”);
dictionary.Add(2, “92分”);
dictionary.Add(3, “89分”);
dictionary.Add(1, “88分”);//系统会报错
//方法二:索引器赋值
dictionary[1] = “88分”;//系统不会报错
dictionary[4] = “99分”;
//方法三:对象初始化器
Dictionary
//获取键值为1的值
//方法1:索引器取值
string value = dictionary[1];
//方法2:foreach遍历
foreach (KeyValuePair
- 键与值可以是任何类型,但是键必须在设置时是唯一的,而值可以不唯一- 使用Add()方法添加键值对,不可添加已有的键名- 索引模式可以新赋值+也可以修改已有的键值<a name="mI9C2"></a>## foreach的使用```csharpint[] ints = { 1, 2, 3, 4, 5, 6, 7 };foreach (var item in ints){Console.WriteLine(item);}List<int> intlist = new List<int> { 1, 2, 3, 4, 5 };foreach (var item in intlist){Console.WriteLine(item);}Dictionary<string, string> dictionary2 = new Dictionary<string, string>(){{"A","aa" },{"B","bb" },{"C","cc" },};foreach (KeyValuePair<string, string> item in dictionary2){string key = item.Key;string value = item.Value;}
//新类中class Person{public int Age { get; set; }public string Name { get; set; }public List<Person> GetUserList(){List<Person> personList = new List<Person>();personList.Add(new Person{Age = 1,Name = "A"}) ;personList.Add(new Person{Age = 2,Name = "B"});personList.Add(new Person{Age = 3,Name = "C"});personList.Add(new Person{Age = 4,Name = "D"});return personList;}}//项目中Person person = new Person();List<Person> personList =person.GetUserList();foreach (Person item in personList){Console.WriteLine(item.Name+"的年龄是:"+item.Age);}

