Page 107 - CSharp/C#
P. 107

{
                     if (array[a].Equals(candidate[0]))
                     {
                         int i = 0;
                         for (; i < candidate.Length; i++)
                         {
                             if (false == array[a + i].Equals(candidate[i]))
                                 break;
                         }
                         if (i == candidate.Length)
                             return true;
                     }
                 }
                 return false;
             }

             static bool IsEmptyLocate<T>(T[] array, T[] candidate)
             {
                 return array == null
                     || candidate == null
                     || array.Length == 0
                     || candidate.Length == 0
                     || candidate.Length > array.Length;
             }
         }


        /// Sample


         byte[] EndOfStream = Encoding.ASCII.GetBytes("---3141592---");
         byte[] FakeReceivedFromStream = Encoding.ASCII.GetBytes("Hello, world!!!---3141592---");
         if (FakeReceivedFromStream.Contains(EndOfStream))
         {
             Console.WriteLine("Message received");
         }


        Initializing an array filled with a repeated non-default value


        As we know we can declare an array with default values:


         int[] arr = new int[10];


        This will create an array of 10 integers with each element of the array having value 0 (the default
        value of type int).


        To create an array initialized with a non-default value, we can use Enumerable.Repeat from the
        System.Linq Namespace:

            1.  To create a bool array of size 10 filled with "true"


               bool[] booleanArray = Enumerable.Repeat(true, 10).ToArray();


            2.  To create an int array of size 5 filled with "100"







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