Created
August 6, 2015 10:33
-
-
Save yanisurbis/e8757727af9f2236ba69 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.Collections; | |
using System.Collections.Generic; | |
using System.Linq; | |
using System.Text; | |
using System.Threading.Tasks; | |
namespace ConsoleApplication3 | |
{ | |
class My | |
{ | |
public static void Main() | |
{ | |
////creating | |
//MyPoint p = new MyPoint(); | |
//MyPoint k = new MyPoint(); | |
//MyPoint l = new MyPoint { X = 1, Y = 2, Z = 3 }; | |
//MyPoint n = new MyPoint { X = 1, Y = 2, Z = 3 }; | |
//MyPoint a = l; | |
////viewing | |
//Console.WriteLine(p.ToString()); | |
//Console.WriteLine(l.ToString()); | |
////equality | |
////Console.WriteLine(l.Equals(n)); | |
////Console.WriteLine(p.Equals(n)); | |
//Console.WriteLine("\n"); | |
//Console.WriteLine(Object.Equals(l, n)); | |
//Console.WriteLine(Object.Equals(l, a)); | |
////hash | |
//Console.WriteLine(l.GetHashCode()); | |
MyPoint a1 = new MyPoint(); | |
a1.FillArray(10); | |
foreach (MyPoint x in a1) | |
Console.WriteLine(x.X); | |
Console.ReadLine(); | |
} | |
} | |
class MyPoint : IEnumerable | |
{ | |
int x; | |
int y; | |
int z; | |
MyPoint[] arr; | |
public int X | |
{ | |
get { return x; } | |
set { x = value; } | |
} | |
public int Y | |
{ | |
get { return y; } | |
set { y = value; } | |
} | |
public int Z | |
{ | |
get { return z; } | |
set { z = value; } | |
} | |
public MyPoint() | |
{ | |
Random r = new Random(); | |
x = r.Next(1, 101); | |
y = r.Next(1, 101); | |
z = r.Next(1, 101); | |
} | |
public MyPoint(int x, int y, int z) | |
{ | |
X = x; | |
Y = y; | |
Z = z; | |
} | |
//public IEnumerator GetEnumerator() | |
//{ | |
// return arr.GetEnumerator(); | |
//} | |
//public IEnumerator GetEnumerator() | |
//{ | |
// foreach (MyPoint item in arr) | |
// { | |
// yield return item; | |
// } | |
//} | |
public IEnumerator GetEnumerator() | |
{ | |
return new MyEnumerator(this); | |
} | |
public IEnumerable GetPoints(bool b) | |
{ | |
return new MyEnumerable(this); | |
} | |
class MyEnumerable : IEnumerable | |
{ | |
MyPoint p; | |
public MyEnumerable(MyPoint p) | |
{ | |
this.p = p; | |
} | |
public IEnumerator GetEnumerator() | |
{ | |
return MyPoint.MyEnumerator e = new MyPoint.MyEnumerator(); | |
} | |
} | |
class MyEnumerator : IEnumerator | |
{ | |
MyPoint[] arr; | |
int len; | |
int i; | |
public MyEnumerator(MyPoint x) | |
{ | |
arr = x.arr; | |
len = x.arr.Length; | |
i = 0; | |
} | |
public bool MoveNext() | |
{ | |
return i != len ? true : false; | |
} | |
public object Current | |
{ | |
get | |
{ | |
return arr[i++]; | |
} | |
} | |
public void Reset() | |
{ | |
i = 0; | |
} | |
} | |
public void FillArray(int n) | |
{ | |
arr = new MyPoint[n]; | |
for (int i = 0; i < n; i++) | |
arr[i] = new MyPoint { X = i, Y = i + 1, Z = i + 2 }; | |
} | |
public override string ToString() | |
{ | |
return String.Format("First field = {0}\nSecond field = {1}\nThird field = {2}\n", x, y, z); | |
} | |
public override bool Equals(object obj) | |
{ | |
MyPoint p = obj as MyPoint; | |
if (p != null) | |
{ | |
if (this.x == p.x && this.y == p.y && this.Z == p.Z) | |
return true; | |
} | |
return false; | |
} | |
public override int GetHashCode() | |
{ | |
return this.ToString().GetHashCode(); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment