The only difference I see is the fact that you can’t use the "using staticClass" declaration.
Therefore, I’m wondering:
- Is there a real difference between a static class and a namespace?
- Is there a possibility to avoid having to rewrite the class name every time a member function is called? I’m thinking about something analogous to "using staticClass".
Yes, a
staticclass is technically a type. It can have members (fields, methods, events). A namespace can only hold types (and it’s not considered a “type” by itself;typeof(System)is a compile-time error).There’s no direct equivalent to adding a
usingdirective for a namespace for a static class. You can, however, declare aliases:and use
when referring its members.
Additionally, you can use static classes to declare extension methods on other types and use them directly without referring to the class name explicitly:
and use it like:
Of course, you’ll have to add a
usingdirective for the namespace containing the class to use the extension method if the class is not declared in the root or current namespace.