Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Namespace vs Class Declaration

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?

like image 780
Ralph Avatar asked Mar 17 '13 16:03

Ralph


People also ask

What is difference between namespace and class?

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.

Is namespace same as class in C++?

The namespace and classes are two different concepts. Classes are datatypes. Classes are basically extended version of structures. Classes can contain data members and functions as members, but namespaces can contain variables and functions by grouping them into one.

Can namespace be same as class name?

The Framework Design Guidelines say in section 3.4 “do not use the same name for a namespace and a type in that namespace”.

What is difference between namespace and library?

Namespaces are used to organize code into logical groups and to prevent name collisions that can occur especially when your code base includes multiple libraries. In programming, a library is a collection of precompiled routines that a program can use.


2 Answers

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.

like image 116
bas Avatar answered Nov 02 '22 11:11

bas


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.

like image 22
p.s.w.g Avatar answered Nov 02 '22 10:11

p.s.w.g