High-quality code written in C # program to improve the 157 recommendations [16-19] [dynamic arrays, loop through, a collection of objects initialization]

Foreword

    Software development process, will inevitably use collections, C # is a collection of performance array and several collections. Whether or an array of collections, they have their own advantages and disadvantages. How good is that we must master the collection of skills in the development process. Do not underestimate these techniques, once used the wrong set of development or for a set of methods, the application will depart your expectations and run.

  This article has been updated to http://www.cnblogs.com/aehyok/p/3624579.html  . In this paper, learning record the following:

  Recommendation 16, the case of a variable number of elements in the array should not be used

  Recommendation 17, using foreach loop iterates carried out in most cases

  Recommendation 18, foreach is not a substitute for

  Recommendation 19, the use of more efficient collection of objects and initialization

Recommendation 16, the case of a variable number of elements in the array should not be used

  In C #, once an array is created, the length can not be changed. If we need a dynamic and variable-length collection, it should be created using ArrayList or a List <T>. The array itself, especially the one-dimensional array, in the face of high efficiency requirements of the algorithm will be specifically optimized to enhance its efficiency. Vector has become one-dimensional array, which is the best performance, the use of a special instruction in IL to handle them.

  From the point of view in terms of memory usage, the array has the following characteristics:

  1, when creating an array of memory is allocated a fixed length.

  2, if the value of the array element type, each element of a length equal to the length of a value corresponding to the type of

  3, if the element of the array is a reference type, then the length of each element for reference types IntPtr.Size.

  4, once the storage array structure is allocated, no longer changes.

  And ArryaList is this:

  1, ArrayList list structure is, memory space can be dynamically increased or decreased.

  2, if the type is a value ArrayList storage, it will increase the space of 12 bytes for each element, wherein the 4 bytes for the object reference, an 8-byte packing element is introduced when the object header.

  The List <T> is a generic implementation of ArrayList, which eliminates the overhead of unpacking and packing brings.

If the length must be a method of dynamically changing the array is converted to an array or ArrayList List <T>, as shown in the following code:

Copy the code
            /// define a one-dimensional array
             int [] IARR = { 0, . 1, . 3, . 4, . 6, . 7, . 9 }; /// the array into the ArrayList arrayListInt = the ArrayList ArrayList.Adapter (IARR); arrayListInt.Add ( 11 ); /// converting the array to List <T> List < int> listInt iArr.ToList = < int> (); listInt.Add ( 11);
Copy the code

  Another method is to use an array of replication. Array inherited from System.Array, System.Array abstract class provides some useful implementations, which contains the Copy method, which is responsible for copying the contents of one array to another array. Either way, changing the length of the array is equivalent to re-create an array of objects.

  In order to look itself has an array of features to dynamically change the length, you can create an extension method ReSize named.

Copy the code
    public static class ClassForExtensions
    {
        public static Array ReSize(this Array array,int newSize) { Type t = array.GetType().GetElementType(); Array newArray = Array.CreateInstance(t, newSize); Array.Copy(array, 0, newArray, 0, Math.Min(array.Length, newSize)); return newArray; } }
Copy the code

Call as follows:

        static void Main(string[] args)
        {
            int[] iArr = { 0,1,3,4,6,7,9}; iArr = (int[])ClassForExtensions.ReSize(iArr, 20); Console.ReadLine(); }

Let's compare the performance, first look at the code:

