Page 108 - CSharp/C#
P. 108

int[] intArray = Enumerable.Repeat(100, 5).ToArray();


            3.  To create a string array of size 5 filled with "C#"


               string[] strArray = Enumerable.Repeat("C#", 5).ToArray();


        Copying arrays


        Copying a partial array with the static Array.Copy() method, beginning at index 0 in both, source
        and destination:


         var sourceArray = new int[] { 11, 12, 3, 5, 2, 9, 28, 17 };
         var destinationArray= new int[3];
         Array.Copy(sourceArray, destinationArray, 3);

         // destinationArray will have 11,12 and 3


        Copying the whole array with the CopyTo() instance method, beginning at index 0 of the source and
        the specified index in the destination:


         var sourceArray = new int[] { 11, 12, 7 };
         var destinationArray = new int[6];
         sourceArray.CopyTo(destinationArray, 2);

         // destinationArray will have 0, 0, 11, 12, 7 and 0


        Clone is used to create a copy of an array object.


         var sourceArray = new int[] { 11, 12, 7 };
         var destinationArray = (int)sourceArray.Clone();

         //destinationArray will be created and will have 11,12,17.


        Both CopyTo and Clone perform shallow copy which means the contents contains references to the
        same object as the elements in the original array.


        Creating an array of sequential numbers


        LINQ provides a method that makes it easy to create a collection filled with sequential numbers.
        For example, you can declare an array which contains the integers between 1 and 100.

        The Enumerable.Range method allows us to create sequence of integer numbers from a specified
        start position and a number of elements.

        The method takes two arguments: the starting value and the number of elements to generate.



         Enumerable.Range(int start, int count)

        Note that count cannot be negative.




        https://riptutorial.com/                                                                               54
   103   104   105   106   107   108   109   110   111   112   113