Identifies if the byte array starts with a certain byte - CSharp System

CSharp examples for System:Byte Array

Description

Identifies if the byte array starts with a certain byte

Demo Code


using System.Linq;
using System;// w w w.jav  a  2 s  . c  o m

public class Main{
        /// <summary>
        /// Identifies if the byte array starts with a certain byte
        /// </summary>
        /// <param name="arr">The array to check.</param>
        /// <param name="startsWith">The byte to check for.</param>
        /// <returns>True if the array starts with that byte, false otherwise.</returns>
        public static bool StartsWith(this byte[] arr, byte startsWith)
        {
            if (arr == null || arr.Length == 0)
            {
                return false;
            }

            return arr[0] == startsWith;
        }
        /// <summary>
        /// Identifies if the byte array starts with a certain char
        /// </summary>
        /// <remarks>
        /// Ghostdoc suggested "Startses the with". Dirty little hobbitsses.
        /// </remarks>
        /// <param name="arr">The array to check.</param>
        /// <param name="startsWith">The character to check for.</param>
        /// <returns>True if the array starts with that character, false otherwise.</returns>
        public static bool StartsWith(this byte[] arr, char startsWith)
        {
            return arr.StartsWith((byte)startsWith);
        }
}

Related Tutorials