Azurewrath

Hi all,

Is there a way to make nested libraries like the .net framework, as in:

System.Array.CoolMethod();

Thanks,

aw



Re: Visual C# Language How to make class libraries like this?

bmains

Hey,

In your example, Array is a class, and CoolMethod() is a static method. The Array class is in the System namespace. Sure, you can create nested classes (not libraries) and defined it through:

namespace System

{

public class Array

{
public static void CoolMethod() { }

}

}





Re: Visual C# Language How to make class libraries like this?

Nayan Paregi

Hi

u have misunderstanding this is not a nested library..

Here System is Namespace

and Array is the Class

and CoolMethod() is the Static/Shared Method. u can do this easily. by creating diffrent namespace and classes in that namespace.






Re: Visual C# Language How to make class libraries like this?

Azurewrath

Thanks guys. Then what's the difference between nested classes and namespaces

Thanks,

aw





Re: Visual C# Language How to make class libraries like this?

OmegaMan

If you want a nested namespace just add the period to it such as

Code Snippet


namespace top.middle.bottom
{
class myClass { ... }
}



... usage



top.middle.bottom.myClass mc = new top.middle.bottom.myclass();







Re: Visual C# Language How to make class libraries like this?

Azurewrath

Thanks it's very helpful. Is there a way to nest both the namespace and classes in the same entry So like

namespace

class

namespace

class

class

...

Thanks again,

aw





Re: Visual C# Language How to make class libraries like this?

OmegaMan

No a class cannot contain a namespace it can only be contained by a namespace.





Re: Visual C# Language How to make class libraries like this?

Azurewrath

Thanks alot. Then can you nest namespaces into each other

Thanks,

aw





Re: Visual C# Language How to make class libraries like this?

OmegaMan

yes a namespace can contain a namespace within and be contained by a top level namespace...hence the top.middle.bottom could be expressed by

Code Snippet

namespace top
{
namespace middle
{
namespace bottom
{
public myClass {...}
}
}
}







Re: Visual C# Language How to make class libraries like this?

Azurewrath

Thanks alot! Just out of curiosity is naming a namespace like top.middle.bottom would be confusing Like it might lead people into thinking it's like a nested namespace

Also is this how the net framework is built

Thanks again,

aw





Re: Visual C# Language How to make class libraries like this?

OmegaMan

The ideas of namespaces is for these purposes
  1. Avoid name collisions with same named classes and objects.
  2. Grouping of like functionality.
The .Net framework is for the most part logically grouped and the hierarchy of the levels infers information about those levels.

As to what you call your namespaces is up to you but, yes anything can be confusing, so use it wisely.