xiacy

导航

2012年5月1日 #

5.4.1 将匿名方法用于Action<T>委托类型

摘要: Action<string> printReverse = delegate(string text) { char[] chars = text.ToCharArray(); Array.Reverse(chars); Console.WriteLine(new string(chars)); }; Action<int> printRoot = delegate(int number) { ... 阅读全文

posted @ 2012-05-01 22:57 xiacy 阅读(270) 评论(0) 推荐(0) 编辑

5.3.3 C#1和C#2之间的一处重大改变

摘要: delegate void SampleDelegate(string x); public class SinPpet { public void CandidateAction(string x) { Console.WriteLine("Snippet.CandidateAction"); } } public class Derived : SinPpet { public void CandidateAction(object o) { ... 阅读全文

posted @ 2012-05-01 22:23 xiacy 阅读(189) 评论(0) 推荐(0) 编辑

5.3.2 委托返回类型的协变性

摘要: delegate Stream StreamFactory(); static MemoryStream GenerateSampleData() { byte[] buffer = new byte[16]; for (int i = 0; i < buffer.Length; i++) { buffer[i] = (byte)i; } return new MemoryStream(buffer); ... 阅读全文

posted @ 2012-05-01 22:13 xiacy 阅读(151) 评论(0) 推荐(0) 编辑

5.2.1 委托参数的逆变性

摘要: static void LogPlainEvent(object sender, EventArgs e) { Console.WriteLine("LogPlain"); } static void LogKeyEvent(object sender, KeyPressEventArgs e) { Console.WriteLine("LogKey"); } static void LogMouseEvent(object sender, MouseEventArg... 阅读全文

posted @ 2012-05-01 22:12 xiacy 阅读(163) 评论(0) 推荐(0) 编辑

5.1.1 订阅三个按钮事件

摘要: static void LogPlainEvent(object sender, EventArgs e) { Console.WriteLine("LogPlain"); } static void LogKeyEvent(object sender, KeyPressEventArgs e) { Console.WriteLine("LogKey"); } static void LogMouseEvent(object sender, Mouse... 阅读全文

posted @ 2012-05-01 21:55 xiacy 阅读(159) 评论(0) 推荐(0) 编辑

4.3.2 使用null进行赋值和比较

摘要: class Person { DateTime birth; DateTime? death; string name; public TimeSpan Age { get { if (death.HasValue) return death.Value - birth; else return DateTime.Now - birth... 阅读全文

posted @ 2012-05-01 15:16 xiacy 阅读(186) 评论(0) 推荐(0) 编辑

4.2.1 使用Nullable<T>的各个成员

摘要: static void Display(Nullable<int> x) { Console.WriteLine("HasValue:{0}", x.HasValue); if (x.HasValue) { Console.WriteLine("Value:{0}", x.Value); Console.WriteLine("Explicit conversion:{0}", (int)x); } ... 阅读全文

posted @ 2012-05-01 14:38 xiacy 阅读(209) 评论(0) 推荐(0) 编辑

3.4.3 一个完整的泛型枚举---从0枚举到9

摘要: class CountingEnumerable : IEnumerable<int> { #region IEnumerable<int> 成员 public IEnumerator<int> GetEnumerator() { return new CountingEnumerator(); } #endregion #region IEnumerable 成员 IEnumerator IEnumerable.GetEnumerator() {... 阅读全文

posted @ 2012-05-01 13:57 xiacy 阅读(183) 评论(0) 推荐(0) 编辑

3.4.2 泛型类型的静态构造函数

摘要: class Other<T> { public class Inner<U, V> { static Inner() { Console.WriteLine("Other<{0}>.Inner<{1},{2}>", typeof(T).Name, typeof(U).Name, typeof(V).Name); } public static void DummyMethod() { } } } class P... 阅读全文

posted @ 2012-05-01 13:46 xiacy 阅读(303) 评论(0) 推荐(0) 编辑

3.4.1 证明不同 服装类型具有不同的静态字段

摘要: class TypeWithField<T> { public static string field; public static void PrintField() { Console.WriteLine(field + ":" + typeof(T).Name); } } class Program { static void Main(string[] args) { TypeWithField<int>.field = "fi... 阅读全文

posted @ 2012-05-01 13:38 xiacy 阅读(210) 评论(0) 推荐(0) 编辑

3.3.3 用==和 != 进行引用比较

摘要: static bool AreReferencesEqual<T>(T first, T second) where T:class { return first == second; } static void Main(string[] args) { string name = "Jon"; string intro1 = "My name is :" + name; string intro2 = "My name is :" + name; ... 阅读全文

posted @ 2012-05-01 12:42 xiacy 阅读(201) 评论(0) 推荐(0) 编辑

3.3.3 以泛型方式将一个给定的值和默认值比较

摘要: static int ComparaeToDefaults<T>(T value) where T : IComparable<T> { return value.CompareTo(default(T)); } static void Main(string[] args) { Console.WriteLine(ComparaeToDefaults("x")); Console.WriteLine(ComparaeToDefaults("")); ... 阅读全文

posted @ 2012-05-01 12:00 xiacy 阅读(143) 评论(0) 推荐(0) 编辑

3.2.2 在非泛型类型中实现泛型方法

摘要: static List<T> MakeList<T>(T first, T second) { List<T> list = new List<T>(); list.Add(first); list.Add(second); return list; } static void Main(string[] args) { List<string> list = MakeList<string>("Line 1", "Line 2... 阅读全文

posted @ 2012-05-01 11:46 xiacy 阅读(283) 评论(0) 推荐(0) 编辑

3.2.1 泛型方法(List<T>.ConvertAll<TOutput>方法实战)

摘要: static double TakeSqrt(int x) { return Math.Sqrt(x); } static void Main(string[] args) { List<int> integers = new List<int>(); integers.Add(1); integers.Add(2); integers.Add(3); integers.Add(4); ... 阅读全文

posted @ 2012-05-01 11:37 xiacy 阅读(357) 评论(0) 推荐(0) 编辑

3.2.1 泛型例子,泛型字典统计文本中的单词数

摘要: static Dictionary<string, int> CountWords(string text) { Dictionary<string, int> frequencies; frequencies = new Dictionary<string, int>(); string[] words = Regex.Split(text, @"\W+"); foreach (string word in words) { ... 阅读全文

posted @ 2012-05-01 10:53 xiacy 阅读(312) 评论(0) 推荐(0) 编辑