ArtsAutosBooksBusinessEducationEntertainmentFamilyFashionFoodGamesGenderHealthHolidaysHomeHubPagesPersonal FinancePetsPoliticsReligionSportsTechnologyTravel

What You Need to Know about Namespaces

Updated on January 9, 2018
tamarawilhite profile image

Tamara Wilhite is a technical writer, industrial engineer, mother of 2, and a published sci-fi and horror author.

What Are Namespaces?

Namespaces are used to organize class libraries in several different programming languages.

Names declared in one namespace will not conflict with variable names declared in a separate namespace. Place classes in namespaces to prevent conflict with names in your code with the code written by others. Create namespace aliases to reduce the length of the namespace reference as it is entered and re-entered in the code.

The methods of declaring namespaces, creating namespace aliases and using sub-namespaces depend on the software language being used.

Namespaces let you reuse class names without creating conflict.
Namespaces let you reuse class names without creating conflict. | Source

ASP.NET Namesspaces

The ASP.NET platform uses namespaces to organize base class libraries. Each namespace contains semantically related types within an assembly. For example, the system.IO namespace holds file I/O types such as those used for port manipulation or data compression.

The dot-net runtime uses the same namespaces and same types to run all programs, whereas language specific libraries like MFC use different namespaces and types.
The system.data namespace defines different database types. A single assembly like a dll file may contain several namespaces, while each namespace contains several types.

Nested namespaces fall under a rooted namespace. The system namespace, a root namespace, contains the core types used to build a software application. Nested namespaces under the root namespace have a description of the subcategory listed after a period.
The system.collections namespace defines stock containers and base types. Use the system.data namespace to interact with relational databases.

System.Web is used to build ASP.NET web apps. System.Security namespace holds types related to encryption and access controls. System.XML contains types define how XML data is handled. .NET runtime engines access namespaces.

Programmers explicitly list the namespaces used by files when creating a class, such as a bitmap class defined by the system.drawing namespace. Errors arise when a system.drawing namespace isn't specified but has been called.


Most .NET namespaces reside in the mscorlib.dll file. Many .NET assemblies reside in the global assembly cache directory, also called the GAC. Microsoft Windows' default location for this is the C:\Windows\Assembly folder.

C Namespaces

The C programming language uses namespaces to define declarative regions. Several versions of C use the dot net framework class library.


C allows programmers to use abbreviated namespaces. C programs recognize console.writeline as a reference to the system.console.writeline namespace. Programmers avoid the need to spell out the System namespace at the start of each namespace callout by including the phrase "using System" at the top of the program.


Declare a C namespace with the command "namespace name { //members }". The members can be classes, structures, interfaces and other namespaces. Qualify secondary namespaces with the name of the primary namespace, or the namespace will not be found by the code compiler. You can reference multiple nested namespaces by separating each namespace with a period. C programs without a namespace declared default to a global namespace.


Reference namespaces by aliases with the using directive to avoid spelling out the full namespace designation with the "using alias = name:" command.

C language extern methods handle unmanaged code and methods supplied by external code such as data supplied by a website. Declare extern aliases to create aliases for external assemblies so they do not conflict with your code's namespaces.

Namespaces are used to create Windows Forms.
Namespaces are used to create Windows Forms. | Source

Visual Basic and Visual Studio Namespaces

Visual Studio uses root namespaces called Microsoft and System. The Microsoft namespace is broken down into different programming languages like Visual Basic and C#. Visual Basic or VB programs gain access to namespaces after programmers add a reference to the correct dll library reference.

Create new namespaces with a Namespace statement. Define the classes within the new namespace before ending the statement. Visual Basic assumes qualified namespaces are fully qualified. Declarations fail if a namespace class is missing.

Visual Basic treats unresolved references as partially qualified and searches the current namespace for the variable. Visual Basic searches the next higher level on the namespace hierarchy for the class variable.


Use the Imports statement at the top of a Visual Basic program to avoid mentioning the namespace through the rest of the code.

Windows Forms automatically imports the System root, System.Drawing, System.Data, System.Window.Forms and Microsoft.Visual Basic namespaces. After importing a namespace, create an alias with a Dim command. Create an employee alias with the "Dim Employee As Jobnamespace.Jobclasses.Employee". Refer to the employee alias throughout the rest of the code instead of repeatedly spelling out the full namespace again.


Modify Visual Basic root namespaces through the Solution Explorer's Application tab.
Microsoft Windows Forms use controls defined by the System.Windows.Forms namespace. Both Windows Forms and WPF applications use namespaces to define buttons, data labels and check boxes.

