SharePoint In Action

An attempt to share my day-to-day SharePoint experience

Archive for the tag “List.Contains”

Implementing the IEquatable Interface for Objects Equality Methods in C#

In C#, unlike value types (int, double, struct, DateTime,..) we cannot use operator “==” for objects to check the equality. In order to check the equality of two reference variables, we need to implement the interface:  IEquatable. This interface can be well used for generic collection objects like Dictionary<TKey, TValue> or List<T>

This interface is used when you want to implement your own “Equals” method for your object. Once you implement it, it will be used when you are checking for equality in methods like Contains, IndexOf, LastIndexOf or Remove.

For more info please go to http://msdn.microsoft.com/en-us/library/ms131187.aspx

An example of this can look like this: (I will use this class in my next post)


using System;
using System.Collections.Generic;

namespace ValidateSiteOwners
{
    class OwnerInfo : IEquatable<OwnerInfo>
    {
        private string _name;
        private string _role;
        private bool _isAdmin;
        string _siteUrl;
        private bool _distinct;
        public string Name
        {
            get { return _name;}
            set { _name = value;}
        }

        public string Role
        {
            get { return _role; }
            set { _role = value; }
        }

        public bool IsAdmin
        {
            get { return _isAdmin; }
            set { _isAdmin = value; }
        }

        public bool Distinct
        {
            get { return _distinct; }
            set { _distinct = value; }
        }

        public string SiteUrl
        {
            get { return _siteUrl; }
            set { _siteUrl = value; }
        }

        public OwnerInfo()
        {
            _name = string.Empty;
            _role = string.Empty;
            _siteUrl = string.Empty;
            _isAdmin = false;
        }

        public OwnerInfo(string name, string role, bool isAdmin, string siteUrl, bool distinct)
        {
            _name = name;
            _role = role;
            _isAdmin = isAdmin;
            _siteUrl = siteUrl;
            _distinct = distinct;
        }

        public bool Equals(OwnerInfo other)
        {
            if (this.Distinct)
                if (this.Name.ToLower() == other.Name.ToLower() && this.Role.ToLower() == other.Role.ToLower() && this.IsAdmin == other.IsAdmin)
                {
                    return true;
                }
                else
                {
                    return false;
                }
            else
                if (this.Name.ToLower() == other.Name.ToLower() && this.Role.ToLower() == other.Role.ToLower() && this.IsAdmin == other.IsAdmin && this.SiteUrl == other.SiteUrl)
                {
                    return true;
                }
                else
                {
                    return false;
                }
        }

        public override bool Equals(Object obj)
        {
            if (obj == null)
                return false;

            OwnerInfo ownerObj = obj as OwnerInfo;
            if (ownerObj == null)
                return false;
            else
                return Equals(ownerObj);
        }

        public static bool operator ==(OwnerInfo owner1, OwnerInfo owner2)
        {
            if ((object)owner1 == null || ((object)owner2) == null)
                return Object.Equals(owner1, owner2);
            return owner1.Equals(owner2);
        }

        public static bool operator !=(OwnerInfo owner1, OwnerInfo owner2)
        {
            if (owner1 == null || owner2 == null)
                return !Object.Equals(owner1, owner2);
            return !(owner1.Equals(owner2));
        }
    }
}

Please note that my class implements IEquatable by overriding the implementation of IEquatable<T>.Equals, Object.Equals

Cheers,

Nader Heshmat

Post Navigation