当前位置: 移动技术网 > IT编程>开发语言>c# > C#知识整理

C#知识整理

2019年07月18日  | 移动技术网IT编程  | 我要评论

这里简单介绍了一些常用的属性,以及一些术语的解释和举例说明,不太全面,希望读者多多补充。

1.重载:函数名相同,参数的个数或参数类型不同;

 public void mydog(string s);
 public void mydog(int i);
 public void mydog(string s,int i); 

2.继承:一个类继承另一个类中的成员,被继承的叫做基类,继承类叫做派生类;

class a
 {
 成员;
 }
class b:a //继承的写法 派生类:基类
 { 
 成员;
 }

3.多态:可以在子类中重写父类的方法 重写方法需要定义override类型

 public override duotai()
 {
 console.writeline("此处可以重写父类中的'duotai'方法");
 }

4.接口:实现提供了一种规范和约束,关键词 interface

 1.修饰符:new public protected internal private;

 2.接口成员前不允许有修饰符;

 3.一个类可以继承多个接口;

 4.格式: 接口修饰符  关键字  接口名

 public interfa jiekou
 {
 void g(); //接口成员;
 }

5.抽象类:关键字(abstract)

 public abstract class pisaall
 {
 成员;
 }

6.封装(类的属性):将一组数据打包封装.

 public string mianbing { get; set; }
 public string shui { get; set; }

7.构造函数:与类同名,public修饰,没有返回值(不是void)

class dog
 {
 public dog(string s,int i ) //构造函数写法
 {
 console.writeline("这是一只小狗!名叫{0},今年{1}岁",s,i);
 }
 }

 mian中调用:
 dog dog = new dog("铛铛",4);

8.成员访问控制符:

   1.public (共有的):允许任何类中的成员进行访问.

   2.private (私有的):不能被其他类中的成员访问,包括派生类也不好使.

   3.internal (内部成员):只能被程序集内的类的成员访问,而程序集外的类(包括派生类)的成员是不允许访问的.

   4.protected (保护成员):可以被本类或派生类中的成员访问,其他类中的成员不允许访问.

9.连接数据库用的语句:

 1. string constr = "data source=iucl8v4y7nw5ira\\sqlexpress;initial catalog=bookshopplus;user id=sa;pwd=sa123"; 
 
 2. static string s = @"server=my-20150918rbsf;database=beauty;integrated security = true"; 
 
 3. static string s = configurationsettings.appsettings["dbinfo"].tostring(); 

10.异常处理:

 1. try{}catch{}
 
 2. try{}catch{}finally{}

 3. using(sqlconnection con = new sqlconnection(constr)){}

11.命名空间:

 1.using system.data.sqlclient; ==>用于sql数据库

 2.using system.data; ==>可使用data类

 3.using system.collections; ==>arraylist数组 

12.arraylist : ①相当于一种高级的动态数组,array类的升级版本.

            ②利于遍历数组,是一个很方便的容器类,可以存储任何引用类型或值类型.     

arraylist arr = new arraylist();
 arraylist arr1 = new arraylist(30); ==>可以添加到30以上 student类
 arr.add(12); ==>定义int类型      class student                       {
 arr.add(true); ==>定义bool类型      public int no { get; set; }  
 arr.add("hello"); ==>定义字符串类型      public string name { get; set; }
 student st = new student(); ==>实例化student类   }
 st.no = 1001; ==>给st中的no赋值
 st.name = "zhangsan"; ==>给st中的name赋值
 arr.add(st); ==>将st的值添加到arr中
 arr.removeat(1); ==>删除数组中第二个
 arr.insert(1, "world"); ==>插入数据world
 for (int i = 0; i < arr.count; i++)
 {
 console.writeline(arr[i]); ==>for循环便利数组
 }

13.hashtable类型 : 两个参数影响其性能 ==> 初始容量、加载因子.

hashtable ht = new hashtable();
 ht.add("0531","济南市");
 ht.add("0532","青岛市");
 ht.add("0536","潍坊市");
 ht.add("0631","威海市"); 
 console.writeline(ht["0531"]); ==>输出下标为0531的值,此处为"济南市";
 console.writeline(ht.count); ==>输出hashtable数组的长度,此处为4;

14.icollection类型 : 是ienumerable的加强型接口,提供同步处理、赋值功能.

hashtable ht = new hashtable();
 ht.add("0531","济南市");
 ht.add("0532","青岛市");
 ht.add("0536","潍坊市");
 ht.add("0631","威海市"); 
 icollection keys = ht.keys; ==>获取hashtable中所有的keys值,这里不是方法,所以keys后没括号
 foreach(string k in keys)
 {
 console.writeline("{0}-----{1}",k,ht[k]);
 }

