摘要: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() { }...
阅读全文
摘要: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...
阅读全文
摘要: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
阅读全文
摘要:利用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); } }}...
阅读全文
摘要: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...
阅读全文
摘要: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
阅读全文
摘要: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...
阅读全文
摘要: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
阅读全文
摘要: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
阅读全文
摘要:从匿名方法放回一个值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
阅读全文
摘要:Action<string> printReverse = delegate(string text) { char[] chars = text.ToCharArray(); Array.Reverse(chars); Console.WriteLine(new string(chars)); }; Action<int> printRoot = delegate(int number) { ...
阅读全文
摘要: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) { ...
阅读全文
摘要: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); ...
阅读全文
摘要: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...
阅读全文
摘要: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...
阅读全文
摘要:class Person { DateTime birth; DateTime? death; string name; public TimeSpan Age { get { if (death.HasValue) return death.Value - birth; else return DateTime.Now - birth...
阅读全文
摘要: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); } ...
阅读全文
摘要:class CountingEnumerable : IEnumerable<int> { #region IEnumerable<int> 成员 public IEnumerator<int> GetEnumerator() { return new CountingEnumerator(); } #endregion #region IEnumerable 成员 IEnumerator IEnumerable.GetEnumerator() {...
阅读全文
摘要: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...
阅读全文
摘要: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...
阅读全文
摘要: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; ...
阅读全文
摘要: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("")); ...
阅读全文
摘要: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...
阅读全文
摘要: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); ...
阅读全文
摘要: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) { ...
阅读全文