【C#】性别类

实现了与bool类型的自动转换,方便存放到数据库和前台显示

纯粹为了方便才写的这个类,娱乐而已

    public struct Sex
    {
        public static readonly string FemaleString = "女";
        public static readonly string MaleString = "男";

        private bool _isMale;

        public static implicit operator Sex(bool sex)
        {
            return new Sex { _isMale = sex };
        }

        public static implicit operator bool(Sex sex)
        {
            return sex._isMale;
        }

        public override string ToString()
        {
            return _isMale ? MaleString : FemaleString;
        }

        public static Sex Parse(string str)
        {
            if (string.Compare(str, MaleString, true) == 0)
                return true;
            else if (string.Compare(str, FemaleString, true) == 0)
                return false;
            else
                throw new ArgumentOutOfRangeException("str");
        }
    }
posted @ 2011-04-25 14:56  Aimeast  阅读(1335)  评论(1编辑  收藏  举报