References

  1. "Visual C# 2010 Recipes: A Problem-Solution Approach" by Allen Jones, Adam Freeman, Matthew MacDonald and Rakesh Rajan
  2. "Pro C# 2008 and the .NET 3.5 Platform, Fourth Edition" by Andrew Troelsen
  3. "Pro VB 2010 and the .NET 4.0 Platform" by Andrew Troelsen and Vidya Vrat Agarwal
  4. "C# 4.0: The Complete Reference" by Herbert Schildt
  5. "Visual Basic 2010 Programmer's Reference" by Rod Stephens

Comments

Submit a Comment

No comments yet.

working

This website uses cookies

As a user in the EEA, your approval is needed on a few things. To provide a better website experience, hubpages.com uses cookies (and other similar technologies) and may collect, process, and share personal data. Please choose which areas of our service you consent to our doing so.

For more information on managing or withdrawing consents and how we handle data, visit our Privacy Policy at: https://hubpages.com/privacy-policy#gdpr

Show Details
Necessary
HubPages Device IDThis is used to identify particular browsers or devices when the access the service, and is used for security reasons.
LoginThis is necessary to sign in to the HubPages Service.
Google RecaptchaThis is used to prevent bots and spam. (Privacy Policy)
AkismetThis is used to detect comment spam. (Privacy Policy)
HubPages Google AnalyticsThis is used to provide data on traffic to our website, all personally identifyable data is anonymized. (Privacy Policy)
HubPages Traffic PixelThis is used to collect data on traffic to articles and other pages on our site. Unless you are signed in to a HubPages account, all personally identifiable information is anonymized.
Amazon Web ServicesThis is a cloud services platform that we used to host our service. (Privacy Policy)
CloudflareThis is a cloud CDN service that we use to efficiently deliver files required for our service to operate such as javascript, cascading style sheets, images, and videos. (Privacy Policy)
Google Hosted LibrariesJavascript software libraries such as jQuery are loaded at endpoints on the googleapis.com or gstatic.com domains, for performance and efficiency reasons. (Privacy Policy)
Features
Google Custom SearchThis is feature allows you to search the site. (Privacy Policy)
Google MapsSome articles have Google Maps embedded in them. (Privacy Policy)
Google ChartsThis is used to display charts and graphs on articles and the author center. (Privacy Policy)
Google AdSense Host APIThis service allows you to sign up for or associate a Google AdSense account with HubPages, so that you can earn money from ads on your articles. No data is shared unless you engage with this feature. (Privacy Policy)
Google YouTubeSome articles have YouTube videos embedded in them. (Privacy Policy)
VimeoSome articles have Vimeo videos embedded in them. (Privacy Policy)
PaypalThis is used for a registered author who enrolls in the HubPages Earnings program and requests to be paid via PayPal. No data is shared with Paypal unless you engage with this feature. (Privacy Policy)
Facebook LoginYou can use this to streamline signing up for, or signing in to your Hubpages account. No data is shared with Facebook unless you engage with this feature. (Privacy Policy)
MavenThis supports the Maven widget and search functionality. (Privacy Policy)
Marketing
Google AdSenseThis is an ad network. (Privacy Policy)
Google DoubleClickGoogle provides ad serving technology and runs an ad network. (Privacy Policy)
Index ExchangeThis is an ad network. (Privacy Policy)
SovrnThis is an ad network. (Privacy Policy)
Facebook AdsThis is an ad network. (Privacy Policy)
Amazon Unified Ad MarketplaceThis is an ad network. (Privacy Policy)
AppNexusThis is an ad network. (Privacy Policy)
OpenxThis is an ad network. (Privacy Policy)
Rubicon ProjectThis is an ad network. (Privacy Policy)
TripleLiftThis is an ad network. (Privacy Policy)
Say MediaWe partner with Say Media to deliver ad campaigns on our sites. (Privacy Policy)
Remarketing PixelsWe may use remarketing pixels from advertising networks such as Google AdWords, Bing Ads, and Facebook in order to advertise the HubPages Service to people that have visited our sites.
Conversion Tracking PixelsWe may use conversion tracking pixels from advertising networks such as Google AdWords, Bing Ads, and Facebook in order to identify when an advertisement has successfully resulted in the desired action, such as signing up for the HubPages Service or publishing an article on the HubPages Service.
Statistics
Author Google AnalyticsThis is used to provide traffic data and reports to the authors of articles on the HubPages Service. (Privacy Policy)
ComscoreComScore is a media measurement and analytics company providing marketing data and analytics to enterprises, media and advertising agencies, and publishers. Non-consent will result in ComScore only processing obfuscated personal data. (Privacy Policy)
Amazon Tracking PixelSome articles display amazon products as part of the Amazon Affiliate program, this pixel provides traffic statistics for those products (Privacy Policy)