All docs
V21.1
21.1
20.2
20.1
19.2
19.1
The page you are viewing does not exist in version 19.1. This link will take you to the root page.
18.2
The page you are viewing does not exist in version 18.2. This link will take you to the root page.
18.1
The page you are viewing does not exist in version 18.1. This link will take you to the root page.
17.2
The page you are viewing does not exist in version 17.2. This link will take you to the root page.

How to: Create Regions for Explicit Interface Implementations

  • 2 minutes to read

CodeRush can wrap sorted groups in dynamic regions with built-in variables. This example shows how to wrap explicit interface implementations groups in regions.

Follow the steps below.

  • Copy the following code and paste it to your project.
using System;
using System.Collections.Generic;
using System.Runtime.Serialization;

namespace Project {

    [Serializable]
    class PersonInfo : IDisposable, ISerializable, IEquatable<PersonInfo> {

        public string Name { get; set; }
        public int Age { get; set; }
        public string Id { get; set; }
        void IDisposable.Dispose() {
        }

        protected PersonInfo(SerializationInfo info, StreamingContext context) {
            if (info == null)
                return;
            Name = info.GetString(nameof(Name));
            Age = info.GetInt32(nameof(Age));
            Id = info.GetString(nameof(Id));
        }
        void ISerializable.GetObjectData(SerializationInfo info, StreamingContext context) {
            if (info == null)
                return;
            info.AddValue(nameof(Name), Name);
            info.AddValue(nameof(Age), Age);
            info.AddValue(nameof(Id), Id);
        }
        public override bool Equals(object obj) {
            if (obj is PersonInfo)
                return this.Equals((PersonInfo)obj);
            return base.Equals(obj);
        }
        public static bool operator ==(PersonInfo first, PersonInfo second) {
            if ((object)first == null)
                return (object)second == null;
            return first.Equals(second);
        }
        public static bool operator !=(PersonInfo first, PersonInfo second) {
            return !(first == second);
        }
        bool IEquatable<PersonInfo>.Equals(PersonInfo other) {
            if (ReferenceEquals(null, other))
                return false;
            if (ReferenceEquals(this, other))
                return true;
            return Equals(Name, other.Name) && Age.Equals(other.Age) && Equals(Id, other.Id);
        }
        public override int GetHashCode() {
            unchecked {
                int hashCode = 47;
                if (Name != null)
                    hashCode = hashCode * 53 ^ EqualityComparer<string>.Default.GetHashCode(Name);
                hashCode = hashCode * 53 ^ Age.GetHashCode();
                if (Id != null)
                    hashCode = hashCode * 53 ^ EqualityComparer<string>.Default.GetHashCode(Id);
                return hashCode;
            }
        }
    }
}
  • Open the Editor | All Languages | Organize Members options page to configure the Member Organization rules feature.

    open-organize-members-page

  • Select the Explicit interface implementations rule from the member organization rules list.

    explicit-interface-implementations-page

NOTE

The default Rule priority setting is High priority for explicit interface implementations. This allows CodeRush to group these interface implementations prior to other rules.

  • In the “Sort by” section, enable the Wrap distinct groups in regions option to activate wrapping of explicit interface implementations in regions.

    wrap-groups-in-region-option

  • Type “{ExplicitInterfaceName} members” in the “Region name” text box.

    type-region-name

  • Click Apply and OK to save changes and close the Organize Members options page.

  • Switch to the code editor.

  • Place the caret to any place in the class body.

  • Press Ctrl+. or Ctrl+~ to invoke the Code Actions Menu

  • Choose Organize Members from the list and press Enter.

wrap-interface-region

CodeRush wraps explicit interface implementations in regions and places these regions in the beginning of the class.

See Also