[. Net]: castingenumerable

Source: Internet
Author: User

 

Preface:

It is inevitable to write a program and use a custom function to transform the object array.
I wrote an object for this action and shared it with you.

 

Note:

This object has the following features:
1. Only when it is used can it be transformed. Instead of memory, create an array copy after the entire transformation.
2. You can add parameters during transformation.

 

Example:

namespace CLK.Collections.ConsoleApplication{    public class User    {        public string LastName = string.Empty;        public string FirstName = string.Empty;    }    public class Data    {        public string DisplayName = string.Empty;    }    class Program    {        static void Main(string[] args)        {                        User[] userArray = new User[2];            userArray[0] = new User();            userArray[0].LastName = "Chou";            userArray[0].FirstName = "Clark";            userArray[1] = new User();            userArray[1].LastName = "AAAA";            userArray[1].FirstName = "BBBBB";            IEnumerable<Data> dataList = CreateDataList(userArray, "★★★★★");            foreach (Data data in dataList)            {                Console.WriteLine(data.DisplayName);            }            Console.WriteLine("");                        Console.WriteLine("End");            Console.ReadLine();        }        static IEnumerable<Data> CreateDataList(IEnumerable<User> userList, string decorator)        {            return new CastingEnumerable<Data, User>(userList, delegate(User user) { return CreateData(user, decorator); });        }        static Data CreateData(User user, string decorator)        {            Data data = new Data();            data.DisplayName = decorator + user.LastName + "." + user.FirstName + decorator;            return data;        }    }  }

 

Program code:

namespace CLK.Collections{    public class CastingEnumerable<TResult, TSource> : IEnumerable<TResult>    {        //Properties         private readonly IEnumerable<TSource> _sourceEnumerable;        private CastingEnumerableDelegate<TResult, TSource> _enumerableDelegate;        // Construction         public CastingEnumerable(IEnumerable<TSource> sourceEnumerable, CastingEnumerableDelegate<TResult, TSource> enumerableDelegate)        {            Require#region Require            if (sourceEnumerable == null) throw new ArgumentNullException();            if (enumerableDelegate == null) throw new ArgumentNullException();            #endregion            _sourceEnumerable = sourceEnumerable;            _enumerableDelegate = enumerableDelegate;        }        // Member         public IEnumerator<TResult> GetEnumerator()        {            return new CastingEnumerator<TResult, TSource>(_sourceEnumerable.GetEnumerator(), _enumerableDelegate);        }        System.Collections.IEnumerator System.Collections.IEnumerable.GetEnumerator()        {            return this.GetEnumerator();        }    }}

 

namespace CLK.Collections{    public class CastingEnumerator<TResult, TSource> : IEnumerator<TResult>    {        //Properties         private readonly IEnumerator<TSource> _sourceEnumerator;        private readonly CastingEnumerableDelegate<TResult, TSource> _enumerableDelegate;        private TResult _currentResult = default(TResult);        // Construction         public CastingEnumerator(IEnumerator<TSource> sourceEnumerator, CastingEnumerableDelegate<TResult, TSource> enumerableDelegate)        {            Require#region Require            if (sourceEnumerator == null) throw new ArgumentNullException();            if (enumerableDelegate == null) throw new ArgumentNullException();            #endregion            _sourceEnumerator = sourceEnumerator;            _enumerableDelegate = enumerableDelegate;        }        public virtual void Dispose()        {            _sourceEnumerator.Dispose();            _currentResult = default(TResult);        }        // Member          public TResult Current        {            get            {                return _currentResult;            }        }        object System.Collections.IEnumerator.Current        {            get            {                return this.Current;            }        }        public bool MoveNext()        {            if (_sourceEnumerator.MoveNext() == true)            {                _currentResult = _enumerableDelegate(_sourceEnumerator.Current);                return true;            }            return false;        }        public void Reset()        {            _sourceEnumerator.Reset();            _currentResult = default(TResult);        }    }}

 

public delegate TResult CastingEnumerableDelegate<TResult, TSource>(TSource source);

 

Related information:

Http://stackoverflow.com/questions/504729/how-do-i-cast-a-listt-effectively
Http://msdn.microsoft.com/zh-tw/library/0yw3tz5k.aspx

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.