楼主: Lisrelchen
1452 13

C# 3.0 Cookbook, 3rd Edition [推广有奖]

  • 0关注
  • 62粉丝

VIP

已卖:4194份资源

院士

67%

还不是VIP/贵宾

-

TA的文库  其他...

Bayesian NewOccidental

Spatial Data Analysis

东西方数据挖掘

威望
0
论坛币
50288 个
通用积分
83.6306
学术水平
253 点
热心指数
300 点
信用等级
208 点
经验
41518 点
帖子
3256
精华
14
在线时间
766 小时
注册时间
2006-5-4
最后登录
2022-11-6

楼主
Lisrelchen 发表于 2017-7-15 09:35:10 |AI写论文

+2 论坛币
k人 参与回答

经管之家送您一份

应届毕业生专属福利!

求职就业群
赵安豆老师微信:zhaoandou666

经管之家联合CDA

送您一个全额奖学金名额~ !

感谢您参与论坛问题回答

经管之家送您两个论坛币!

+2 论坛币


本帖隐藏的内容

C# 3.0 Cookbook, 3rd Edition.pdf (4.65 MB)


二维码

扫码加我 拉你入群

请注明:姓名-公司-职位

以便审核进群资格,未注明则拒绝

关键词:Cookbook Edition dition editio Book

沙发
hjtoh 发表于 2017-7-15 09:43:15
谢谢分享

藤椅
Lisrelchen 发表于 2017-7-15 09:43:49
  1. 6.1. Creating an lterator on a Generic Type

  2. Problem

  3. You want elements contained in your generic type to be enumerated using the foreach statement.

  4. Solution

  5. Add an iterator to your generic type, as shown here:

  6.         public class ShoppingList<T> : IEnumerable<T>
  7.         {
  8.             private List<T> _items = new List<T>();

  9.             public void Add(T name)
  10.             {
  11.                 _items.Add(name);
  12.             }

  13.             public IEnumerator<T> GetEnumerator()
  14.             {
  15.                  return _items.GetEnumerator();
  16.             }
  17.         }
复制代码

板凳
Lisrelchen 发表于 2017-7-15 09:44:27
  1. 6.2. Creating an Iterator on a Nongeneric Type

  2. Problem

  3. You want to be able to access elements contained in your nongeneric collection type using the foreach statement.

  4. Solution

  5. Implement IEnumerable on your nongeneric type:

  6.         public class StampCollection : IEnumerable
  7.         {
  8.             private Dictionary<string, Stamp> _stamps =
  9.                 new Dictionary<string, Stamp>();

  10.             public void Add(Stamp stamp)
  11.             {
  12.                 _stamps.Add(stamp.Name, stamp);
  13.             }
  14.             public IEnumerator GetEnumerator()
  15.             {
  16.                 // Return all stamps in the stamp collection
  17.                 // in order of publication
  18.                 var orderedStamps = from Stamp stamp in _stamps.Values
  19.                                     orderby stamp.Year
  20.                                     select stamp;
  21.                 foreach (Stamp stamp in orderedStamps)
  22.                 {
  23.                     yield return stamp;
  24.                 }
  25.             }
  26.         }

  27.         public class Stamp
  28.         {
  29.             public Stamp(int year, string name)
  30.             {
  31.                 this.Year = year;
  32.                 this.Name = name;
  33.             }
  34.             public int Year { get; set; }
  35.             public string Name { get; set; }

  36.             public override string ToString()
  37.             {
  38.                 return this.Year + ":" + this.Name;
  39.             }
  40.         }
复制代码

