1 頁 (共 1 頁)

Func<> & Action<> 差別

發表於 : 週五 7月 08, 2011 2:15 pm
rusli
Action 無回傳型態
Func 有回傳型態

代碼: 選擇全部


MapField<IMobilePhone>(num, m.ContactMechType.Replace(m.VirtualType, string.Empty) , person,
                                           d => d.ContactNumber == num,
                                           (isPrimary, contactMechTypeName, value) =>
                                           person.AddMobilePhone(isPrimary, contactMechTypeName, null, value));


代碼: 選擇全部


        private void MapField<T>(string value, string contactMechTypeName, IPerson person, Func<T, bool> pridacate,
                                Action<bool, string, string> addMechs)
        {
            if (!string.IsNullOrEmpty(value))
            {
                IList<T> phones = person.ContactMechs.OfType<T>().ToList();

                if (phones.Count() > 0)
                {
                    if (phones.Where(pridacate).Count() == 0)
                    {
                        addMechs.Invoke(false, contactMechTypeName, value);
                    }
                }
                else
                {
                    addMechs.Invoke(true, contactMechTypeName, value);
                }
            }
        }



[間單版本] Func<> & Action<> 差別

發表於 : 週五 7月 08, 2011 2:53 pm
rusli

代碼: 選擇全部


     //// d 為 IMobilePhone
            MyFunction<IMobilePhone>(d => d.ContactNumber == 1,
                (bool參數1, 字串參數2, 整數參數3) => DoSomething(bool參數1, 字串參數2, 整數參數3));



代碼: 選擇全部

        private void DoSomething(bool a, string b, int c)
        {
            //// do
        }


代碼: 選擇全部

        private void MyFunction<T>(Func<T, bool> pridacate, Action<bool, string, int> addMechs)
          {
              IList<T> p = MyAllPeoples.OfType<T>().ToList();

              //// 執行 Func
              ////  pridacate  回傳 bool 參數型別 為T
              if (p.Where(pridacate).Count() == 0)
              {
                  //// 執行Action 丟三個參數
                  addMechs.Invoke(false, "字串", 1);
              }
          }