none
Why does a static class have "Equals" and "ReferenceEquals"? RRS feed

  • Question

  • Hi all:

    As far as I see, a static class something like a sealed class with all the static methods, properties……,ect. It shouldn't inherit from any classes (even from object). Because any instance-based methods cannot be used directly there.

    But——to my surprise!Even if I define:

     public static class ConstDefination
        {
            public const string CONNECTIONID = "123456789";
            public const string HUBNAME = "PageHub";
        }

    From the intellisense you can see:

    '

    Why?Anyone kind to tell me?

    Thanks!


    ASP.NET Forum
    Other Discussion Forums
    FreeRice Donate
    Issues to report
    Free Tech Books Search and Download

    Friday, February 21, 2014 5:15 AM

Answers

  • " It shouldn't inherit from any classes (even from object)."

    Why not? Every class inherits from object, sealed just means that you cannot inherit from it, not that the class doesn't inherit from anything.

    "based methods cannot be used directly there."

    Those aren't instance methods, Object has 2 static methods - Equals(object, object) and ReferenceEquals(object, object).

    • Marked as answer by ThankfulHeart Saturday, February 22, 2014 5:58 AM
    Friday, February 21, 2014 6:16 AM
    Moderator
  • Hi, static class cannot be inherited, but it can inherit from another class, for example:

            class c
            {
                public static void t()
                {
                }
            }
    
            static class c1 : c
            {
                public static void t1()
                {
                    
                }
            }

    and you can use

    c1.t();

    all classes is inherit from Object class, 

    and there are 2 static method in Object class: "Equals" and "ReferenceEquals", so you can call these 2 static methods from all classes

    • Marked as answer by ThankfulHeart Saturday, February 22, 2014 5:58 AM
    Friday, February 21, 2014 6:35 AM

All replies

  • " It shouldn't inherit from any classes (even from object)."

    Why not? Every class inherits from object, sealed just means that you cannot inherit from it, not that the class doesn't inherit from anything.

    "based methods cannot be used directly there."

    Those aren't instance methods, Object has 2 static methods - Equals(object, object) and ReferenceEquals(object, object).

    • Marked as answer by ThankfulHeart Saturday, February 22, 2014 5:58 AM
    Friday, February 21, 2014 6:16 AM
    Moderator
  • Hi, static class cannot be inherited, but it can inherit from another class, for example:

            class c
            {
                public static void t()
                {
                }
            }
    
            static class c1 : c
            {
                public static void t1()
                {
                    
                }
            }

    and you can use

    c1.t();

    all classes is inherit from Object class, 

    and there are 2 static method in Object class: "Equals" and "ReferenceEquals", so you can call these 2 static methods from all classes

    • Marked as answer by ThankfulHeart Saturday, February 22, 2014 5:58 AM
    Friday, February 21, 2014 6:35 AM