Why are static classes considered “classes” and “reference types”?

前端 未结 10 1154
生来不讨喜
生来不讨喜 2021-02-01 04:54

I’ve been pondering about the C# and CIL type system today and I’ve started to wonder why static classes are considered classes. There are many ways in which they are not really

10条回答
  •  小鲜肉
    小鲜肉 (楼主)
    2021-02-01 05:44

    Static classes and "normal" classes (and structs) are containers for executable code (members fields, properties, methods) and they declare a Type. If they had a separate word for this then we would ask the opposite ("if they are so similar, why did you not use the kayword class?"). I'd suggest "CLR via C#", where it's well explained how type resolving, method calling, etc occurs. It works in the same way for "both" classes, just instance members have additional parameter passed in for the instance object.

    Classes are not like namespaces because they are only for naming and referencing. They do not affect the functionality of the class.

    Classes are also different from interfaces, because interfaces are merely compile-time verification tools and do not have functionality of their own.

提交回复
热议问题