15.ienumerator 迭代器(与hashtable)

hashtable ht = new hashtable();
 ht.add("0531","济南市");
 ht.add("0532","青岛市");
 ht.add("0536","潍坊市");
 ht.add("0631","威海市");
 icollection keys = ht.keys; ==>获取hashtable中所有的keys值,这里不是方法,所以keys后没括号
 ienumerator ie = keys.getenumerator(); ==>返回访问集合的枚举数
 while(ie.movenext()) ==>枚举数推进到集合的下一元素 
 {
 console.writeline(ie.current); ==>获取集合中当前元素
 console.writeline("{0}-----{1}",ie.current,ht[ie.current]);
 }

16.ienumerator 迭代器(与arraylist)

arraylist arr1 = new arraylist(); 
 arr.add(12);  ==>定义int类型    
 arr.add(true); ==>定义bool类型     arr.add("hello"); ==>定义字符串类型  
  arr.insert(1,"world"); ==>在第一个后面插入字符串"world"
 ienumerator ie = keys.getenumerator(); ==>返回访问集合的枚举数 
    while(ie.movenext()) ==>枚举数推进到集合的下一元素 
    {
   console.writeline(ie.current); ==>获取集合中当前元素
   console.writeline("{0}-----{1}",ie.current,ht[ie.current]);
    }

17.list 泛型: ①类和方法的具体参数可延迟到客户代码中声明,实现.

           ②可以与任何数据类型一起工作(类、方法).

定义student按照学号排序的类:
 class mystudentcompare:icomparer<student> ==>定义类型为比较两个对象而实现的方法
 { 
 public int compare(student st1, student st2)
 {
 return st1.no - st2.no;
 }
 }
 定义student类:
 class student
 {
 public int no { get; set; }
   
 public string name { get; set; }
 }
 定义student泛型:
 list<student> list = new list<student>(); ==>定义student类型的泛型
 list.add(new student(1002, "张三2"));
 list.add(new student(1004, "张三4"));
 list.add(new student(1003, "张三3"));
 list.add(new student(1001, "张三1"));
 list.add(new student(1005, "张三5"));
 list.sort(new mystudentcompare()); ==>调用定义的类方法
 foreach (student st in list)
 {
 console.writeline(st.tostring());
 }

18.linkedlist 类型: 双向列表,效率较高, 只能找第一个和最后一个.

linkedlist<int> lnk = new linkedlist<int>(); ==>定义int类型的泛型 
 lnk.addfirst(1); 
 lnk.addlast(2); 
 lnk.addlast(3); 
 foreach (var lnk1 in lnk) ==> var可以识别类型,var本身也是种类型
 {
 console.writeline(lnk1);
 } 
 linkedlistnode<int> first = lnk.first; ==>获取第一个节点 
 console.writeline(first.value); ==>把第一个节点值输出

19.dictionary :需要引用 using system.collections 命名空间.

描述: ①从一组键(key)到一组值(value)的映射,每个添加项都是由一个值及其相关联的键组成.

         ②任何键都必须是唯一的.

         ③键不能为空引用null,若值为引用类型,则可以为空值.

         ④key和value可以是任何类型(string,int,custom,class).

 dictionary<int, string> dic = new dictionary<int, string>();
 dic.add(0531, "济南"); 
 dic.add(0532, "青岛"); 
 icollection<int> key2 = dic.keys; ==>获取dic中的键的集合 
 foreach (var k in key2) ==> var 是3.0 新加的功能
 {
 console.writeline("{0}----{1}", k, dic[k]);
 }

20.hashset : 是一个无序集合,不能有重复值.

hashset<string> hs = new hashset<string>(); ==>可添加string类型的数值
 hs.add("12345"); 
 hs.add("apple"); 
 hs.add("1234"); 
 hs.add("hello"); 
 hs.add("123"); 
 hs.add("world");
 ienumerator<string> ie = hs.getenumerator();
 while (ie.movenext())
 {
 console.writeline(ie.current);
 }
 console.writeline(hs.count);

21.自定义泛型 :

 public class person <t>
 {
 public t no {get ; set ;}
 } 

注: 本文纯属记事本写的,可能会产生一些代码错误,希望读者加以改正!~

以上就是本文的全部内容,希望本文的内容对大家的学习或者工作能带来一定的帮助,同时也希望多多支持移动技术网!

如对本文有疑问, 点击进行留言回复!!

相关文章:

验证码:
移动技术网