< 返回新闻公共列表
云南大王-学习C#.NET、使用sqlserver数据库的总结
发布时间:2020-04-13 00:00:00
介绍:之前没有学习过C#这个语言,我是刚刚从大学毕业的学生,我的专业是嵌入式系统工程(底层驱动ARM、Linux、单片机),本人很喜欢计算机,更喜欢软件开发(应用层)的这种工作,只是当时考大学时没有过多的了解选错了专业,毕业后开始找工作一直在找我喜欢的应用层行业,找了30多家公司终于有一个愿意培养我的了(主要是我不会应用层的技术,没有公司要我,所以找的公司很多,但我没放弃),在公司的三个月里我一边工作一边学习,现在把我所学到的都记录下来,下面有的代码我是复制其他人的,也别介意我是研究了好久了学会了,方便记录我就黏贴在我这里。大家可以学习,参考,指点错误,我也当个笔记方便以后复习。
有个提示:我是有过C语言底子的,学习C#时那些简单基础的语法很快就能懂得使用,我相信学过类似的语言(Java、C++)都能一看就会那些简单基础,假如不会那请先学习编程的简单语法知识(我博客里也有简单语法知识),所以那些基础性东西我就不写了。我用的软件是vs2010,sqlserver2008。基础部分大多数是”控制台应用程序“,后面一点点延伸winform应用程序,web程序内的(HTML、css、javascript、jpuery、div+css)、MVC,ASP.NET。重点练习项目:超市管理系统、音乐播放器、局域网聊天(tcp/udp)、外网聊天(我相信很多程序员不会写)
想成功,多动脑!不要做个“手重眼轻”,能看会 未必能会写,很多都是用代码来讲解,全是“干货”。
接下来就是我这三个月的历程:
目录
一、基础
1.多态、继承
1)、virtual虚方法多态
2)、abstract抽象方法
2.泛型
1)、泛型方法
2)、泛型类
3)、泛型接口
4)、泛型约束
3.集合
1)、泛型集合(list)
2)、ArrayList集合
3)、Dictionary键值对集合
4)、Hashtable键值对集合
感觉少了什么,仔细想想我少写一个“枚举”,我就把枚举插在这个位置吧,临时在网上找的(不要夸我太懒,用到枚举的地方真的少,我就懒得写了)
5)、枚举
4.拆装箱
5.逆变和协变
6.工厂模式
7.部分类partial
8.密封类sealed
9.重写ToString方法
10.属性
11.里氏转换is、as
12.接口
13.接口与抽象类的区别
14.流操作
15.序列化、反序列化(XML和二进制)
16.PropertyInfo 反射
17.GUID编号
18.StringBuilder
19.委托、事件
20.编写DLL文件、调用DLL文件、传参、返回值
21.字符串大小写转换
22.字符串大小写转换
23.单例模式
24.文件、文件夹操作(path、File、Directory)
项目:----对基础知识的巩固复习
25.超市管理系统(控制台应用程序)
26.小游戏(石头剪刀布)
27.英汉词典
28.音乐播放器(winform)--------------这个软件会做了,才能代表C# winform开发 “基础”已经学会了
--------------------基础基本差不多了,等到全部结束后,我们再来更详细练习基础部分----------------------
二、线程
1.创建线程_前台_后台_挂起_唤醒_阻塞_终止_优先级
2.同步
3.线程池
4.异步
三、进程
1.进程简单操作
2.进程间通信
项目练习:映射综合
四、网络编程
大概了解tcp、udp、socket工作原理,代码是如何编写的,先不需要立刻学会,下面我还会很详细你讲解的
了解部分:
1.tcp
2.udp
3.标准的socket
详细讲解:---->>>>(我花了两块五买的教程,拿出来了给大家分享,代码已经全是我亲手写的了)
1.复习委托机制
2.复习回调机制
3.TCP服务端
4.同步TCP客户端
5.异步TCP客户端
6.UDP进程通信
7.UDP广播和组播
8.局域网聊天室
9.WebClient
10.打洞(可以不用学 ,高端技术很难得)
项目练习:
局域网聊天(简单)、
局域网聊天(仿QQ)、
广域网聊天(仿QQ,很难,学不会就不要学了,嘿嘿很多程序员都不会的)
串口通讯(详细讲解,我认为串口要好好学习,现在的可以都是软件和硬件搭配的好才最值钱,就不过多解释了)
---------------------------------------------------------------------------------------------------------------
数据库了
第一部分:
ac
my
sq
他们的工作原理和操作
第二部分:
和winform的搭配使用
三层架构
项目:管理系统之类的
第三部分(自选喽)
ao很火
-----------------------------------------------------------------------------------------------------------------------------
WPF
把winform管理系统之类的项目改写成WPF
----------------------
ASP.NET
html css js jq MVC
把winform管理系统之类的项目改写成ASP.NET
--------------------------------------------
细致化的复习winform控件之类的知识
-----------------------------------------
在来几个其他技术
---------------------------------------
结束!发言
一、基础
1.多态、继承
1 using System;
2 using System.Collections.Generic;
3 using System.Linq;
4 using System.Text;
5
6 //实现多态的手段
7 //1)、虚方法
8 //步骤:
9 //1、将父类的方法标记为虚方法 ,使用关键字 virtual,这个函数可以被子类重新写一个遍。
10 //理解:通过继承实现的不同对象调用相同的方法,表现出不同的行为,称之为多态。
11
12 //2)、抽象类
13 //当父类中的方法不知道如何去实现的时候,可以考虑将父类写成抽象类,将方法写成抽象方法。
14 //派生类要实现抽象方法,派生抽象类可以不用重写父类抽象方法
15 //抽象类和接口 很像,我会在写接口时介绍他俩的区别
16
17 //3)、继承
18 //新类(即派生类)将获取基类的所有非私有数据和行为以及新类为自己定义的所有其他数据或行为。
19 //子类实现父类的属性和方法,并在此基础上进行相关的扩展
20
21 //下面是虚方法的多态:
22 namespace _1.多态_虚方法_
23 {
24 public class Animal
25 {
26 public virtual void Eat()//虚方法 virtual
27 {
28 Console.WriteLine("动物会吃东西");
29 }
30 }
31
32 public class Dog : Animal//继承Animal Animal为父类
33 {
34 public override void Eat()//重写虚方法 override
35 {
36 Console.WriteLine("狗也会吃东西");
37 }
38 }
39
40 public class Cat : Animal
41 {
42 public override void Eat()
43 {
44 Console.WriteLine("猫也会吃东西");
45 }
46 }
47
48 class Program
49 {
50 static void Main(string[] args)
51 {
52 //各种实现 体会吧
53 /*********************/
54 Animal animal = new Animal();
55 animal.Eat();
56 Dog dog = new Dog();
57 dog.Eat();
58 Cat cat = new Cat();
59 cat.Eat();
60 /*******************/
61 Console.WriteLine();
62 /********************/
63 Animal[] al = new Animal[3];
64 al[0] = new Animal();
65 al[0].Eat();
66 al[1] = new Dog();
67 al[1].Eat();
68 al[2] = new Cat();
69 al[2].Eat();
70 /*********************/
71 Console.WriteLine();
72 /**********************/
73 Animal a1 = new Animal();
74 a1.Eat();
75 Animal a2 = new Dog();
76 a2.Eat();
77 Animal a3 = new Cat();
78 a3.Eat();
79
80 Console.ReadKey();
81 }
82 }
83 }
virtual虚方法多态
1 using System;
2 using System.Collections.Generic;
3 using System.Linq;
4 using System.Text;
5
6 //下面是抽象类的多态:
7 namespace _2.多态_抽象_
8 {
9 public abstract class Animal
10 {
11 public abstract void Eat();//请注意抽象方法没有方法体
12 }
13
14 public abstract class Person:Animal
15 {
16 public abstract void 喝拉撒睡();
17 }
18
19 public class Me : Person
20 {
21 public override void Eat()
22 {
23 Console.WriteLine("我会吃");
24 }
25 //两个抽象方法缺一不可,要全部实现
26 public override void 喝拉撒睡()
27 {
28 Console.WriteLine("我还会喝拉撒睡");
29 }
30 }
31
32 public class You : Animal //You继承Animal 继承用:
33 {
34 public override void Eat()
35 {
36 Console.WriteLine("你就是个吃货");
37 }
38 }
39
40 class Program
41 {
42 static void Main(string[] args)
43 {
44 //Animal animal = new Animal();//抽象类不能被继承!!!!
45 Me m = new Me();
46 m.Eat();
47 m.喝拉撒睡();
48 You y = new You();
49 y.Eat();
50 ////////////
51 Console.WriteLine();
52 ////////////
53 Person[] p = new Person[1];
54 Animal[] a=new Animal[2];
55 a[0] = new Me();
56 p[0]= new Me();
57 p[0].Eat();
58 p[0].喝拉撒睡();
59
60 a[1] = new You();
61 a[1].Eat();
62
63 Console.ReadKey();
64 }
65 }
66 }
abstract抽象方法
2.泛型
1 using System;
2 using System.Collections.Generic;
3 using System.Linq;
4 using System.Text;
5
6 namespace 泛型方法
7 {
8 class Program
9 {
10 static void fx
(ref T l, ref T r)
11 {
12 //l和r值交换
13 T temp;
14 temp = l;
15 l = r;
16 r = temp;
17 }
18
19 static void Main(string[] args)
20 {
21 int a = 1;
22 int b = 2;
23
24 //fx(ref a, ref b);
25 fx(ref a, ref b);//这两种方式是一样的
26
27 Console.WriteLine(a+" "+b);
28 Console.ReadKey();
29 }
30 }
31 }
泛型方法
1 using System;
2 using System.Collections.Generic;
3 using System.Linq;
4 using System.Text;
5
6 namespace 泛型类
7 {
8 //我在这把通常大家爱写的T写成A,是为了让大家知道这知道代表一个类型不是固定的T
9 class FxClass
10 {
11 public void Fx(ref A l, ref A r)
12 {
13 A temp;
14 temp = l;
15 l = r;
16 r = temp;
17 }
18 }
19
20 class Program
21 {
22 static void Main(string[] args)
23 {
24 int a = 1;
25 int b = 2;
26
27 FxClass fxl = new FxClass();
28 fxl.Fx(ref a, ref b);
29 Console.WriteLine(a + " " + b);
30 Console.ReadKey();
31 }
32 }
33 }
泛型类
1 using System;
2 using System.Collections.Generic;
3 using System.Linq;
4 using System.Text;
5
6 namespace 泛型接口
7 {
8 class Program
9 {
10 static void Main(string[] args)
11 {
12 Student stu = new Student();
13 Console.WriteLine(stu.Jia(2, 3));
14 Console.WriteLine(stu.Jian(3, 1));
15 Console.Read();
16 }
17 }
18
19 //定义泛型接口
20 interface Icomuter
21 {
22 //定义泛型方法
23 T Jia(T item1, T item2);
24 T Jian(T item1, T item2);
25
26 }
27
28 //继承泛型接口
29 class Student : Icomuter
30 {
31 //继承实现泛型方法
32 public int Jia(int item1, int item2)
33 {
34 return item1 + item2;
35 }
36 public int Jian(int item1, int item2)
37 {
38 return item1 - item2;
39 }
40 }
41 }
泛型接口
1 using System;
2 using System.Collections.Generic;
3 using System.Linq;
4 using System.Text;
5
6 #region .net泛型约束:
7 //所谓泛型,即通过参数化类型来实现在同一份代码上操作多种数据类型。
8 //泛型编程是一种编程范式,它利用“参数化类型”将类型抽象化,从而实现更为灵活的复用。
9
10 //在定义泛型类时,可以对客户端代码能够在实例化类时用于类型参数的类型种类施加限制。
11 //如果客户端代码尝试使用某个约束所不允许的类型来实例化类,则会产生编译时错误。
12 //这些限制称为约束。约束是使用 where 上下文关键字指定的。
13 #endregion
14
15 #region 约束 说明
16 //T:struct
17 //类型参数必须是值类型。可以指定除 Nullable 以外的任何值类型。
18 //T:class
19 //类型参数必须是引用类型,包括任何类、接口、委托或数组类型。
20 //T:new()
21 //类型参数必须具有无参数的公共构造函数。当与其他约束一起使用时,new() 约束必须最后指定。
22 //T:<基类名>
23 //类型参数必须是指定的基类或派生自指定的基类。
24 //T:<接口名称>
25 //类型参数必须是指定的接口或实现指定的接口。可以指定多个接口约束。约束接口也可以是泛型的。
26 //T:U
27 //为 T 提供的类型参数必须是为 U 提供的参数或派生自为 U 提供的参数。这称为裸类型约束.
28 #endregion
29
30 namespace 泛型约束
31 {
32 class Program
33 {
34 static void fx(ref T l, ref T r) where T : IComparable //where是约束T类型在IComparable范围内
35 {
36 T temp;
37 temp = l;
38 l = r;
39 r = temp;
40 }
41
42 static void Main(string[] args)
43 {
44 int a = 1;
45 int b = 2;
46
47 //fx(ref a, ref b);
48 fx(ref a, ref b);//这两种方式是一样的
49 Console.WriteLine(a + " " + b);
50 Console.ReadKey();
51 }
52 }
53 }
泛型约束
3.集合
1 using System;
2 using System.Collections.Generic;
3 using System.Linq;
4 using System.Text;
5
6 //泛型集合 很简单 我就介绍一下几个部分
7 namespace _1.泛型集合_list_
8 {
9 class Program
10 {
11 static void Main(string[] args)
12 {
13 List list = new List();
14 list.Add(1);
15 list.Add(2);
16 list.Add(3);
17
18 list.AddRange(new int[]{4,5,6});//添加集合
19
20 list.AddRange(list);//添加自己
21
22 int[] ii = list.ToArray();//转为数组
23
24 //怕误会以为泛型集合只可以操作整数
25 List lt = new List();
26 //其实都是一样的,我就不多写了
27 lt.Add("请叫我:毕小帅");//大家都叫我“小帅”嘿嘿
28 lt.Add("我叫:小毕");
29
30 //输出
31 for (int i = 0; i < list.Count; i++)
32 {
33 Console.Write(list[i].ToString());
34 }
35 Console.WriteLine();
36 for (int i = 0; i < lt.Count; i++)
37 {
38 Console.WriteLine(lt[i].ToString());
39 }
40 Console.WriteLine();
41 //也可以用foreach
42 //foreach在数据多的时候能比for的速度快
43 foreach (var item in list)
44 {
45 Console.Write(item.ToString());
46 }
47
48 Console.ReadKey();
49 }
50 }
51 }
泛型集合(list)
1 using System;
2 using System.Collections.Generic;
3 using System.Linq;
4 using System.Text;
5 using System.Collections;
6
7 namespace ArrayList集合
8 {
9 class Program
10 {
11 static void Main(string[] args)
12 {
13 ArrayList al = new ArrayList();
14 //我喜欢它的最重要的一点就是:他什么类型都可以储存
15 //还不用像数组一样定义长度
16 //和list集合不同的就是不用定义类型
17 //缺点 读取时需要拆装箱
18 al.Add(1); //添加数字
19 al.Add("abc"); //添加字符串
20 al.Add('d'); //添加字符
21
22 al.AddRange(new char[] { 'e', 'f' });//添加数组
23 al.AddRange(al); //添加集合 自己
24
25 for (int i = 0; i < al.Count; i++)
26 {
27 Console.WriteLine(al[i].ToString());
28 }
29
30 Console.Read();
31 }
32 }
33 }
ArrayList集合
1 using System;
2 using System.Collections.Generic;
3 using System.Linq;
4 using System.Text;
5
6 namespace Dictionary键值对集合
7 {
8 class Program
9 {
10 static void Main(string[] args)
11 {
12 //他和list集合性质差不多,先确定了类型
13 Dictionary dy = new Dictionary();
14
15 dy.Add("key1", "value1");//添加
16
17 dy["key2"] = "value2";//如果键不存在也可以通过此方法来添加键/值对
18
19 dy.ContainsKey("key1"); //判断该键是否存在
20
21 foreach (Object key in dy.Keys) //遍历key
22 { }
23 foreach (Object value in dy.Values)//遍历value
24 { }
25
26 foreach (var de in dy) //同时遍历键/值对
27 {
28 Console.WriteLine(de.Key);
29 Console.WriteLine(de.Value);
30 }
31 dy.Clear();//清除所有
32
33 Console.Read();
34 }
35 }
36 }
Dictionary键值对集合
1 using System;
2 using System.Collections.Generic;
3 using System.Linq;
4 using System.Text;
5 using System.Collections;
6
7 namespace Hashtable键值对集合
8 {
9 class Program
10 {
11 static void Main(string[] args)
12 {
13 //我的感觉他和Arrylist集合性质差不多,不分类型的存储
14 //键值是唯一的
15 Hashtable ht = new Hashtable();
16
17 ht.Add("key1", "value1");
18
19 ht.Add("key2", "value2");
20
21 ht.Contains("key1");//判断key1键是否存在
22
23 ht.ContainsKey("key1");//同上
24
25 foreach (Object key in ht.Keys) //遍历key
26 { }
27 foreach (Object value in ht.Values)//遍历value
28 { }
29
30 foreach (DictionaryEntry de in ht) //同时遍历键/值对
31 {
32 Console.WriteLine(de.Key);
33 Console.WriteLine(de.Value);
34 }
35
36
37 /****************************/
38 ht.Remove("key1");//移除key1键元素
39
40 //前后做个判断
41 Console.WriteLine();
42 foreach (DictionaryEntry de in ht) //同时遍历键/值对
43 {
44 Console.WriteLine(de.Key);
45 Console.WriteLine(de.Value);
46 }
47 /********************************/
48
49
50
51 /****************************/
52 ht.Clear();//清除所有元素
53
54 //前后做个判断
55 Console.WriteLine();
56 foreach (DictionaryEntry de in ht) //同时遍历键/值对
57 {
58 Console.WriteLine(de.Key);
59 Console.WriteLine(de.Value);
60 Console.WriteLine("这句话没输出就是键值对被清空了");
61 }
62
63
64 Console.Read();
65 }
66 }
67 }
Hashtable键值对集合
1 using System;
2 using System.Collections.Generic;
3 using System.Linq;
4 using System.Text;
5
6 namespace 枚举
7 {
8 class Program
9 {
10 static void Main(string[] args)
11 {
12 Console.WriteLine(Enum.GetName(typeof(Man), 1)); //还是 刘备 (由值获取名字)
13
14 string[] array1 = Enum.GetNames(typeof(Man));
15 Console.WriteLine(array1[1]); //关羽
16
17 Array array2 = Enum.GetValues(typeof(Man));
18 Console.WriteLine(array2.GetValue(1)); //还是关羽
19
20 Type t = Enum.GetUnderlyingType(typeof(Man));
21 Console.WriteLine(t); //输出 Int32
22
23 //由值获取内容
24 int i = 1;
25 string Name = Enum.Parse(typeof(Man), i.ToString()).ToString(); //此时 Name="刘备"
26 Console.WriteLine(Name);
27
28 //由值获取内容
29 string Name2 = "关羽";
30 int j = Convert.ToInt32(Enum.Parse(typeof(Man), Name2)); //此时 j=2
31 Console.WriteLine(j);
32
33 //这段是我自己写的。。。。(不是全都复制的)
34 //判断输入的字符串是否在枚举内
35 Console.WriteLine("请输入");
36 string str = Console.ReadLine();
37 bool b = Enum.IsDefined(typeof(Week), str);
38
39 Console.WriteLine(b.ToString());
40
41 Console.ReadKey();
42 }
43 }
44
45 enum Man
46 {
47 刘备 = 1,
48 关羽 = 2,
49 张飞 = 3
50 }
51 enum Week
52 {
53 Monday = 1,
54 Tuesday = 2,
55 Wednesday = 3,
56 Sunday = 0,
57 Everyday = 1 //成员的值可以设置成一样的,但是成员不行
58 }
59 enum sex : byte //显示指定枚举的底层数据类型
60 {
61 male,
62 female, //此逗号可以省略
63 }; //此分号可以省略
64
65 }
枚举
4.拆装箱
1 using System;
2 using System.Collections.Generic;
3 using System.Linq;
4 using System.Text;
5 using System.Collections;
6 using System.Diagnostics;
7
8 //装箱操作和拆箱操作是要额外耗费cpu和内存资源的,
9 //所以用泛型来减少装箱操作和拆箱操作消耗。
10 namespace 拆箱_装箱
11 {
12 class Program
13 {
14 static void Main(string[] args)
15 {
16 int n1 = 10;
17 object o = n1;//装箱
18 int nn = (int)o;//拆箱
19
20
21 ArrayList lista = new ArrayList();
22 List list = new List();
23 //这个循环发生了100万次装箱操作
24 Stopwatch sw = new Stopwatch();
25 sw.Start();
26 for (int i = 0; i < 10000000; i++)
27 {
28 list.Add(i);
29 }
30 sw.Stop();//计时
31 Console.WriteLine(sw.Elapsed);
32 Console.ReadKey();
33
34
35 //这个地方没有发生任意类型的装箱或者拆箱
36 string str = "123";
37 int n2 = Convert.ToInt32(str);
38
39
40 int n3 = 10;
41 IComparable ic = n3;//装箱
42
43 }
44 }
45 }
拆装箱
5.逆变和协变
1 在网上收集的,平时感觉不常用
2 我们都知道.Net里或者说在OO的世界里,可以安全地把子类的引用赋给父类引用,例如:
3
4 //父类 = 子类
5 string str = "string";
6 object obj = str;//变了
7
8 out 支持协变
9 in 支持逆变
10
11 协变(Foo<父类> = Foo<子类> ): (个人理解:object(范围大)=string(范围小))
12
13 //泛型委托:
14 public delegate T MyFuncA();//不支持逆变与协变
15 public delegate T MyFuncB();//支持协变
16
17 MyFuncA