22 Haziran 2018

C# ile Yeni Veri Tipi Oluşturmak

Para işlemlerinde kullanmak için virgülden sonraki sadece iki basamağı hesaba katmak için Money türünde yeni bir veri tipi oluşturdum.

Money.cs

using System;
using System.Globalization;

namespace ConsoleApplication1
{
    public struct Money
    {
        public static int MidPointWayFromZero = 2;
        private decimal value;
        private Money(decimal value)
        {
            this.value = Decimal.Round(value, MidPointWayFromZero, MidpointRounding.AwayFromZero);
        }

        public static implicit operator Money(decimal value) => new Money(value);
        public static implicit operator Money(byte value) => new Money(value);
        public static implicit operator Money(sbyte value) => new Money(value);
        public static implicit operator Money(short value) => new Money(value);
        public static implicit operator Money(ushort value) => new Money(value);
        public static implicit operator Money(char value) => new Money(value);
        public static implicit operator Money(int value) => new Money(value);
        public static implicit operator Money(uint value) => new Money(value);
        public static implicit operator Money(long value) => new Money(value);
        public static implicit operator Money(ulong value) => new Money(value);
        public static implicit operator Money(float value) => new Money(Convert.ToDecimal(value));
        public static implicit operator Money(double value) => new Money(Convert.ToDecimal(value));


        public static explicit operator decimal(Money money) => money.value;
        public static explicit operator byte(Money money) => Convert.ToByte(money.value);
        public static explicit operator sbyte(Money money) => Convert.ToSByte(money.value);
        public static explicit operator char(Money money) => Convert.ToChar(money.value);
        public static explicit operator short(Money money) => Convert.ToInt16(money.value);
        public static explicit operator ushort(Money money) => Convert.ToUInt16(money.value);
        public static explicit operator int(Money money) => Convert.ToInt32(money.value);
        public static explicit operator uint(Money money) => Convert.ToUInt32(money.value);
        public static explicit operator long(Money money) => Convert.ToInt64(money.value);
        public static explicit operator ulong(Money money) => Convert.ToUInt64(money.value);
        public static explicit operator float(Money money) => Convert.ToSingle(money.value);
        public static explicit operator double(Money money) => Convert.ToDouble(money.value);




        public override String ToString() => value.ToString();

        public String ToString(String format) => value.ToString(format);

        public String ToString(IFormatProvider provider) => value.ToString(provider);

        public String ToString(String format, IFormatProvider provider) => value.ToString(format, provider);


        public static Money Parse(String s) => new Money(Decimal.Parse(s));

        public static Money Parse(String s, NumberStyles style) => new Money(Decimal.Parse(s, style));

        public static Money Parse(String s, IFormatProvider provider) => new Money(Decimal.Parse(s, provider));

        public static Money Parse(String s, NumberStyles style, IFormatProvider provider) => new Money(Decimal.Parse(s, style, provider));

        public static Boolean TryParse(String s, out Money result) => Decimal.TryParse(s, out result.value);

        public static Boolean TryParse(String s, NumberStyles style, IFormatProvider provider, out Decimal result)
            => Decimal.TryParse(s, style, provider, out result);

        public static Money Round(Money d, int decimals = 0) => new Money(Decimal.Round(d.value, decimals));


        public static Money Round(Money d, MidpointRounding mode) => new Money(Decimal.Round(d.value, 0, mode));


        public static Money Round(Money d, int decimals, MidpointRounding mode) => new Money(Decimal.Round(d.value, decimals, mode));

        public static short ToInt16(Money value) => Convert.ToInt16(value.value);
        public static int ToInt32(Money d) => Convert.ToInt32(d.value);
        public static long ToInt64(Money d) => Convert.ToInt64(d.value);



        public static Money operator +(Money d) => d;

        public static Money operator -(Money d) => -d;

        public static Money operator ++(Money d) => new Money(d.value + 1);

        public static Money operator --(Money d) => new Money(d.value - 1);

        public static Money operator +(Money d1, Money d2) => new Money(d1.value + d2.value);

        public static Money operator -(Money d1, Money d2) => new Money(d1.value - d2.value);

        public static Money operator *(Money d1, Money d2) => new Money(d1.value * d2.value);


        public static Money operator /(Money d1, Money d2) => new Money(d1.value / d2.value);

        public static Money operator %(Money d1, Money d2) => new Money(d1.value % d2.value);

        public static bool operator ==(Money d1, Money d2) => d1.value == d2.value;


        public static bool operator !=(Money d1, Money d2) => d1.value != d2.value;

        public static bool operator <(Money d1, Money d2) => d1.value < d2.value;

        public static bool operator <=(Money d1, Money d2) => d1.value <= d2.value;

        public static bool operator >(Money d1, Money d2) => d1.value > d2.value;

        public static bool operator >=(Money d1, Money d2) => d1.value >= d2.value;


        public TypeCode GetTypeCode()
        {
            return TypeCode.Decimal;
        }

    }
}

Consol içerisinde kullanım testi
using System;

namespace ConsoleApplication1
{

    class Program
    {
        public static void Main(params string[] args)
        {

            Money money = 919;

            Console.WriteLine(IsPrime(money));
            money += .23;
            money *= .18;
            Console.WriteLine(money);
        }

        private static bool IsPrime(Money number)
        {
            Money i = 2;
            if (number <= 2 || number % 2 == 0)
                return false;
            i++;
            while (i * i <= number)
            {
                if (number % i == 0)
                    return false;
                i++;
            }
            return true;
        }
    }
}