第2章C#语言基础.ppt

上传人:本田雅阁 文档编号:2252191 上传时间:2019-03-11 格式:PPT 页数:34 大小:116.01KB
返回 下载 相关 举报
第2章C#语言基础.ppt_第1页
第1页 / 共34页
第2章C#语言基础.ppt_第2页
第2页 / 共34页
第2章C#语言基础.ppt_第3页
第3页 / 共34页
亲,该文档总共34页,到这儿已超出免费预览范围,如果喜欢就下载吧!
资源描述

《第2章C#语言基础.ppt》由会员分享,可在线阅读,更多相关《第2章C#语言基础.ppt(34页珍藏版)》请在三一文库上搜索。

1、第2章 C#语言基础,ASP.NET Web应用程序设计教程 清华大学出版社 北京交通大学出版社,第2章 C#语言基础,2.1 数据类型与表达式 2.2 程序流程控制 2.3 类 2.4 泛型与集合 2.5 委托与事件 2.6 LINQ,2.1 数据类型与表达式,2.1.1 数据类型,数据类型,值类型,引用类型,简单类型,结构类型,枚举类型,类类型,接口类型,数组类型,委托类型,2.1.1 数据类型,1. 简单数据类型 整数类型sbyte,byte,short,ushort,int,uint,long,ulong,double 浮点类型float,double decimal类型 bool(t

2、rue和false) char(16位Unicode),2.1.1 数据类型,2. 结构 struct Rectangle public int x, y; /矩形左上角的坐标 public int width, height; /矩形的宽和高 public Rectangle(int a, int b, int w, int h) x = a; y = b; width = w; height = h; ,2.1.1 数据类型,3. 枚举 基类型默认为整型: 常用形式有: enum days Sun,Mon,Tue,Wed,Thu,Fri, Sat; /第一个成员值默认为0,第二个为1, e

3、num days Mon=1,Tue,Wed,Thu,Fri, Sat, Sun ; enum days mon=1,wed=3,sun,thu=8; 基类型为其他类型: enum num:longMax=2147483648L,Min=255L; long a=(long)Rang.Max;,2.1.1 数据类型,4. 数组 一维数组 数组必须在初始化之后才可以使用。例如: int Array=new int3; Array0=2; Array1=3; Array2=4; 或 int Array=new int32,3,4; int Array=new int 2,3,4;,2.1.1 数据

4、类型,多维数组 int, b= new int2,21,2,3,4; 交错数组 int c=new intnew int1,2,new int3,4,5;,2.1.1 数据类型,5. 字符串 字符串是使用 string 关键字声明的、由一个或多个字符构成的一组字符。 两种表达方式: 用双引号引起来。 用引起来。 例如, string FirstName = “Jones“; string DirName = “c:windows“;,2.1.1 数据类型,6. 日期与时间 使用DateTime结构创建、表示日期和时间。 使用Now属性获取系统日期。例如, DateTime Birthday =

5、 new DateTime(1990,8,14); DateTime today = DateTime.Now;,2.1.2 常量与变量,1. 常量 const int count = 30; 2. 变量 public static long id = 1027;/静态变量 public int number; /实例变量 var age = 30;/隐式变量 var name = “张三“; /隐式变量,2.2 程序流程控制,选择语句 switch表达式的类型可以为整型,字符型,string和枚举类型 循环语句 foreach(类型 变量名 in 集合) 循环语句; int list=10,

6、20,30; foreach(int m in list) Response.Write(m);,2.2 程序流程控制,异常处理try,catch,finally StreamReader st=null; try sr=File.Open(“c:test.txt”) catch(DirectoryNotFoundException e) Response.Write(e.Message); finally if(sr!=null) sr.Close(); ,2.3 类,2.3.1 类声明 类修饰符 class 类名 类体 public class Person private string

7、_name; private int _age; private long _ID; public Person(string n, int a, long i) _name = n; _age = a; _ID = i; public virtual string Speak() return “My Name is “ + _name + “ and my age is “+ _age.ToString(); ,2.3.2 类成员,1. 方法 声明 方法修饰符 返回类型 方法名(行参列表)方法体 参数 值参数 引用参数(ref) 输出参数(out) 参数数组(params),2.3.2 类

