Nov182008

IComparable, IEquatable

I am still reading the book about writing more effective c# code. One of the chapters is talking about using IComparable and IEquatable for comparions, and checking equality. The news interfaces are strongly typed, so the code is less, cleaner, and nicer; however, for a lot of purposes you may want to support old style override Equals(object obj) too, and as good practive whenever you overwrite Equals, you should overwrite GetHastCode, especially if you are using LINQ or update wont work.

here is a simple code from one my project that does it all 

[code:c#]

 public class Restriction:IComparable<Restriction>,IEquatable<Restriction>
    {
        /// <summary>
        /// Restriction Unique ID, should be assigned by DB
        /// </summary>
        /// <remarks>if it is <1 then throws ArgumentOutOfRangeException</remarks>
        private int ID_;
        public int ID
        {
            get{ return this.ID_;}
            set
            {
                if (value<1)
                    throw new ArgumentOutOfRangeException("ID can not be less then 1");
                this.ID_ = value;
            }
        }
        /// <summary>
        /// Restriction rule
        /// </summary>
        /// <remarks>If it is empty or null, will throw ArgumentNullException
        ///          If len(Rule)>MAX_RESTRICTION_LEN throws ArgumentOutOfRangeException
        /// </remarks>
        /// <seealso cref="Constants"/>
        private string Rule_;
        public string Rule
        {
            get { return this.Rule_; }
            set
            {
                if(String.IsNullOrEmpty(value))
                    throw new ArgumentNullException("Rule can not be empty or null");
                if(value.Trim().Length==0)
                    throw new ArgumentNullException("Rule can not be empty or null");
                if(value.Trim().Length>Constants.MAX_RESTRICTION_LEN)
                    throw new ArgumentOutOfRangeException("Length of Rule can not be bigger than MAX_RESTRICTION_LEN");
                this.Rule_ = value;
            }
        }

        public Restriction(int ID, string Rule)
        {
            this.ID = ID;
            this.Rule = Rule;
        }

        public int CompareTo(Restriction other)
        {
            if (other == null)
                return 1; // any non null object > null
            int rVal = Comparer<string>.Default.Compare(this.Rule.ToLower().Trim(),
                                                        other.Rule_.ToLower().Trim());
            return rVal;
        }

        public bool Equals(Restriction other)
        {
            if (Object.ReferenceEquals(other, null))
                return false;
            return this.Rule.ToLower().Trim() == other.Rule.ToLower().Trim();
        }

        public override bool Equals(object obj)
        {
            if (obj.GetType() == typeof(Restriction))
                return this.Equals(obj as Restriction);
            return false;
        }

        public override int GetHashCode()
        {
            return this.Rule.ToLower().Trim().GetHashCode();
        }

        public override string ToString()
        {
            return this.Rule;
        }
    }

[/code]



Tags:

E-mail | Permalink | Trackback | Post RSSRSS comment feed 0 Responses

Add comment