Advertisement
POdkovyrkinDaniil

Untitled

Mar 3rd, 2018
322
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.06 KB | None | 0 0
  1. using System;
  2. using System.Collections;
  3. using System.Collections.Generic;
  4. using System.Text;
  5.  
  6. namespace hashes
  7. {
  8. class ReadonlyBytes : IEnumerable
  9. {
  10. public byte[] bytes;
  11. public int Length { get => bytes.Length; }
  12. private int hash;
  13.  
  14. public ReadonlyBytes()
  15. {
  16. hash = this.GetHashCode();
  17. }
  18. public ReadonlyBytes(params byte[] args)
  19. {
  20. bytes = args ?? throw new ArgumentNullException();
  21. }
  22. public byte this[int index]
  23. {
  24. get
  25. {
  26. if (index < 0 || index >= Length) throw new IndexOutOfRangeException();
  27. return bytes[index];
  28. }
  29.  
  30. set
  31. {
  32. if (index < 0 || index >= Length) throw new IndexOutOfRangeException();
  33. bytes[index] = value;
  34. }
  35. }
  36.  
  37. public override bool Equals(object obj)
  38. {
  39. if (!(obj is ReadonlyBytes)) return false;
  40. var array = obj as ReadonlyBytes;
  41. if (this.GetHashCode() == array.GetHashCode())
  42. return true;
  43. if (this.Length != array.Length)
  44. return false;
  45. return true;
  46. }
  47. public override int GetHashCode()
  48. {
  49. hash = this.ToString().GetHashCode();
  50. return hash;
  51. }
  52. public override string ToString()
  53. {
  54. var builder = new StringBuilder();
  55. builder.Append("[");
  56. for (var i = 0; i < bytes.Length - 1; i++)
  57. {
  58. builder.Append(bytes[bytes.Length - 1].ToString());
  59. builder.Append(", ");
  60. }
  61. builder.Append(bytes[bytes.Length - 1].ToString());
  62. builder.Append("]");
  63. return builder.ToString();
  64. }
  65.  
  66. IEnumerator IEnumerable.GetEnumerator()
  67. {
  68. for (var i = 0; i < Length; i++)
  69. {
  70. yield return bytes[i];
  71. }
  72. }
  73. }
  74. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement