xiacy

导航

05 2012 档案

8.2 简化的初始化
摘要:public class Person{ public int Age { get; set; } public string Name { get; set; } List<Person> friends = new List<Person>(); public List<Person> Friends { get { return friends; } } Location home = new Location(); public Location Home { get { return home; } } public Person() { }... 阅读全文

posted @ 2012-05-29 23:22 xiacy 阅读(190) 评论(0) 推荐(0)

6.2.3 迭代器的执行流程(yield break 语句)
摘要:static IEnumerable<int> CountWithTimeLimit(DateTime limit){ for (int i = 1; i <= 100; i++) { if (DateTime.Now >= limit) yield break; yield return i; }}static void Main(string[] args){ DateTime stop = DateTime.Now.AddSeconds(2); foreach (int i in CountWithTimeL... 阅读全文

posted @ 2012-05-14 00:13 xiacy 阅读(207) 评论(0) 推荐(0)

6.2.2 观察迭代器的工作流程
摘要:static readonly string Padding = new string(' ', 30);static IEnumerable<int> CreateEnumerable(){ Console.WriteLine("{0}start of CreateEnumerable()", Padding); for (int i = 0; i < 3; i++) { Console.WriteLine("{0}about to yield {1}", Padding, i); yield return i; Co 阅读全文

posted @ 2012-05-10 22:52 xiacy 阅读(152) 评论(0) 推荐(0)

6.2.1 迭代器块和 yield return 简介
摘要:利用c#2和 yield return 来迭代示例集合class Program{ static void Main(string[] args) { object[] values = { "a", "b", "c", "d", "e" }; IterationSample collection = new IterationSample(values, 1); foreach (object x in collection) { Console.WriteLine(x); } }}... 阅读全文

posted @ 2012-05-10 22:30 xiacy 阅读(279) 评论(0) 推荐(0)

5.5.6 共享和非共享的变量混合使用
摘要:MethodInvoker[] delegates = new MethodInvoker[2];int outside = 0;for(int i=0;i<2;i++){ int inside = 0; delegates[i] = delegate { Console.WriteLine("【outside:{0},inside:{1}】", outside, inside); outside++; inside++; };}MethodInvoker first=delegates[0];MethodInvoker sec... 阅读全文

posted @ 2012-05-06 12:36 xiacy 阅读(147) 评论(0) 推荐(0)

5.5.5 局部变量的实例化
摘要:List<MethodInvoker> list = new List<MethodInvoker>();for (int i = 0; i < 5; i++){ int counter = i * 10; list.Add(delegate { Console.WriteLine(counter); counter++; });}foreach (MethodInvoker t in list){ t();}list[0]();list[0]();list[0]();list[1]();输出结果01020304012311 阅读全文

posted @ 2012-05-06 12:07 xiacy 阅读(157) 评论(0) 推荐(0)

5.5.4 捕获变量的延长生存期
摘要:static void Main(string[] args){ MethodInvoker x = CreateDelegateInstance(); x(); x(); x();}static MethodInvoker CreateDelegateInstance(){ int counter = 5; MethodInvoker ret = delegate { Console.WriteLine(counter); counter++; }; ret(); return ret;}输出结果为:56... 阅读全文

posted @ 2012-05-06 12:00 xiacy 阅读(195) 评论(0) 推荐(0)

5.5.2 测试被捕获的变量的行为
摘要:string captured = "before x is created";MethodInvoker x = delegate{ Console.WriteLine(captured); captured = "changed by x";};Console.WriteLine(captured);captured = "directly before x is invoked";x();Console.WriteLine(captured);captured = "before second invocation&q 阅读全文

posted @ 2012-05-06 11:54 xiacy 阅读(149) 评论(0) 推荐(0)

5.5.1 定义闭包和不同类型
摘要:int outerVariable = 5;string capturedVariable = "captured";if (DateTime.Now.Hour == 23){ int normalLocalVariable = DateTime.Now.Minute; Console.WriteLine(normalLocalVariable);}MethodInvoker x = delegate(){ string anonLocal = "local to anonymous method"; Console.WriteLine("{0 阅读全文

posted @ 2012-05-06 11:46 xiacy 阅读(165) 评论(0) 推荐(0)

5.4.2 匿名方法的返回值
摘要:从匿名方法放回一个值Predicate<int> isEven = delegate(int n) { return n % 2 == 0; };Console.WriteLine(isEven(1));Console.WriteLine(isEven(2));Console.WriteLine(isEven(3));Console.WriteLine(isEven(4));Console.WriteLine(isEven(5));Console.ReadKey();用匿名方法简便的排序文件static void SortAndShowFiles(string titile, Co 阅读全文

posted @ 2012-05-06 11:01 xiacy 阅读(267) 评论(0) 推荐(0)

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 阅读(280) 评论(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 阅读(202) 评论(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 阅读(161) 评论(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 阅读(170) 评论(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 阅读(167) 评论(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 阅读(193) 评论(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 阅读(217) 评论(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 阅读(192) 评论(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 阅读(314) 评论(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 阅读(218) 评论(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 阅读(205) 评论(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 阅读(152) 评论(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 阅读(294) 评论(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 阅读(368) 评论(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 阅读(321) 评论(0) 推荐(0)