7

I'm new to C# and I can't seem to find any info on this so I will ask it here.

Do classes in namespaces have to ever be declared?

using System;
public class myprogram
{
    void main()
    {
        // The console class does not have to be declared?
        Console.WriteLine("Hello World");
    }
}

If I'm not using a namespace then I have to declare a class

class mathstuff
{
    private int numberone = 2;
    private int numbertwo = 3;
    public int addhere()
    {
        return numberone + numbertwo;
    }


using System;
public class myprogram
{
    void main()
    {
        // the class here needs to be declared.
        mathstuff mymath = new mathstuff();
        Console.WriteLine(mymath.addhere());
    }
}

Am I understanding this correctly?

svick
  • 236,525
  • 50
  • 385
  • 514
Ralph
  • 889
  • 14
  • 25
  • This could be better explain [here](http://msdn.microsoft.com/en-us/library/z2kcy19k(v=vs.80).aspx). One thing you should remember is that if you don't declare a namespace, a global namespace is automatically declared. – von v. Mar 17 '13 at 16:56
  • I don't see any mention of namespaces in either of the 2 code examples you provide. Are you not actually referring to the difference between static and non-static classes? – levelnis Mar 17 '13 at 17:01
  • I don't think the word "declared" means what you think it means. What do you think "declared" means? – Eric Lippert Mar 17 '13 at 18:33

2 Answers2

12

A namespace is simply a way to make clear in which context the class is living in. Think of your own name, Ralph. We have many Ralphs in this world, but one of that is you. An extra way to get rid of the ambiguity is to add your surname. So that if we have 2 Ralphs, we have a bigger chance of talking about you.

The same works for classes. If you define class AClass and you would have the need of define another class AClass there would be no way to distinguish between the two. A namespace would be that 'surname'. A way of having to classes, but still able to distinguish between the two different classes, with the same name.

To answer your question, it has nothing to do with "not having to declare". It would only be easier to write code.

For example:

using System;

public class myprogram
{
    void main()
    {
        // the class here needs to be declared.
        Console.WriteLine("blah");
    }
}

Because of the using System; you don't have to declare the namespace of Console. There is only one Console available, which lives in the System namespace. If you wouldn't declare your using System; namespace then you'd need to explain where Console can be found. Like this.

System.Console.WriteLine("blah");

From MSDN:

The namespace keyword is used to declare a scope. This namespace scope lets you organize code and gives you a way to create globally unique types.

For more info check MSDN for namespace.

bas
  • 13,550
  • 20
  • 69
  • 146
  • 1
    I've actually seen developers use their own name, e.g. `Ralph`, as a namespace for their C# classes. – p.s.w.g Mar 17 '13 at 17:09
  • Thanks for the responses. Isn't Writeline a method within the Console class? Yet Console does not need to be declared to use it. Or is Console not a class within System? – Ralph Mar 17 '13 at 17:14
  • @Ralph, Yes, it is a static class in System namespace, thus you can use it without instantiating if that is what you mean. – bas Mar 17 '13 at 17:37
  • @p.s.w.g awesome comment ! That I did never see :p – bas Mar 17 '13 at 17:39
  • @bas thanks for clearing that up for me, that is what I wanted to know. – Ralph Mar 17 '13 at 20:24
  • @Ralph not a problem. Please accept the question if it's resolved. Cheers – bas Mar 17 '13 at 20:25
6

I think what you mean is "can you declare a class without a namespace?". Yes you can, it's referred to as the global namespace.

class BaseClass
{
}

class SubClass : global::BaseClass
{
}

However, this is very bad practice, and you should never do this in a production application.

p.s.w.g
  • 146,324
  • 30
  • 291
  • 331