Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections;
- using System.Collections.Generic;
- using System.Text;
- namespace hashes
- {
- class ReadonlyBytes : IEnumerable
- {
- public byte[] bytes;
- public int Length { get => bytes.Length; }
- private int hash;
- public ReadonlyBytes()
- {
- hash = this.GetHashCode();
- }
- public ReadonlyBytes(params byte[] args)
- {
- bytes = args ?? throw new ArgumentNullException();
- }
- public byte this[int index]
- {
- get
- {
- if (index < 0 || index >= Length) throw new IndexOutOfRangeException();
- return bytes[index];
- }
- set
- {
- if (index < 0 || index >= Length) throw new IndexOutOfRangeException();
- bytes[index] = value;
- }
- }
- public override bool Equals(object obj)
- {
- if (!(obj is ReadonlyBytes)) return false;
- var array = obj as ReadonlyBytes;
- if (this.GetHashCode() == array.GetHashCode())
- return true;
- if (this.Length != array.Length)
- return false;
- return true;
- }
- public override int GetHashCode()
- {
- hash = this.ToString().GetHashCode();
- return hash;
- }
- public override string ToString()
- {
- var builder = new StringBuilder();
- builder.Append("[");
- for (var i = 0; i < bytes.Length - 1; i++)
- {
- builder.Append(bytes[bytes.Length - 1].ToString());
- builder.Append(", ");
- }
- builder.Append(bytes[bytes.Length - 1].ToString());
- builder.Append("]");
- return builder.ToString();
- }
- IEnumerator IEnumerable.GetEnumerator()
- {
- for (var i = 0; i < Length; i++)
- {
- yield return bytes[i];
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement