软件编程
位置:首页>> 软件编程>> C#编程>> C#知识整理

C#知识整理

作者:键盘上青春  发布时间:2021-08-02 18:24:26 

标签:C#,知识

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

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 ;}
}

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

来源:http://www.cnblogs.com/swjian/p/6362468.html

0
投稿

猜你喜欢

手机版 软件编程 asp之家 www.aspxhome.com