public class NonZero >> { >> private int i = 1; >> public int I >> { >> get { return i; } >> set { i = value; } >> } >> >> public static NonZero operator ++(NonZero nz) >> { >> nz.i = nz.i == -1 ? 1 : nz.i+1; >> return nz; >> } >> >> public static NonZero operator --(NonZero nz) >> { >> nz.i = nz.i == 1 ? -1 : nz.i - 1; >> return nz; >> } >> }>>works but requires odd syntax: int i = (NonZeroInstance++).I
>namespace GregoryAdam.Base.Types >{ > public class NonZero > { > private int nz = 1; > > #region Constructor > //------------------------------------------------------------------------- > public NonZero(int i) > { > nz = (i == 0) ? 1 : i; > } > //------------------------------------------------------------------------- > public NonZero() > { > } > //------------------------------------------------------------------------- > #endregion > //------------------------------------------------------------------------- > > //------------------------------------------------------------------------- > public override string ToString() > { > return nz.ToString(); > } > //------------------------------------------------------------------------- > > #region Cast implementation > //------------------------------------------------------------------------- > #region (int) NonZero > // implicit conversion of NonZero to int > public static implicit operator int(NonZero f) > { > return f.nz; > } > #endregion > //------------------------------------------------------------------------- > #region (NonZero) int > // explicit conversion of int to NonZero: 0 gets converted into 1 > // may want to make this implicit > public static explicit operator NonZero(int i) > { > return new NonZero(i); > } > #endregion > //------------------------------------------------------------------------- > #endregion Cast > > #region Operators > #region ++ > //------------------------------------------------------------------------- > public static NonZero operator ++(NonZero f1) > { >#if false // this were a structure > // -1 + 1 = 0 >> 1 handled in constructor > return new NonZero(f1.nz + 1); >#else > if (++f1.nz == 0) > f1.nz = 1; > > return f1; >#endif > } > //------------------------------------------------------------------------- > #endregion > #region -- > //------------------------------------------------------------------------- > public static NonZero operator --(NonZero f1) > { >#if false // this were a structure > // handle 1 - 1 = 0 >> -1 > if (f1.nz == 1) > return new NonZero(-1); > > return new NonZero(f1.nz - 1); >#else > if (--f1.nz == 0) > f1.nz = -1; > > return f1; >#endif > > } > //------------------------------------------------------------------------- > #endregion > #endregion Operators > > } >} >>
>class Test >{ > > > static void Main(string[] args) > { > var xx = new NonZero(); > Console.WriteLine(" xx = {0}", xx); > > var yy = --xx; > Console.WriteLine(" xx = {0}", xx); > Console.WriteLine(" yy = {0}", yy); > > var zz = yy++; > Console.WriteLine(" yy = {0}", yy); > Console.WriteLine(" zz = {0}", zz); > > var tt = new NonZero(); > > int i = --tt; // implicit conversion > > Console.WriteLine(" tt = {0}", tt); > Console.WriteLine(" i = {0}", i); > > > var ww = new NonZero(3); > Console.WriteLine(" ww = {0}", ww); > Console.WriteLine(" --ww = {0}", --ww); > > Console.ReadLine(); > > } >} >>