报纸
Lisrelchen 发表于 2017-7-15 09:45:47
  1. 6.3. Creating Custom Enumerators
  2. Problem
  3. You need to add foreach support to a class, but the normal way of adding an iterator (i.e., implementing IEnumerable on a type and returning a reference to this IEnumerable from a member function) is not flexible enough. Instead of simply iterating from the first element to the last, you also need to iterate from the last to the first, and you need to be able to step over, or skip, a predefined number of elements on each iteration. You want to make all of these types of iterators available to your class.

  4. Solution
  5. The Container<T> class shown in Example 6-1 acts as a container for a private List<T> called internalList. Container is implemented so you can use it in a foreach loop to iterate through the private internalList.

  6. Example 6-1. Creating custom iterators
  7. public class Container<T> : IEnumerable<T>
  8. {
  9.     public Container() {}

  10.     private List<T> _internalList = new List<T>();

  11.     // This iterator iterates over each element from first to last
  12.     public IEnumerator<T> GetEnumerator()
  13.     {
  14.         return _internalList.GetEnumerator();
  15.     }

  16.     // This iterator iterates over each element from last to first
  17.     public IEnumerable<T> GetReverseOrderEnumerator()
  18.     {
  19.         foreach (T item in ((IEnumerable<T>)_internalList).Reverse())
  20.         {
  21.                 yield return item;
  22.         }
  23.     }
  24.     // This iterator iterates over each element from last to first stepping
  25.     // over a predefined number of elements
  26.     public IEnumerable<T> GetReverseStepEnumerator(int step)
  27.     {
  28.         foreach (T item in ((IEnumerable<T>)_internalList).Reverse().EveryNthItem(step))
  29.         {
  30.             yield return item;
  31.         }
  32.     }
  33.     #region IEnumerable Members

  34.     IEnumerator IEnumerable.GetEnumerator()
  35.     {
  36.         return GetEnumerator();
  37.     }

  38.     #endregion

  39.     public void Clear()
  40.     {
  41.         _internalList.Clear();
  42.     }

  43.     public void Add(T item)
  44.     {
  45.         _internalList.Add(item);
  46.     }

  47.     public void AddRange(ICollection<T> collection)
  48.     {
  49.         _internalList.AddRange(collection);
  50.     }
  51. }
复制代码

地板
Lisrelchen 发表于 2017-7-15 09:46:18
  1. 6.4. Implementing Iterator Logic
  2. Problem
  3. Iterators need to provide access to the data elements in the collections they are implemented for. You need a good way to work with sets of data.

  4. Solution
  5. Use LINQ to implement iterator logic, as shown in Example 6-2. The highlighted items are just a few parts of LINQ that can help you with the implementation of iterator logic.

  6. Example 6-2. Implementing iterator logic with LINQ
  7. public class SectionalList<T> : IEnumerable<T>
  8. {
  9.         private List<T> _items = new List<T>();

  10.         public void Add(T item)
  11.         {
  12.                 _items.Add(item);
  13.         }

  14.     public IEnumerator<T> GetEnumerator()
  15.     {
  16.         return _items.GetEnumerator();
  17.     }

  18.     IEnumerator IEnumerable.GetEnumerator()
  19.     {
  20.         return GetEnumerator();
  21.     }

  22.         public IEnumerable<T> GetFirstHalf()
  23.         {
  24.                 foreach(T item in _items.Take(_items.Count / 2))
  25.                 {
  26.                         yield return item;
  27.                 }
  28.         }
  29.         public IEnumerable<T> GetSecondHalf()
  30.         {
  31.         foreach (T item in _items.Skip(_items.Count / 2))
  32.         {
  33.             yield return item;
  34.         }
  35.     }

  36.     public IEnumerable<T> GetFilteredValues(Func<T, bool> predicate)
  37.     {
  38.         foreach (T item in _items.TakeWhile(predicate))
  39.         {
  40.             yield return item;
  41.         }
  42.     }

  43.     public IEnumerable<T> GetReverseFilteredValues(Func<T, bool> predicate)
  44.     {
  45.         foreach (T item in _items.SkipWhile(predicate))
  46.         {
  47.             yield return item;
  48.         }
  49.     }
  50. }
复制代码

7
Lisrelchen 发表于 2017-7-15 09:47:19
  1. 6.5. Forcing an Iterator to Stop Iterating
  2. Problem
  3. You have a requirement that if an iterator encounters malformed or out-of-bounds data that the iterations are to stop immediately.

  4. Solution
  5. It is possible to throw an exception from an iterator, which terminates the iterator and the foreach loop, but a controlled stop to an iterator should not be an exceptional condition. To do this, you can use the yield break statement within your iterator:

  6.         public class UpperLimitList<T> : IEnumerable<T>
  7.         {
  8.                 private List<T> _items = new List<T>();
  9.                 private bool noMoreItemsCanBeAdded;
  10.                 private int upperLimit = -1;

  11.                 public int UpperLimit
  12.                 {
  13.                         get {return (upperLimit);}
  14.                         set {upperLimit = value;}
  15.                 }

  16.                 public void Add(T name)
  17.                 {
  18.                         _items.Add(name);
  19.                 }

  20.                 public IEnumerator<T> GetEnumerator()
  21.                 {
  22.                         for (int index = 0; index < _items.Count; index++)
  23.                         {
  24.                                 if (noMoreItemsCanBeAdded)
  25.                                 {
  26.                                         yield break;
  27.                                 }
  28.                                 else
  29.                                 {
  30.                                         if (upperLimit >= 0 && index >= upperLimit-1)
  31.                                         {
  32.                                                 noMoreItemsCanBeAdded = true;
  33.                                         }

  34.                                         yield return (_items[index]);
  35.                                 }
  36.                         }
  37.                 }

  38.             IEnumerator IEnumerable.GetEnumerator()
  39.             {
  40.                 return GetEnumerator();
  41.             }
  42.         }
复制代码

8
Lisrelchen 发表于 2017-7-15 09:48:42
  1. 6.6. Dealing with Finally Blocks and Iterators
  2. Problem
  3. You have added a try/finally block to your iterator, and you notice that the finally block is not being executed when you think it should.

  4. Solution
  5. Wrap a try block around the iteration code in the GetEnumerator iterator with a finally block following this try block:

  6.         public class StringSet : IEnumerable<string>
  7.         {
  8.             private List<string> _items = new List<string>();

  9.             public void Add(string value)
  10.             {
  11.                 _items.Add(value);
  12.             }
  13.             public IEnumerator<string> GetEnumerator()
  14.             {
  15.                 try
  16.                 {
  17.                     for (int index = 0; index < _items.Count; index++)
  18.                     {
  19.                         yield return (_items[index]);
  20.                     }
  21.                 }
  22.                 finally
  23.                 {
  24.                     // Only executed at end of foreach loop (including on yield break)
  25.                     Console.WriteLine("In iterator finally block");
  26.                 }
  27.             }
  28.             #region IEnumerable Members

  29.             IEnumerator IEnumerable.GetEnumerator()
  30.             {
  31.                 return GetEnumerator();
  32.             }


  33.             #endregion
  34.         }
复制代码

9
Lisrelchen 发表于 2017-7-15 09:49:59
  1. 6.8. Organizing Your Interface Implementations
  2. Problem
  3. You have a class that implements an interface with many methods. These methods support only the interface functionality and don’t necessarily relate well to the other code in your class. You would like to keep the interface implementation code separate from the main class code.

  4. Solution
  5. Use partial classes to separate the interface implementation code into a separate file. For example, you have a class called TriValue that takes three decimal values and performs some operations on them, such as getting the average, the sum, and the product. This code is currently in a file called TriValue.cs, which contains:

  6.         public partial class TriValue
  7.         {
  8.             public decimal First { get; set; }
  9.             public decimal Second { get; set; }
  10.             public decimal Third { get; set; }
  11.             public TriValue(decimal first, decimal second, decimal third)
  12.             {
  13.                 this.First = first;
  14.                 this.Second = second;
  15.                 this.Third = third;
  16.             }

  17.             public TypeCode GetTypeCode()
  18.             {
  19.                 return TypeCode.Object;
  20.             }

  21.             public decimal Average
  22.             {
  23.                 get { return (Sum / 3); }
  24.             }

  25.             public decimal Sum
  26.             {
  27.                 get { return First + Second + Third; }
  28.             }

  29.             public decimal Product
  30.             {
  31.                 get { return First * Second * Third; }
  32.             }
  33.         }
复制代码

10
MouJack007 发表于 2017-7-15 09:51:02
谢谢楼主分享!

您需要登录后才可以回帖 登录 | 我要注册

本版微信群
加好友,备注jltj
拉您入交流群
GMT+8, 2026-1-5 09:41