DEV 331深度探索 Microsoft Visual C# 2.0.ppt
《DEV 331深度探索 Microsoft Visual C# 2.0.ppt》由会员分享,可在线阅读,更多相关《DEV 331深度探索 Microsoft Visual C# 2.0.ppt(32页珍藏版)》请在麦多课文档分享上搜索。
1、DEV 331 深度探索 Microsoft Visual C# 2.0,付仲恺 微软特邀开发专家,C# 2.0增强,范型(Generics) 匿名方法(Anonymous methods) 可为空类型(Nullable types) 迭代器(Iterators) 局部类型(Partial types) 等等100%向后兼容,public class List private object elements;private int count;public void Add(object element) if (count = elements.Length) Resize(count *
2、2);elementscount+ = element;public object thisint index get return elementsindex; set elementsindex = value; public int Count get return count; ,范型(Generics),public class List private T elements;private int count;public void Add(T element) if (count = elements.Length) Resize(count * 2);elementscount
3、+ = element;public T thisint index get return elementsindex; set elementsindex = value; public int Count get return count; ,List intList = new List();intList.Add(1); intList.Add(2); intList.Add(“Three“);int i = (int)intList0;,List intList = new List();intList.Add(1); / Argument is boxed intList.Add(
4、2); / Argument is boxed intList.Add(“Three“); / Should be an errorint i = (int)intList0; / Cast required,List intList = new List();intList.Add(1); / No boxing intList.Add(2); / No boxing intList.Add(“Three“); / Compile-time errorint i = intList0; / No cast required,范型(Generics),为什么需要范型? 类型检查,不进行拆箱和类
5、型强制转换 增加了代码重用机会(类型集合) C#如何实现范型? 在运行时实例化,而不是编译时 类型检查在声明时,而不是实例化时进行 同时支持引用和值类型 完整的运行时类型信息,范型(Generics),类型参数能够被应用于: 类,结构体,接口,委托类型,class Dictionary .struct HashBucket .interface IComparer .delegate R Function(A arg);,Dictionary customerLookupTable;Dictionary orderLookupTable;Dictionary wordCount;,范型(Gen
6、erics),类型参数能够被应用于: 类,结构体,接口,委托类型 方法,class Utils public static T CreateArray(T value, int size) T result = new Tsize;for (int i = 0; i size; i+) resulti = value;return result; ,string names = Utils.CreateArray(“, 10); int numbers = Utils.CreateArray(-1, 100);,string names = Utils.CreateArray(“, 10);
7、int numbers = Utils.CreateArray(-1, 100);,类型推断!,范型(Generics),类型参数能够被应用于: 类,结构体,接口,委托类型 方法 类型参数能够具有约束,class Dictionary public void Add(K key, V value) .if (IComparable)key).CompareTo(x) = 0) . ,class Dictionary where K: IComparable public void Add(K key, V value) .if (key.CompareTo(x) = 0) . ,class D
8、ictionary: IDictionarywhere K: IComparablewhere V: IKeyProvider, IPersistable, new() public void Add(K key, V value) . ,范型(Generics),只允许有一个基本约束 实际类,或者结构体 可以有多个次要约束 接口或者类型参数 可以有一个构造约束 new(),class Link where T: class .class Nullable where T: struct .class Relation where T: class where U: T .,范型(Generi
9、cs),default(T)类型转换Null检查,void Foo() T x = null; / ErrorT y = default(T); / Ok ,void Foo(T x) int i = (int)x; / Errorint j = (int)(object)x; / Ok ,void Foo(T x) if (object)x = null) . ,范型(Generics),集合类集合接口集合基本类工具类反射,List Dictionary SortedDictionary Stack Queue,IList IDictionary ICollection IEnumerabl
10、e IEnumerator IComparable IComparer,Collection KeyedCollection ReadOnlyCollection,Nullable EventHandler Comparer,范型(Generics)性能,匿名方法(Anonymous Methods),delegate bool Predicate(T item);public class List public List FindAll(Predicate filter) List result = new List();foreach (T item in this) if (filter
11、(item) result.Add(item);return result; ,public class Bank List accounts;List GetOverdrawnAccounts() return accounts.FindAll(new Predicate(IsOverdrawn);static bool IsOwerdrawn(Account a) return a.Balance 0; ,public class Bank List accounts;List GetOverdrawnAccounts() return accounts.FindAll(delegate(
12、Account a) return a.Balance 0; ); ,匿名方法(Anonymous Methods),代码块替代委托方法 自动推断委托类型 代码块可以忽略参数列表,button.Click += delegate MessageBox.Show(“Hello“); ;,button.Click += delegate(object sender, EventArgs e) MessageBox.Show(Button)sender).Text); ;,匿名方法(Anonymous Methods),支持Closures,public class Bank List accoun
13、ts;List GetOverdrawnAccounts() return accounts.FindAll(delegate(Account a) return a.Balance 0; ); ,public class Bank List accounts;List GetOverdrawnAccounts() return accounts.FindAll(delegate(Account a) return a.Balance GetLargeAccounts(double minBal) return accounts.FindAll(delegate(Account a) retu
14、rn a.Balance = minBal; ); ,public class Bank List GetLargeAccounts(double minBal) Helper helper = new Helper();helper.minBal = minBal;return accounts.FindAll(helper.Matches);internal class Helperinternal double minBal;internal bool Matches(Account a) return a.Balance = minBal; ,匿名方法(Anonymous Method
- 1.请仔细阅读文档,确保文档完整性,对于不预览、不比对内容而直接下载带来的问题本站不予受理。
- 2.下载的文档,不会出现我们的网址水印。
- 3、该文档所得收入(下载+内容+预览)归上传者、原创作者;如果您是本文档原作者,请点此认领!既往收益都归您。
下载文档到电脑,查找使用更方便
2000 积分 0人已下载
下载 | 加入VIP,交流精品资源 |
- 配套讲稿:
如PPT文件的首页显示word图标,表示该PPT已包含配套word讲稿。双击word图标可打开word文档。
- 特殊限制:
部分文档作品中含有的国旗、国徽等图片,仅作为作品整体效果示例展示,禁止商用。设计者仅对作品中独创性部分享有著作权。
- 关 键 词:
- DEV331 深度 探索 MICROSOFTVISUALC20PPT
