in ,

Study C# Half 2 – Namespaces

Final Replace: Dec 31, 2022

This tutorial is a part of a collection. You possibly can see the opposite tutorials under:

Within the second a part of our studying C# collection we’re going to clarify namespaces. You bought a have a look at a namespace once we ran the pattern code within the final chapter, and now we’re going to dig in a bit of deeper into C# namespaces and what they imply.

What’s a Namespace?

The .NET Framework class library is big, over 10,000 courses in actual fact. Namespaces carry order to all these courses by creating containers for entities resembling courses or sorts.

In our Good day World utility it regarded like this:

However we additionally had one other namespace in use that we recognized above that:

These two traces let the compiler know what namespaces we’re working in. We carried out the utilizing assertion for System to let our program have entry to entities throughout the “System” namespace, which is part of the .NET Framework. The namespace “HelloWorldApp” is our namespace that we created for our utility.

The first operate of namepsaces is organizing your code. With our Good day World utility they didn’t do a lot as a result of it’s very minimal, however in an actual utility they allow you to out immensely.

What do they do?

A namespace organizes your code by creating boundaries and hierarchies to your sorts. They do a number of issues:

Establish the aim of the kind – For instance, System.IO controls enter and output, System.Textual content accommodates courses that take care of character encodings.

Creates a hierarchy of sorts – As an example, System.Internet handles communication at the next degree, whereas System.Internet.Sockets is extra granular and focuses on sockets. Chances are you’ll solely want what’s contained inside sockets, or it’s possible you’ll want a number of elements of the System.Internet namespace.

Allow you to navigate the Library – As acknowledged earlier than there are over 10,000 courses within the .NET Framework library and also you don’t want all of them. Once you’re looking out you don’t need to search all of them searching for a specific methodology. As within the instance above, you recognize sockets are used for communication, so you may browse to System.Internet and search for System.Internet.Sockets after which discover the strategy or sort you’re searching for. Autocomplete in Visible Studio helps with this additionally.

Namespaces guarantee uniqueness – Namespaces can decide how a way is used. For instance the “scan” methodology means one thing totally different for a networking library than it might for an OCR library. Within the regex class there’s a “seize” however should you’re writing one thing for a cell phone it may imply taking an image.

Namespaces may also make your code cleaner – By implementing “utilizing” directives you may embrace a namespace and reference all of the strategies in it inside it’s scope.

For instance as an alternative of writing System.Console.WriteLine(“textual content”) you may name it out implicitly, resembling Console.WriteLine(“textual content”). This will not look like a lot of a distinction, however when working with massive tasks issues like this matter.

How Namespaces are Named

The first operate of namepsaces is organizing your code. With our Good day World utility they didn’t do a lot as a result of it’s very minimal, however in an actual utility they allow you to out immensely. The identify of your namespace displays who created it and what it’s function is.

System.[name] – These are entities which can be a part of the .NET Framework Class Library.


  • System.IO
  • System.Textual content
  • System.Console

You possibly can even put your personal sorts into the System namespace, however I wouldn’t suggest it.

Microsoft.[name] – These are normally libraries with performance created by Microsoft, however not included within the .NET Framework libraries.


  • Microsoft.CSharp
  • Microsoft.Runtime.Internet hosting
  • Microsoft.SqlServer

[CompanyName].[name] – Corporations usually put their very own identify in as the primary a part of the namespace to point one thing is using their very own instruments.


  • Telerik.Home windows.Controls
  • CookComputing.XmlRpc

[ProjectName].[name] – Open supply tasks usually use the identical naming conventions for his or her namespaces as nicely.


  • Codemonkey.TextParser
  • Thor.Internet.Views

As you may see within the examples a hierarchy is created by the namespace and we attempt to replicate that within the naming. You possibly can identify your namespaces nearly any manner you’d like however it’s extremely advisable to comply with the final conference:


This provides readability to your answer and helps shield you from naming conflicts. The best way you determine your namespaces will make your life simpler down the highway. We’ll discover even additional within the subsequent part.

Extra Naming Concerns

Stopping conflicts is a full time job for any programming language or framework and C# .NET is not any exception. Listed below are some tricks to comply with when naming your namespaces.

Use your organization identify within the namespace – This helps keep away from conflicts in case your entities are named just like one other firm’s software program.

Don’t put model numbers within the names – Altering namespaces introduces a bunch of issues, and model numbers change ceaselessly.

Don’t identify it after the inner codename of your organization challenge – These names have a tendency to vary over time.

Don’t use key phrases or sorts as names for namespaces – this appears fairly apparent, however can result in some nasty conflicts down the highway.

Express References and Namespace Aliases

So what if in case you have two strategies with the identical identify in two totally different namespaces? Nicely, it’s not the top of the world. You simply must name them out explicitly:


Xmltools.Reader.Parsetags("someparameter", "anotherone");

As you may see, you’ve gotten can two totally different strategies, even with differing kinds and signatures, and also you’ll be simply positive so long as you name them out explicitly. This needs to be simple for many programmers to determine in the event that they’re studying your code.

Another choice for equally named sorts can be to make use of an alias. That is one other solution to differentiate equally named sorts. Let’s say you’ve gotten a path class outlined in certainly one of your namespaces, however you additionally want a category that works with filesystem paths. Right here’s each of them known as out explicitly:

utilizing System.IO;
utilizing JeremysTools.IniReader;

// express calls can be:

You possibly can create an alias for each very simply to keep away from confusion:

utilizing IOPath = System.IO.Path;
utilizing IniPath = JeremysTools.IniReader.Path;

See how simple that’s? Now you may simply differentiate between IOPath and IniPath with out doing a bunch of additional typing.

Nested Namespaces

The .NET Framework makes use of namespace nesting lots, with good motive. As an example the System namespace is big, and accommodates sorts resembling System.Internet and subcategories resembling System.Internet.Socket and System.Internet.Mail. In case you declare the socket namespace, you may’t neccessarily use the Mail namespace sorts, although they’re each within the System.Internet namespace. This creates the hierarchy we talked about earlier. That is carried out for effieciency, as we don’t need to load your entire framework for each utility.

You need to use nested namespaces too in case your utility wants it. It’s a quite simple methodology:

namespace YourApp {

	namespace NetTools {
		// do stuff

It is a easy solution to create a hierarchy and higher arrange your code. One other much more succinct methodology seems to be like this:

namespace YourApp.Internet
	// do stuff

That is the most typical methodology. Any code you place in a nested namespace can use sorts from the mum or dad namespace with out calling it explicitly.


I hope this tutorial was a superb introduction to namespaces. Truthfully I didn’t assume it might be this lengthy till I began writing it and actually enthusiastic about it. This isn’t actually code heavy, however the subsequent half shall be after I begin speaking about sorts.

Most tutorials go straight from Good day world to sorts, however I believed the namespace can be an necessary first step since all of your code is contained in them. However software program is about knowledge, so the following half shall be about sorts, variables and courses then we’ll begin on some conditional operation. I’ll additionally begin to dive into some extra object oriented stuff so that you begin off doing issues in an object oriented method.

I hope these things helps, go away me some feedback or shoot me a message if in case you have any questions or feedback.

Are you able to beat my SkillIQ Rating in C#??

C# Skill IQ

I believe you may! Take the C# take a look at right here to see the place you stand!

Supply hyperlink

What do you think?

Written by admin

Laisser un commentaire

Votre adresse e-mail ne sera pas publiée. Les champs obligatoires sont indiqués avec *

GIPHY App Key not set. Please check settings

3 Books I Completed Lately

What’s E-A-T in search engine optimisation? 5 Issues You Can Do to Enhance E-A-T