-5

I have a little confusion in .net System.Collections.Generic System is a namespace, collections is a project inside that namespace, generics is a class inside the collections project. So list, array, queue ... all are inside generics. How it is possible to put a class inside another class.

Oral Resu
  • 54
  • 3
  • 3
    Have you tried doing it yourself? C# has always allowed this – UnholySheep Apr 30 '19 at 19:13
  • 4
    That is a namespace, not a class. – SLaks Apr 30 '19 at 19:13
  • 7
    *"collections is a project"* - Nope, it's a namespace. *"generics is a class"* - Also a namespace. – David Apr 30 '19 at 19:14
  • 1
    `System` is a namespace. So is `System.Collections`, and so is `System.Collections.Generic`. Those are all namespaces. Where did you get the idea that one was a project, or that `System.Collections.Generic` was a *class*? –  Apr 30 '19 at 19:16
  • I get the sense English isn't your first language, so I'll cut you a break, but I'm not sure we really understand what you're asking here, unfortunately. – senfo Apr 30 '19 at 19:19
  • System.Collections.Generic is not a class, but as a side note, of course you can define a class inside another class: `class A { class B { } static void Test() { var x = new A.B(); } };` These are called ["nested classes"](https://stackoverflow.com/a/48879/424129) (thanks senfo!) but **you are not looking at one here**. – 15ee8f99-57ff-4f92-890c-b56153 Apr 30 '19 at 19:20
  • hey @Amy the dot between system and collection means that collection is a subpart of system. if system is a namespace so collection might be a project inside that. – Oral Resu Apr 30 '19 at 19:20
  • 2
    @OralResu That's not correct. You're making a lot of assumptions here, and I'm not sure where you're getting them from. `System.Collections.Generic` is a namespace within a namespace within a namespace. *None* of those names refers to a project, nor can refer to a project. – 15ee8f99-57ff-4f92-890c-b56153 Apr 30 '19 at 19:21
  • 1
    @EdPlunkett I think you mean "nested classes". – senfo Apr 30 '19 at 19:22
  • 1
    @OralResu "Project" has no bearing on all this. I can have a project with many namespaces, or even no namespaces. By default when you create a new C# project, the namespace for files created in the root folder will match the name of the but you can easily change that if you want. – mason Apr 30 '19 at 19:23
  • 1
    I'm upvoting this question because, while it's a question only a novice C# programmer would ask, the teeming loins of our planet are producing novice C# programmers at a virtually unimaginable rate. It adds more marginal value than another iteration of "why can't I bind to a private field in WPF", or "why is my uninitialized reference null". – 15ee8f99-57ff-4f92-890c-b56153 Apr 30 '19 at 19:26
  • 1
    @EdPlunkett You mean system is a namespace and collections is a nested namespace inside system and ...am I right? – Oral Resu Apr 30 '19 at 19:33
  • @OralResu That's correct. Namespaces can nest very deeply, perhaps arbitrarily deeply (I haven't looked this up in the standard). Project names don't exist as identifiers in C#. When you create a project in VS, it's *default* namespace will be the same as the name of the project, but you can go ahead and change either one and the other won't care -- the tool just happens to use the same string for both, for convenience. – 15ee8f99-57ff-4f92-890c-b56153 Apr 30 '19 at 19:33
  • 2
    @EdPlunkett I asked because i don't know. and i don't care if someone is upvoting or downvoting my question. I care just answers. thanks for you help – Oral Resu Apr 30 '19 at 19:35

2 Answers2

3

System.Collections.Generic is a namespace. Not a class.

System.Collections is a namespace.

System is a namespace.

None of these have anything do with projects. They could all be in the same project, or they could all be in separate projects. One namespace could even contain code from many different projects.

When you create a new project in Visual Studio, say "Assembly1", the default root namespace for the new project will also be Assembly1. However, there's no requirement for the two to be the same: You can rename the project to MyNewAssembly, and the namespace name will not change. If another project links to MyNewAssembly.dll, the types in your assembly still will be found in the Assembly1 namespace.

None of these are classes. However, you can have a class inside another class. We typically call these nested classees.

mason
  • 31,774
  • 10
  • 77
  • 121
1

The C# standard says namespaces are declared with the namespace keyword, followed by the name of the namespace, the body, and optionally followed by a semicolon.

Taken from the standards page:

namespace_declaration
    : 'namespace' qualified_identifier namespace_body ';'?
    ;

qualified_identifier
    : identifier ('.' identifier)*
    ;

namespace_body
    : '{' extern_alias_directive* using_directive* namespace_member_declaration* '}'
    ;

The qualified_identifier of a namespace_declaration may be a single identifier or a sequence of identifiers separated by "." tokens. The latter form permits a program to define a nested namespace without lexically nesting several namespace declarations. For example,

namespace N1.N2
{
    class A {}

    class B {}
}

This is semantically equivalent to the following:

namespace N1
{
    namespace N2
    {
        class A {}

        class B {}
    }
}

For all intents and purposes, you can ignore pretty much all of that and simply consider System.Collections.Generic to be the namespace where the List<> class "lives".

If you're curious, check out the source code for Systems.Collections. You can find the implementation of the List<> class on line #292. As you can see on line #43, it's declared within System.Collections.Generic namespace.

senfo
  • 28,488
  • 15
  • 76
  • 106