Copy the code
    class Program
    {
        static void Main(string[] args) { ResizeArray(); ResizeList(); Console.ReadLine(); } public static void ResizeArray() { int[] iArr = {0,1,3,4,6,8 }; Stopwatch watch = new Stopwatch(); watch.Start();///用于测量时间间隔 iArr = (int[])iArr.ReSize(10); watch.Stop();/// Console.WriteLine("ResizeArray:{0}", watch.Elapsed); } public static void ResizeList() { List<int> iArr = new List<int>(new int[] { 0, 1, 3, 4, 6, 8, 9 }); Stopwatch watch = new Stopwatch(); watch.Start(); iArr.Add(0); iArr.Add(0); iArr.Add(0); watch.Stop(); Console.WriteLine("ResizeList:{0}", watch.Elapsed); } }
Copy the code

Main function is mainly called own definition of the two methods, a first length of the array is re-set, the second length is set List <T>, the running time measured by:

Strictly speaking, List <T> there is no change in the length of the argument, here it is mainly to compare, on the List <T> set length and is assigned, even so, in terms of time efficiency ResizeList higher than ResizeArray very many.

Recommendation 17, using foreach loop iterates carried out in most cases

 Here on how to use foreach to traverse the ensemble can I just wrote an article about two IEnumerable and IEnumerator interfaces, are interested can look at. http://www.cnblogs.com/aehyok/p/3641193.html

Feeling using foreach cycle through a total of three advantages of it:

1, provides a relatively simple, concise syntax.

2, the code placed automatically try-finally block

3, if the type implements IDispose interfaces, foreach will automatically call the Dispose method after the end of the cycle

Recommendation 18, foreach is not a substitute for

foreach there is a problem: it is not a collection of additions and deletions operation cycle support. Let's look at a simple example:

Copy the code
            List<int> list = new List<int>() { 1, 2, 3, 4, 5 }; foreach (int item in list) { list.Remove(item); Console.WriteLine(item.ToString()); } Console.ReadLine();
Copy the code

With a look the results:

So let's try to use for:

            List<int> list = new List<int>() { 1, 2, 3, 4, 5 }; for (int i = 0; i < list.Count(); i++) { list.Remove(list[i]); } Console.ReadLine();

  进行删除肯定是没问题的。但是要仔细看一下,比如它第一次删除索引0的时候,也就是删除了1,那么它会立即重新调整索引,然后第二次删除的时候,删除的不是2,而是3这个项。那么最终运行完发现还剩余两项

 foreach循环使用了迭代器进行集合的遍历,它在FCL提供的迭代器内部维护了一个对集合版本的控制。那么什么是集合版本呢?简单的说,其实它就是一个整型的变量,任何对集合的增删操作都会使版本号加1。foreach循环会调用MoveNext方法来遍历元素,在MoveNext方法内部会进行版本号的检测,一旦检测到版本号有变动,就会抛出InvalidOperationException异常。

  如果使用for循环就不会带来这样的问题。for直接使用所引器,它不对集合版本号进行判断,所以不存在因为集合的变动而带来的异常(当然,超出索引长度这种情况除外)。

  索引,因为版本检测的缘故,foreach循环并不能带起for循环。 

建议19、使用更有效的对象和集合初始化

   对象初始化设定项支持可以直接在大括号中对自动实现的属性进行赋值。

Copy the code
    class Person
    {
        public string Name { get; set; } public int Age { get; set; } } class Program { static void Main(string[] args) { Person person = new Person() { Name = "aehyok", Age = 25 }; Console.ReadLine(); } }
Copy the code

以往只能依靠构造方法传值进去,或者在对象构造完毕后对属性进行赋值。现在这些步骤简化了,初始化设定项实际相当于编译器在对象生成后对属性进行了赋值。

Copy the code
    class Person
    {
        public string Name { get; set; } public int Age { get; set; } } class Program { static void Main(string[] args) { Person person = new Person() { Name = "Kris", Age = 22 }; List<Person> personList = new List<Person>() { new Person() { Name = "aehyok", Age = 25 }, person, null }; Console.ReadLine(); } }
Copy the code

使用集合的初始化设定项,编译器会在集合对象创建完毕后对集合调用Add方法。上面这段代码展示了如何在初始化语句中创建一个新对象或一个现有对象,以及一个null值。

 不过,初始化设定项绝不仅仅是为了对象和集合初始化的方便,它更重要的作用是为LINQ查询中的匿名类型进行属性的初始化。由于LINQ查询返回的集合中匿名类型的属性都是只读的,如果需要为匿名类型属性赋值,或者增加属性,只能通过初始化设定项来进行。初始化设定项还能为属性使用表达式。

来看一段代码:

Copy the code
List<Person> lst = new List<Person>()
 { 
    new Person(){ Age = 10,Name="Tommy"}, new Person(){ Age = 20,Name="Sammy"} }; var entity = from p in lst select new { p.Name, AgeScope = p.Age > 10 ? "Old" : "Young" }; foreach (var item in entity) { Response.Write(string.Format("name is {0},{1}", item.Name, item.AgeScope)); }
Copy the code

AgeScope property is the result calculated that with such a convenient way of initialization, makes the code more elegant and flexible.

Guess you like

Origin www.cnblogs.com/ljdong7/p/12014499.html