Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the difference between a static class and a namespace? (in C#)

Tags:

c#

The only difference I see is the fact that you can't use the "using staticClass" declaration.

Therefore, I'm wondering:

  1. Is there a real difference between a static class and a namespace?
  2. 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".
like image 420
Dimitri C. Avatar asked Jan 06 '10 14:01

Dimitri C.


People also ask

What is the difference between class and namespace?

Classes are data types. They are an expanded concept of structures, they can contain data members, but they can also contain functions as members whereas a namespace is simply an abstract way of grouping items together. A namespace cannot be created as an object; think of it more as a naming convention.

What is the difference between static and class?

The difference between a static class and a non-static class is that a static class cannot be instantiated or inherited and that all of the members of the class are static in nature. To declare a class as static, you should mark it with the static keyword in the class declaration.

What are the two types of namespaces?

When creating a namespace, you must choose one of two namespace types: a stand-alone namespace or a domain-based namespace. In addition, if you choose a domain-based namespace, you must choose a namespace mode: Windows 2000 Server mode or Windows Server 2008 mode.

Can namespace and class have same name?

Inside a namespace, no two classes can have the same name.


3 Answers

Yes, a static class 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 using directive for a namespace for a static class. You can, however, declare aliases:

using ShortName = ReallyReallyLongStaticClassName;

and use

ShortName.Member

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:

public static class IntExtensions {
   public static int Square(this int i) { return i * i; }
}

and use it like:

int i = 2;
int iSquared = i.Square(); // note that the class name is not mentioned here.

Of course, you'll have to add a using directive for the namespace containing the class to use the extension method if the class is not declared in the root or current namespace.

like image 131
mmx Avatar answered Sep 25 '22 07:09

mmx


Static class is still a class. It can contain methods, properties, etc. Namespace is just a namespace. It's just a helper to distinguish between class declarations with the same name.

Function can't live in a namespace alone, it belongs to a class.

Extensions is probably what you are looking for, if you need a static function without mentioning the name of the class.

public static class MathExtensions
{
 public static int Square(this int x)
 {
  return x * x;
 }
}
//...
// var hundredSquare = 100.Square();
like image 42
George Polevoy Avatar answered Sep 24 '22 07:09

George Polevoy


One other difference is that namespaces can span several assemblies, while classes cannot.

like image 32
R. Martinho Fernandes Avatar answered Sep 25 '22 07:09

R. Martinho Fernandes