Func<> & Action<> 差別

rusli
Site Admin
文章: 212
註冊時間: 週三 7月 07, 2010 9:49 pm

Func<> & Action<> 差別

文章rusli » 週五 7月 08, 2011 2:15 pm

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);
                }
            }
        }



rusli
Site Admin
文章: 212
註冊時間: 週三 7月 07, 2010 9:49 pm

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

文章rusli » 週五 7月 08, 2011 2:53 pm

代碼: 選擇全部


     //// 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);
              }
          }


回到「C#」

誰在線上

正在瀏覽這個版面的使用者:沒有註冊會員 和 1 位訪客