Last active
December 22, 2015 05:09
-
-
Save Ummon/6422140 to your computer and use it in GitHub Desktop.
IntString
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; | |
namespace ExperimentationsCSharp | |
{ | |
public struct IntString : IEquatable<IntString>, IComparable<IntString> | |
{ | |
int value; | |
public IntString(int v) | |
{ | |
this.value = v; | |
} | |
IntString(string str) | |
{ | |
int.TryParse(str, out this.value); | |
} | |
public bool Equals(IntString other) | |
{ | |
return this.value.Equals(other.value); | |
} | |
public static bool operator ==(IntString v1, IntString v2) | |
{ | |
return v1.Equals(v2); | |
} | |
public static bool operator !=(IntString v1, IntString v2) | |
{ | |
return !v1.Equals(v2); | |
} | |
public static IntString operator +(IntString v1, IntString v2) | |
{ | |
return new IntString(v1.value + v2.value); | |
} | |
public static implicit operator IntString(int v) | |
{ | |
return new IntString(v); | |
} | |
public static explicit operator IntString(string str) | |
{ | |
return new IntString(str); | |
} | |
public static implicit operator int(IntString istr) | |
{ | |
return istr.value; | |
} | |
public static implicit operator string(IntString istr) | |
{ | |
return istr.value.ToString(); | |
} | |
public override bool Equals(object obj) | |
{ | |
if (obj is IntString) | |
return this.Equals((IntString)obj); | |
else | |
return false; | |
} | |
public override int GetHashCode() | |
{ | |
return this.value.GetHashCode(); | |
} | |
public override string ToString() | |
{ | |
return this.value.ToString(); | |
} | |
public int CompareTo(IntString other) | |
{ | |
return this.value.CompareTo(other.value); | |
} | |
} | |
class Program | |
{ | |
static void Main(string[] args) | |
{ | |
IntString a = 10; | |
int b = 20; | |
string c = "30"; | |
Console.WriteLine("b + a : {0}", b + a); | |
Console.WriteLine("a + b : {0}", a + b); | |
Console.WriteLine("a + c : {0}", a + c); | |
Console.WriteLine("c + a : {0}", c + a); | |
// Output: | |
// b + a : 30 | |
// a + b : 30 | |
// a + c : 1030 | |
// c + a : 3010 | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment