c#基础梳理
2012-12-05 11:19 露珠的微笑 阅读(252) 评论(0) 收藏 举报一、类型、变量和值
C# 是一种强类型语言。
类型中存储的信息可以包括:
-
该类型的变量所需的存储空间。
-
该类型可以表示的最大值和最小值。
-
该类型包含的成员(方法、字段、事件等)。
-
该类型所继承的基类型。
-
将在运行时为其分配变量内存的位置。
-
允许的运算种类
2、在变量声明中指定类型
在程序中声明变量或常量时,必须指定其类型或者使用关键字 var 让编译器可以推断其类型
string name
var limit = 3;
一、访问修饰符
1、用在类型:
public:任何地方都可以访问
internal:表示只能在当前项目中被访问
2、用在成员上
public:
private:本类代码块中可访问,其它不行
protected:本类及子类可访问,即使子类在其它项目也是可以访问的,其余不行。
internal:表示只能在当前程序集完全公开
protected internal:被protected internal修饰的属性/方法只能在它的在同一个程序集(Assembly)中的子类被访问。

二、访问级别约束
1、子类的访问级别不能比父类的高
2、字段和属性的访问级别不能高于对应成员的访问级别
3、方法的访问级别不能高于参数与返回值的访问级别
4、不允许访问不可访问的成员
三、静态成员
1、调用前初始化
静态字段的赋值也可以由静态构造方法完成的,静态构造方法不允许手动调用,由系统自动调用且只被调用一次
2、静态成员只有在第一次访问的时候进行初始化,但是会一直保留在内存中
3、静态类不允许被继承,静态类只能继承自object,且不能实现任何接口,即:继承(多态)与静态是两个互斥的概念
4、静态类的本质: 1)查看IL代码 :abstract+sealed
2)不允许实例化: abstract
3)不允许被继承: sealed
四、继承和多态
1、多态:
多态:同一个函数在不同对象里有不同的实现
多态的使用:将不同的对象当作父类来看,屏蔽掉各个对象间的不同,写出通用代码,做出通用编程,同样调用不同结果,以适应需求的不断变化
多态的保障
1)继承,相同名字的方法(重载不算)
2)里氏转换原则
2、用重写基类方法的多态可以实现父类访问子类的方法:
1)父类定义虚方法,由子类去实现
2)如果父类指向子类调用这个方法,那么实现的具体代码就是父类指向的那个子类所提供的方法
3、有关new与override,overload
overload:重载指的是同一个类中有两个或多个名字相同但是参数不同的方法,(注:返回值不能区别函数是否重载),重载没有关键字。
override:重写的方法称为重写基方法,重写的基方法必须与 override 方法具有相同的签名。重写的基方法必须是 virtual、abstract 或 override 的,不能重写非虚方法或静态方法。
new:覆盖指的是不同类中(基类或派生类)有两个或多个返回类型、方法名、参数都相同,但是方法体不同的方法,基类覆盖了父类方法
重载overload:
public void Fun() { Console.WriteLine("I am F"); } public void Fun(int i) { Console.WriteLine("I am F,i={0}",i); }
重写override:
1 namespace ConsoleApplication1 2 { 3 class Program 4 { 5 static void Main(string[] args) 6 { 7 a test = new b(); 8 test.Fun(); 9 Console.Read(); 10 } 11 } 12 13 14 15 class a 16 { 17 public virtual void Fun() 18 { 19 Console.WriteLine("I am a"); 20 } 21 } 22 23 24 25 class b : a 26 { 27 public override void Fun() 28 { 29 Console.WriteLine("I am b"); 30 } 31 } 32 }
以上b对父类的方法Fun如果用NEW关键字覆盖,则控制台输出I am a
覆盖new:
class Program { static void Main(string[] args) { a testa = new b(); testa.Fun();//调用父类的Fun b testb = new b(); testa.Fun();//调用b的Fun Console.Read(); } } class a { public void Fun()//父类方法无virtual修饰 { Console.WriteLine("I am a"); } } class b : a { public new void Fun()//覆盖父类方法 { Console.WriteLine("I am b"); } }
4、匿名方法
// Create a handler for a click event button1.Click += delegate(System.Object o, System.EventArgs e) { System.Windows.Forms.MessageBox.Show("Click!"); }; // Create a delegate instance delegate void Del(int x); // Instantiate the delegate using an anonymous method Del d = delegate(int k) { /* ... */ };
五、抽象类
1、抽象类
不允许实例化,不能new
抽象类可以有构造方法
抽象类可以来源于非抽象类
2、由子类实现抽象属性的方法
1)用override重写属性
2)添加一个字段,使用属性
六、接口
接口的存在就是为了多态
1、语法: public interface 接口名
{
//成员
}
2、默认接口成员均是public,所以不需要考虑访问修饰符
3、在继承中有接口,又有类的时候注意:
1)语法
class 类名:父类,接口,接口
{
//成员
}
2)多个接口中如果有重名方法怎么办?
->使用显示实现接口的方式
->方法名是有接口名引导的
->方法没有访问修饰符
->方法不能被该类型的实例对象说调用,必须将其转换为接口类型进行调用
七、参数关键字out和ref
一般使用ref是使用方法修改某些数据以后,要求方法结束后还能使用
一般使用out是为了使用某个方法为某些变量进行初始化
1、static void Main(string[] args)
{
int num;
GetNum(out num);
Console.WriteLine(num);
}
static void GetNum(out int num)
{
num = 10;
}
八、using关键字
using 关键字有两个主要用途:
1.作为指令,用于为命名空间创建别名或导入其他命名空间中定义的类型。
2.作为语句,用于定义一个范围,在此范围的末尾将释放对象。
using指令
①允许在命名空间中使用类型,这样,您就不必在该命名空间中限定某个类型的使用:
using System.Text;
using PC.Company;
②为命名空间或类型创建别名。
using MyCompany = PC.Company; //命名空间的别名。
using Project = PC.Company.Project; //类型的别名
using引入命名空间,并不等于编译器编译时加载该命名空间所在的程序集,程序集的加载决定于程序中对该程序集是否存在调用操作,如果代码中不存在任何调用操作则编译器将不会加载using引入命名空间所在程序集。因此,在源文件开头,引入多个命名空间,并非加载多个程序集,不会造成“过度引用”的弊端。
using语句
using 语句允许程序员指定使用资源的对象应当何时释放资源。using 语句中使用的对象必须实现 IDisposable 接口。此接口提供了 Dispose 方法,该方法将释放此对象的资源。
①可以在 using 语句之中声明对象。
Font font2 = new Font("Arial", 10.0f);
using (font2)
{
// use font2
}
②可以在 using 语句之前声明对象。
using (Font font2 = new Font("Arial", 10.0f))
{
// use font2
}
③可以有多个对象与 using 语句一起使用,但是必须在 using 语句内部声明这些对象。
using (Font font3=new Font("Arial",10.0f), font4=new Font("Arial",10.0f))
{
// Use font3 and font4.
}
使用规则 :①using只能用于实现了IDisposable接口的类型,禁止为不支持IDisposable接口的类型使用using语句,否则会出现编译错误;
②using语句适用于清理单个非托管资源的情况,而多个非托管对象的清理最好以try-finnaly来实现,因为嵌套的using语句可能存在隐藏的Bug。内层using块引发异常时,将不能 释放外层using块的对象资源;
③using语句支持初始化多个变量,但前提是这些变量的类型必须相同
using(Pen p1 = new Pen(Brushes.Black), p2 = new Pen(Brushes.Blue))
{
//
}
④针对初始化对个不同类型的变量时,可以都声明为IDisposable类型
using (IDisposable font = new Font("Verdana", 12), pen = new Pen(Brushes.Black))
{
float size = (font as Font).Size;
Brush brush = (pen as Pen).Brush;
}
using实质 在程序编译阶段,编译器会自动将using语句生成为try-finally语句,并在finally块中调用对象的Dispose方法,来清理资源。所以,using语句等效于try-finally语句,
九、委托
委托是一种引用方法的类型。一旦为委托分配了方法,委托将与该方法具有完全相同的行为。委托方法的使用可以像其他任何方法一样,具有参数和返回值
public delegate int PerformCalculation(int x, int y);
// Declare a delegate delegate void Del(); class SampleClass { public void InstanceMethod() { System.Console.WriteLine("A message from the instance method."); } static public void StaticMethod() { System.Console.WriteLine("A message from the static method."); } } class TestSampleClass { static void Main() { SampleClass sc = new SampleClass(); // Map the delegate to the instance method: Del d = sc.InstanceMethod; d(); // Map to the static method: d = SampleClass.StaticMethod; d(); } }
合并和移除委托
delegate void Del(string s); class TestClass { static void Hello(string s) { System.Console.WriteLine(" Hello, {0}!", s); } static void Goodbye(string s) { System.Console.WriteLine(" Goodbye, {0}!", s); } static void Main() { Del a, b, c, d; // Create the delegate object a that references // the method Hello: a = Hello; // Create the delegate object b that references // the method Goodbye: b = Goodbye; // The two delegates, a and b, are composed to form c: c = a + b; // Remove a from the composed delegate, leaving d, // which calls only the method Goodbye: d = c - a; System.Console.WriteLine("Invoking delegate a:"); a("A"); System.Console.WriteLine("Invoking delegate b:"); b("B"); System.Console.WriteLine("Invoking delegate c:"); c("C"); System.Console.WriteLine("Invoking delegate d:"); d("D"); } }
九、集合
System.Collections命名空间包含接口和类(如列表,队列,哈希表和字典)
System.Collections.Generic:定义泛型集合的接口和类
System.Collectins.Specialized:专用的和强类型的集合(如链接的列表词典,位向量,只包含字符串的集合)
System.Collections概述:
System.Collections 名称空间包含了在你的应用程序中可以用到的6种内建通用集合。另一些更为专业化的集合则归属于 System.Collections.Specialized,在某些情况下你会发现这些专用集合也是非常有用的。加上一些异常(exception)类,这些专业化集合在功能上和内建集合是类似的。
System.Collections名称空间中的“内置”集合划分成了三种类别:
* 有序集合:仅仅实现ICollection接口的集合,在通常情况下,其数据项目的插入顺序控制着从集合中取出对象的的顺序。 如System.Collections.Stack和 System.Collections.Queue类都是ICollection集合的典型例子。
* 索引集合:实现Ilist的集合,其内容能经由从零开始的数字检索取出,就象数组一样。如System.Collections.ArrayList对象是索引集合的一个例子。
* 键式集合:实现 IDictionary 接口的集合,其中包含了能被某些类型的键值检索的项目。IDictionary集合的内容通常按键值方式存储,可以用枚举的方式排序检索。如 System.Collections.HashTable类实现了IDictionary 接口。
浙公网安备 33010602011771号