Hide Equals and ReferenceEquals

I am creating an inner class to create the construction of a Contract template.

using System; using System.Runtime.Serialization; namespace DCS2000.Common35.Core { public class Assertion { public static void Ensure(bool test) { if (!test) { throw new PreconditionException("Precondition test failed"); } } public static void Ensure(object obj) { if (obj == null) { throw new PreconditionException("Precondition null object failed"); } } public static void Require(bool test) { if (!test) { throw new PostconditionException("Postcondition test failed"); } } public static void Require(object obj) { if (obj == null) { throw new PostconditionException("Postcondition null object failed"); } } } } 

When a developer uses this, they will see them as parameters in Intellisense:

  • Make sure
  • Equally
  • ReferenceEquals
  • Require

This is confusing, and I am wondering if there is a way to hide Equals and ReferenceEquals.

NOTE. I already tried this, but for me it did not work:

  [EditorBrowsable(EditorBrowsableState.Never)] public override bool Equals(object obj) { return base.Equals(obj); } 
+4
source share
3 answers

Using:

  [EditorBrowsable(EditorBrowsableState.Never)] public override bool Equals(object obj) { throw new Exception("Assertion does not implement Equals, use Ensure or Require"); } [EditorBrowsable(EditorBrowsableState.Never)] public new bool ReferenceEquals(object objA, object objB) { throw new Exception("Assertion does not implement ReferenceEquals, use Ensure or Require"); } 

This will hide the participants if the developer sets the appropriate VS settings and immediately notifies, alas, at runtime, that they improperly use Equals or ReferenceEquals if they inadvertently use it in the code base.

+2
source

To add Matti's answer, EditorBrowsableState.Never depends on the Visual Studio user settings in the Options, Text Editor, C #, General.

It is only valid if the user has enabled "Hide advanced elements". Visual Studio shows all participants by default.

+4
source

Honestly, I think that anyone who calls himself a .NET developer should be used to use these methods and ignore them when it's not needed.

If you really want to hide them, and if they are both redefinable (I forget that ReferenceEquals is because I never used it), you can override them as private override .

Well, that doesn't work. Now, when I looked at it, private override does not make sense, protected override also not allowed, and private new and protected new create new methods while the base class method is still available. I don’t think you can hide them with access modifiers.

Nevertheless, all this concerned static methods. Yes, I really failed this one.

-2
source

All Articles