Anindya Chatterjee
Anindya Chatterjee

Reputation: 5964

Class library reference problem

I am building a class library and using its default namespace as "System". There suppose I am creating a generic data structure say PriorityQueue and putting it under System.Collections.Generic namespace.

Now when I am referencing that library from another project, I can't see PriorityQueue under "System.Collections.Generic" namespace anymore. Though the library is referenced I can not access any of the classes in it.

Can anyone shed some light on it please. I know that if I change the namespace everything will be ok, but I want to create a seamless integration like .net framework itself with other project, so that one can refer the library and forget about its namespaces.

Upvotes: 1

Views: 268

Answers (6)

Hans Passant
Hans Passant

Reputation: 942207

Did somebody mention yet that this is a bad idea? There are few reasons you wouldn't be able to see the class. Short from the assembly reference, there is only one good one: you forgot to declare the class public.

Upvotes: 1

3Dave
3Dave

Reputation: 29061

In case it wasn't clear: This is a REALLY bad idea.

The System name space should be considered reserved and verboten. If Microsoft decides to introduce a class in a framework update that conflicts with your System.mycrap.blah identifier in the future, you're going to have some pretty hefty refactoring on your hands, and, in the case of an app that's deployed to a client, an emergency update and potential liability for system downtime.

You wouldn't create your own class called "String." By the same token (pun), don't use reserved namespaces.

Also, the namespace "System" doesn't really describe the contents of your namespace. Typically, namespaces should mean something - like, BlogEngine, DatabaseCore, etc. Slapping everything into System is a lot like naming all of your variables "x," or "temp," and implies that the creator doesn't really understand the point of this level of code delineation and organization.

Upvotes: 0

Josh Sterling
Josh Sterling

Reputation: 848

Putting stuff in system namespaces is a bad idea. Firstly it's better to know explicitly where the stuff your using is. However more importantly, if Microsoft releases new stuff that causes a naming conflict with yours, your stuff breaks.

The second reason is probably why you cant see your code.

Upvotes: 2

John Saunders
John Saunders

Reputation: 161821

This is a very bad idea. Pretend you didn't think it up, and use a real namespace.

One does not have "seamless integration" with the .NET Framework, either. If we want to access the List<T> class, then we have to write

using System.Collections.Generic;

If you put your class in MyCompany.Collections.Generic, then you'll get exactly the same level of "seamlessness" that is achieved by the .NET Framework itself.

Upvotes: 3

Femaref
Femaref

Reputation: 61497

Just create your own namespace, e.g. Anindya.Collections.Generic, as placing classes in predefined framework namespaces isn't a good idea. MS might introduce a same class in a later framework, leading to problems.

Upvotes: 1

Scott Weinstein
Scott Weinstein

Reputation: 19117

If you are using the System namespace for your classes, then they will be found in System.

If you want them to be found in System.Collections.Generic, then you need to place them there.

But let's be clear, placing classes in System.* is a bad idea.

Upvotes: 2

Related Questions