8、成员,void Swap(int a , int b ) int t; t = a; a = b; b = t; ,void Swap(ref int a , ref int b ) int t; t = a; a = b; b = t; ,int OutValue(int a , out char b) b = (char)a; return 0; ,int MultiPar(params int var) int sum = 0; for(int i= 0;ivar.Length;i +) sum += vari; return sum; ,2.3.2 类成员,2. 字段与属性 静态字段(

9、static) 实例字段 只读字段(readonly),2.3 类,set和get访问器 class Window private double m_width = 30; public double width getreturn m_width; setm_width = value; ,2.3.3 继承,类修饰符 class 类名 :基类 类体 public class Employee : Person private string _department; private decimal _salary; public Employee(string n , int a , long

10、 i , string d , decimal s) : base(n , a , i ) _department = d;_salary = s; public override string Speak() return base.Speak() + “.Salary is “ + _salary.ToString(); ,2.3.4 类的高级特性,1. 抽象类 abstract class Figure protected double x = 0 , y = 0; public Figure(double a, double b) x =a; y = b; public abstrac

11、t void Area(); ,2.3.4 类的高级特性,2. 密封类 sealed class SealedClass public double x = 0 , y = 0; public SealedClass(double a, double b) x =a; y = b; ,2.3.4 类的高级特性,3. 静态类 static class StaticClass public const int y=100; public static void Add(int x) y += x; ,2.3.4 类的高级特性,4. 分部类 将类或结构的定义分开放在多个文件中,编译时,由编译器把它们

12、合并在一起形成一个完整类,partial class Person private string _name; private int _age; private long _ID; public Person(string n, int a, long i) _name = n; _age = a; _ID = i; ,partial class Person public virtual string Speak() return “My Name is “ + _name + “ and my age is “+ _age.ToString(); ,2.3.4 类的高级特性,5. 匿名类

13、型 var 匿名对象名 = 对象初始化器; var book = new ISBN = “935-6-887-1568“, BookName = “ASP.NET Web应用程序设计“, Price = 30.5; var title = book.BookName;,2.4 泛型与集合,泛型即通过参数化类型来实现在同一份代码上操作多种数据类型。利用泛型也可以达到代码重用的目的。 1. 泛型类 修饰符 class 类名 类体 类名 实例名 = new 类名(构造函数的实参);,2.4 泛型与集合,class Stack private T s; int pos; public Stack(in

14、t size) s = new Tsize; pos = 0; public void Push(T val) spos = val; pos+; public T Pop() pos-; return spos; Stack s1 = new Stack(2); s1.Push(1); Stack s2 = new Stack(3);,2.4 泛型与集合,2. 泛型方法 public int Find(T values,T val) for(int i=0;ivalues.Length;i+) if(valuesi.Equals(val) return i; return -1; ,2.4

15、泛型与集合,3. 集合 List表示一个动态数组 List al = new List(); al.Add(“One“); al.Add(“Two“); Dictionary表示一个字典,是一个键/值对的集合 Dictionary ht = new Dictionary(); ht.Add(1, “one“);ht.Add(2, “two“);,2.5 委托与事件,1. 委托 委托属于引用类型,用于封装方法 delegate 返回值类型 标识符 (形参列表); public class Sort public delegate bool Compare(int a, int b); publi

16、c static void BubbleSort(int element, Compare compare) int ElementArray = new int10 23, 3, 58, 23, 90, 45, 12, 78, 25, 67 ; Sort.Compare cp = new Sort.Compare(Ascending); Sort.BubbleSort(ElementArray, cp);,2.5 委托与事件,2. 匿名方法 委托名 实例变量 = delegate(形参列表); delegate int AddOne(int v); int y = 10; AddOne ao

17、 = delegate(int val) val+; return val; ; y = ao(y);,2.5 委托与事件,3. 事件 修饰符 event 委托类型 事件名;,2.6 LINQ,语言集成查询(Language Integrated Query,LINQ)将标准查询功能集成到高级程序设计语言(如C#、VB.NET)中 1. 基本查询语法 var 查询结果 = from 范围变量 in 数据源 select 范围变量; from子句指定要查询的数据源和范围变量 select子句指定将在执行查询时返回值的类型,2.6 LINQ,2. 投影 指定元素的子集或对元素的运算结果 3. 筛选 从数据源中获取满足条件的数据子集。 var AllCourses = from c in courses where c.CourseName = “ASP.NET” ,2.6 LINQ,4. 排序 使用orderby子句,可以使查询结果有序排列。 var AllCourses = from c in courses orderby c.CourseNumber descending select c; 5. 分组 使用group子句,可以按指定的键分组结果。 var AllCourses = from c in courses group c by c.CourseName;,

展开阅读全文
相关资源
猜你喜欢
相关搜索

当前位置:首页 > 其他


经营许可证编号:宁ICP备18001539号-1