C# Nesting

Please support our C# advertiser: Intel Parallel Studio Home
Reply

Join Date: Dec 2005
Posts: 109
Reputation: complete is an unknown quantity at this point 
Solved Threads: 0
complete's Avatar
complete complete is offline Offline
Junior Poster

C# Nesting

 
0
  #1
Jan 4th, 2008
Can someone explain the nesting in C#? You have a namespace. Inside the namespace, you have a class. Then, inside this class, you have the main function. Why is it set up this way. Are all classes supposed to be ouside this namespace? When would you onclude a class inside this namespace?
Reply With Quote Quick reply to this message  
Join Date: Aug 2006
Posts: 2,065
Reputation: Ramy Mahrous is just really nice Ramy Mahrous is just really nice Ramy Mahrous is just really nice Ramy Mahrous is just really nice 
Solved Threads: 256
Featured Poster
Ramy Mahrous's Avatar
Ramy Mahrous Ramy Mahrous is offline Offline
Postaholic

Re: C# Nesting

 
0
  #2
Jan 5th, 2008
Look,
Nested namespaces: used collect all related-functionality classes inside a parent namespace like
  1. namespace System
  2. {
  3. class string...
  4. class int...
  5. class .....
  6. namespace IO
  7. {
  8. class File...
  9. class Stream....
  10. class .....
  11. }
  12. }

Class inside class to limit the functionality of nested class to the parent class as you don't need your assembly or your nested class namespace classes to use nested class functionality (in short: more encapsulation)

Class outside namespace, won't be used except your assembly. (Don't know why some developers do that!!! as you can flag your class with internal keyword)

Any questions?!
BI Developer | LINKdotNET
B.Sc Computer Science, Helwan University
Technical blog | http://ramymahrous.wordpress.com
LinkedIn | http://www.linkedin.com/in/ramymahrous
Reply With Quote Quick reply to this message  
Join Date: May 2006
Posts: 1,580
Reputation: Infarction has a spectacular aura about Infarction has a spectacular aura about Infarction has a spectacular aura about 
Solved Threads: 52
Infarction's Avatar
Infarction Infarction is offline Offline
Battle Programmer

Re: C# Nesting

 
0
  #3
Jan 6th, 2008
The main reason for enforcing the namespaces is to not pollute the naming scope and to keep the code better organized.

In C, for example, there are neither classes nor namespaces. This means any function has to have a unique name and you can easily end up with a bunch naming conflicts.

In C++, there are classes, which help alleviate a small bit of the problem (as related operations are grouped together), and there are namespaces as well. The namespaces allow you to group classes in a more logical manner. For instance, variations of a vector class can represent a 2D vector, a 3D vector, or an alternative to an array. Since the names for all 3 of these would conflict, with namespaces we can still call each class vector but have them reside in different naming scopes. C++, however, doesn't strictly enforce a namespace structure, so if you create a class named string and put it in the global scope, then it will have a name conflict with any other class named string in the same scope.

C# makes you put your code in namespaces to avoid global type name conflicts. This doesn't totally eliminate the conflicts though, because you can bring whole namespaces into scope with the using clause. When that happens, you need to refer to the classes by Namespace.ClassName to distinguish between them.
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:



Similar Threads
Other Threads in the C# Forum
Thread Tools Search this Thread



Tag cloud for C#
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC