3

in c# if I want to add other System namespaces why do I need to call each namespace?

For example if I want to call the System.Text namespace I have to use:

using System;
using System.Text;
  • why cant I just use using System?
  • Why do I also have to call using System.Text?
  • Why cant I use all System namespaces by just using System?
  • Why we use System for all namespace ?and what the meanings of system ?
Ramesh Rajendran
  • 37,412
  • 45
  • 153
  • 234

6 Answers6

3

You actually just need

using System.Text;

if you only want to use the System.Text namespace.

Within the System namespace, there are various types, eg System.DateTime. System.Text is not in System though, it is a separate namespace.

The confusion is caused by there being types at each "level" of a namespace. This means eg System.DateTime and System.Text both appear to be in System, when in reality the former is a type in System and the latter is a completely different namespace.

David Arno
  • 42,717
  • 16
  • 86
  • 131
  • 1
    Could you elaborate on why the naming conventions are bad? Maybe it's just because I've worked with .NET for years now, but it makes sense to me, and we use the same naming convetions in our own applications. – Lars Kristensen Oct 25 '13 at 12:22
  • @LarsKristensen, I have tried to do that. Hopefully I haven't made my answer overly complex in the process! – David Arno Oct 25 '13 at 12:29
  • Well, it is a little tricky to understand what you want to express - That edit actually made it sound like you think the naming convetion used is a good approach? :) – Lars Kristensen Oct 25 '13 at 12:33
  • @LarsKristensen I was aiming for neutrality, but clearly just achieved confusion instead. I've removed the fluff therefore and tried to get to the point :) – David Arno Oct 25 '13 at 12:40
  • I think you are mixing stuff now - System.DateTime is a type, and System.Text is a namespace. And I still don't know why you think the naming convention is bad. – Lars Kristensen Oct 25 '13 at 12:43
3

the System namespace refers only to the files directly found under System. It does not automatically include all System.* namespaces. This is intended, since these are specialized namespaces which aren't needed in every class. Not every project automatically needs the System.Web namespace, for example.

Nzall
  • 3,439
  • 5
  • 29
  • 59
1

Here's a visual example that may help you out a little.

Basically, you can't access the namespace .Text from namespace .System because the methods contained within the .Text namespace do not exist in the .System namespace.

namespace OuterNamespace
{
    public class DoStuff
    {
        public DoStuff()
        {
            //This DoStuff is different...
        }
    }
    namespace InnerNamespace
    {
        public class DoStuff
        {
            public DoStuff()
            {
                //than this DoStuff.
            }
        }
    }
}

public class Test
{
    public Test()
    {
        //This "DoStuff" class
        OuterNamespace.DoStuff outerStuff = new OuterNamespace.DoStuff();

        //Is different than this "DoStuff" class
        OuterNamespace.InnerNamespace.DoStuff innerStuff = new OuterNamespace.InnerNamespace.DoStuff();
    }
}
Howard Renollet
  • 4,609
  • 1
  • 24
  • 31
0

Because they're different namespaces.

Peter Davidsen
  • 668
  • 5
  • 11
  • Is not an answer, because System is a collection, filled with for example ".Text", why isn't the compiler in library in library System? – Max Oct 25 '13 at 12:16
  • Is being in different assemblies an answer? – Peter Davidsen Oct 25 '13 at 12:17
  • @MaxMommersteeg No, `System` is not a collection filled with eg `.Text`. Please see my answer for clarification. – David Arno Oct 25 '13 at 12:19
  • @DavidArno yeah I updated your answer, since it did some clarification at that point, but Peter 's answer is just to little to less. – Max Oct 25 '13 at 12:21
0

This article explains how to use Namespaces. Especially the example regarding Fully Qualified Names might be interesting to you.

germi
  • 4,628
  • 1
  • 21
  • 38
0

System and System.Text are two different namespaces and that's why you had to refer both.

The System namespace contains fundamental classes and base classes that define commonly-used value and reference data types, events and event handlers, interfaces, attributes, and processing exceptions

The System.Text namespaces contain types for character encoding and string manipulation. A child namespace enables you to process text using regular expressions.

Now if you are confused about naming style then I would stick to Microsoft naming convention since it clearly isolate relating types and easy to iterate. This make stepped type iteration than listing all CLR types within one System namespace.

More about namespaces

S.N
  • 4,910
  • 5
  • 31
  • 51