Skip to main content

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.

  1. 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;
                }
            }
        }
    }
    
  2. Open the Editor | All Languages | Organize Members options page to configure the Member Organization rules feature.

    open-organize-members-page

  3. 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.

  4. 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

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

    type-region-name

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

  7. Switch to the code editor.

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

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

  10. Choose Organize Members from the list and press Enter.

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

wrap-interface-region

See Also