Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections;
- using System.Collections.Generic;
- namespace hashes
- {
- public class ReadonlyBytes : IEnumerable
- {
- public readonly byte[] Array;
- public int Length { get { return Array.Length; } }
- private int currentHashCode { get; set; } = 0;
- public ReadonlyBytes(params byte[] newArray)
- {
- Array = newArray ?? throw new ArgumentNullException();
- }
- public override bool Equals(object obj)
- {
- var newArray = obj as ReadonlyBytes;
- if (newArray.Length != Array.Length) return false;
- for (int i = 0; i < Array.Length; i++)
- if (Array[i] != newArray[i]) return false;
- return true;
- }
- public override int GetHashCode()
- {
- unchecked
- {
- if (currentHashCode != 0) return currentHashCode;
- currentHashCode = 1;
- foreach (var e in Array)
- currentHashCode += (currentHashCode * 322) ^ e;
- return currentHashCode;
- }
- }
- public IEnumerator<byte> GetEnumerator()
- {
- for (int i = 0; i < Array.Length; i++)
- yield return Array[i];
- }
- IEnumerator IEnumerable.GetEnumerator()
- {
- return GetEnumerator();
- }
- public byte this[int index]
- {
- get
- {
- if (index < 0 || index >= Array.Length) throw new IndexOutOfRangeException();
- return Array[index];
- }
- set
- {
- if (index < 0 || index >= Array.Length) throw new IndexOutOfRangeException();
- Array[index] = value;
- }
- }
- public override string ToString()
- {
- return "[" + string.Join(", ", Array) + "]";
- }
- }
- }
Add Comment
Please, Sign In to add comment