Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.ObjectModel;
- using System.Diagnostics;
- using Microsoft.VisualStudio.TestTools.UnitTesting;
- namespace Demos
- {
- [TestClass]
- public class StaticArrayMethodsExample
- {
- [TestMethod]
- public void ReadonlyCollection()
- {
- var names = new[] { "Sarah", "Gentry", "Amrit", "Amrit2" };
- ReadOnlyCollection<string> ro = Array.AsReadOnly(names);
- // readOnly.Add("x"); // no add method on ReadOnlyCollection
- }
- [TestMethod]
- public void FindingItems()
- {
- var names = new[] { "Sarah", "Gentry", "Amrit1", "Amrit2" };
- int gentryIndex = Array.BinarySearch(names, "Gentry");
- int amrit1Index = Array.FindIndex(names, x => x.StartsWith("Amrit"));
- bool isGentryThere = Array.Exists(names, x => x == "Gentry");
- string lastAmrit = Array.FindLast(names, x => x.StartsWith("Amrit"));
- }
- [TestMethod]
- public void ForEach()
- {
- var names = new[] { "Sarah", "Gentry", "Amrit1", "Amrit2" };
- Array.ForEach(names, x =>
- {
- Debug.WriteLine(x);
- Debug.WriteLine(x.Length);
- });
- }
- [TestMethod]
- public void Sorting()
- {
- var names = new[] { "Sarah", "Gentry", "Amrit1", "Amrit2" };
- Array.Sort(names);
- }
- [TestMethod]
- public void Truth()
- {
- var names = new[] { "Sarah", "Gentry", "Amrit1", "Amrit2" };
- bool allNamesStartWithA = Array.TrueForAll(names, x => x.StartsWith("A"));
- }
- [TestMethod]
- public void Reverse()
- {
- var ints = new[] {1,2,3};
- Array.Reverse(ints);
- }
- }
- }
- //************************COPYNG ARRAYS
- [TestClass]
- public class CopyingArraysExample
- {
- [TestMethod]
- public void CloneWithReferenceTypes()
- {
- var names = new[] { "Sarah", "Gentry", "Amrit", "Amrit2" };
- string[] shallowCopy = (string[]) names.Clone();
- var originalElementZero = names[0];
- var copyElementZero = shallowCopy[0];
- var isSameReference =
- object.ReferenceEquals(originalElementZero, copyElementZero);
- }
- [TestMethod]
- public void CloneWithValueTypes()
- {
- var ints = new[] { 1, 2, 3 };
- int[] shallowCopy = (int[])ints.Clone();
- var originalElementZero = ints[0];
- var copyElementZero = shallowCopy[0];
- var isSameReference =
- object.ReferenceEquals(originalElementZero, copyElementZero);
- }
- [TestMethod]
- public void CopyTo()
- {
- var names = new string[6];
- names[0] = "Sarah";
- names[1] = "Gentry";
- names[2] = "Amrit";
- names[3] = "Amrit2";
- var moreNames = new[] { "Sally", "Peter", "Fred" };
- moreNames.CopyTo(names, 3);
- var originalSally = moreNames[0];
- var copiedSally = names[3];
- var isSameReference =
- object.ReferenceEquals(originalSally, copiedSally);
- }
- [TestMethod]
- public void ConvertAll()
- {
- var ints = new[] {1, 2, 3 };
- BigInteger[] bigInts = Array.ConvertAll(ints, x => new BigInteger(x));
- }
- [TestMethod]
- public void ConstrainedCopy()
- {
- var things = new object[] { "Sarah", 1};
- var thingsCopy = new object[2];
- Array.ConstrainedCopy(things, 0, thingsCopy, 0, 2);
- var strings = new string[2];
- things.CopyTo(strings, 0);
- Array.ConstrainedCopy(things, 0, strings, 0, 2);
- }
- [TestMethod]
- public void Resize()
- {
- var names = new[] { "Sarah", "Gentry", "Amrit", "Amrit2" };
- Array.Resize(ref names, 2);
- Array.Resize(ref names, 4);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement