Advertisement
Caminhoneiro

Static arrays methods example/copying arrays

Jul 16th, 2018
124
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 4.50 KB | None | 0 0
  1. using System;
  2. using System.Collections.ObjectModel;
  3. using System.Diagnostics;
  4. using Microsoft.VisualStudio.TestTools.UnitTesting;
  5.  
  6. namespace Demos
  7. {
  8.     [TestClass]
  9.     public class StaticArrayMethodsExample
  10.     {      
  11.  
  12.         [TestMethod]
  13.         public void ReadonlyCollection()
  14.         {
  15.             var names = new[] { "Sarah", "Gentry", "Amrit", "Amrit2" };
  16.  
  17.             ReadOnlyCollection<string> ro = Array.AsReadOnly(names);
  18.  
  19.             // readOnly.Add("x"); // no add method on ReadOnlyCollection
  20.         }
  21.  
  22.  
  23.  
  24.  
  25.  
  26.  
  27.  
  28.  
  29.  
  30.  
  31.         [TestMethod]
  32.         public void FindingItems()
  33.         {
  34.             var names = new[] { "Sarah", "Gentry", "Amrit1", "Amrit2" };
  35.  
  36.             int gentryIndex = Array.BinarySearch(names, "Gentry");
  37.  
  38.             int amrit1Index = Array.FindIndex(names, x => x.StartsWith("Amrit"));
  39.  
  40.             bool isGentryThere = Array.Exists(names, x => x == "Gentry");
  41.  
  42.             string lastAmrit = Array.FindLast(names, x => x.StartsWith("Amrit"));
  43.         }
  44.  
  45.  
  46.  
  47.  
  48.  
  49.  
  50.  
  51.  
  52.  
  53.         [TestMethod]
  54.         public void ForEach()
  55.         {
  56.             var names = new[] { "Sarah", "Gentry", "Amrit1", "Amrit2" };
  57.  
  58.             Array.ForEach(names, x =>
  59.                                      {
  60.                                          Debug.WriteLine(x);
  61.                                          Debug.WriteLine(x.Length);                                        
  62.                                      });
  63.         }
  64.  
  65.  
  66.  
  67.  
  68.  
  69.  
  70.  
  71.  
  72.  
  73.         [TestMethod]
  74.         public void Sorting()
  75.         {
  76.             var names = new[] { "Sarah", "Gentry", "Amrit1", "Amrit2" };
  77.  
  78.             Array.Sort(names);
  79.         }
  80.  
  81.  
  82.  
  83.  
  84.  
  85.  
  86.  
  87.  
  88.  
  89.  
  90.  
  91.         [TestMethod]
  92.         public void Truth()
  93.         {
  94.             var names = new[] { "Sarah", "Gentry", "Amrit1", "Amrit2" };
  95.  
  96.             bool allNamesStartWithA = Array.TrueForAll(names, x => x.StartsWith("A"));
  97.         }
  98.  
  99.  
  100.  
  101.  
  102.  
  103.  
  104.  
  105.  
  106.  
  107.  
  108.  
  109.         [TestMethod]
  110.         public void Reverse()
  111.         {
  112.             var ints = new[] {1,2,3};
  113.  
  114.             Array.Reverse(ints);
  115.         }
  116.     }
  117. }
  118.  
  119.  
  120. //************************COPYNG ARRAYS
  121.  [TestClass]
  122.     public class CopyingArraysExample
  123.     {
  124.  
  125.         [TestMethod]
  126.         public void CloneWithReferenceTypes()
  127.         {
  128.             var names = new[] { "Sarah", "Gentry", "Amrit", "Amrit2" };
  129.  
  130.             string[] shallowCopy = (string[]) names.Clone();
  131.  
  132.             var originalElementZero = names[0];
  133.  
  134.             var copyElementZero = shallowCopy[0];
  135.  
  136.             var isSameReference =
  137.                 object.ReferenceEquals(originalElementZero, copyElementZero);
  138.         }
  139.  
  140.  
  141.  
  142.  
  143.  
  144.  
  145.  
  146.  
  147.         [TestMethod]
  148.         public void CloneWithValueTypes()
  149.         {
  150.             var ints = new[] { 1, 2, 3 };
  151.  
  152.             int[] shallowCopy = (int[])ints.Clone();
  153.  
  154.             var originalElementZero = ints[0];
  155.  
  156.             var copyElementZero = shallowCopy[0];
  157.  
  158.             var isSameReference =
  159.                 object.ReferenceEquals(originalElementZero, copyElementZero);
  160.         }
  161.  
  162.  
  163.  
  164.  
  165.  
  166.  
  167.  
  168.  
  169.         [TestMethod]
  170.         public void CopyTo()
  171.         {
  172.             var names = new string[6];
  173.             names[0] = "Sarah";
  174.             names[1] = "Gentry";
  175.             names[2] = "Amrit";
  176.             names[3] = "Amrit2";
  177.  
  178.             var moreNames = new[] { "Sally", "Peter", "Fred" };
  179.  
  180.             moreNames.CopyTo(names, 3);
  181.  
  182.             var originalSally = moreNames[0];
  183.             var copiedSally = names[3];
  184.  
  185.             var isSameReference =
  186.                 object.ReferenceEquals(originalSally, copiedSally);
  187.         }
  188.  
  189.  
  190.  
  191.  
  192.  
  193.  
  194.  
  195.  
  196.  
  197.         [TestMethod]
  198.         public void ConvertAll()
  199.         {
  200.             var ints = new[] {1, 2, 3 };
  201.  
  202.             BigInteger[] bigInts = Array.ConvertAll(ints, x => new BigInteger(x));
  203.         }
  204.  
  205.  
  206.  
  207.  
  208.  
  209.  
  210.  
  211.  
  212.  
  213.  
  214.  
  215.  
  216.  
  217.  
  218.         [TestMethod]
  219.         public void ConstrainedCopy()
  220.         {
  221.             var things = new object[] { "Sarah", 1};
  222.  
  223.             var thingsCopy = new object[2];            
  224.  
  225.             Array.ConstrainedCopy(things, 0, thingsCopy, 0, 2);
  226.  
  227.             var strings = new string[2];
  228.  
  229.             things.CopyTo(strings, 0);
  230.  
  231.             Array.ConstrainedCopy(things, 0, strings, 0, 2);
  232.         }
  233.  
  234.  
  235.  
  236.  
  237.  
  238.  
  239.  
  240.  
  241.  
  242.  
  243.  
  244.  
  245.         [TestMethod]
  246.         public void Resize()
  247.         {
  248.             var names = new[] { "Sarah", "Gentry", "Amrit", "Amrit2" };
  249.  
  250.             Array.Resize(ref names, 2);
  251.  
  252.             Array.Resize(ref names, 4);
  253.         }
  254.     }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement