What is a namespace in C# and why is it important
Hello guys, I'm confused, what is a namespace in C# and why do we need it please, where can we omit it, is it correct if we omit it? If not, why please.
71 Replies
When we say something like
System.Console
Console is a class, System is a "namespace"; what does that meanNamespaces are like folders on your computer
It's like a "package" ? Like a "package" can contain multiple classeS? or it's the parent folder wrapping these classes?
"package" has a slightly different meaning
one moment
ok
So,
You can think of classes as files. You could just place all of your files on your computer on your desktop, but of course you don't since that would be a mess. Instead, you organize them into folders.
Namespaces are the same way, you don't put all your classes onto your "desktop" (something called the global namespace in this case), you put them into namespaces for organization.
So
Console
is a class inside System
, which is a namespace which contains a lot of fundamental C# things, like the console and things like strings and numbersthe namespace is a specific folder we need to create when writing code? or it just need to be there if for example we have different scripts related to each other, then each script will have same namespace ?
Generally when you put class in a folder, the namespace also specifies that folder
Generally yes, but technically you don't have to
If you have
<projectRoot>/Database/Models
, then the namespace would be <projectName>.Database.Models
It's not enforced unless you explicitly ask it to do that
In the end namespaces exist to organize. It keeps it simple what a file needs, and also removes the need to use overly complex names for classes to avoid writing the same name twice by accidentprojectName would refer to the folder which contains the class files?
No, just the name of your project
Generally your namespaces should at least mention that
ah
But this, like all things, can be changed
yeah I see, it's just a label ?
The namespace?
yep, a label for file organization ?
Pretty much, and in that can be more namespaces and/or your classes and all that
And when you want to use it somewhere, you would specify it with a using statement
For example, given the previous example, you would write
using <projectName>.Database.Models;
at the top of the fileyep I see
Then that file can use the classes and things in that namespace
A namespace is completely optional, but you should really always use it
alright noted, I have just begin to learn C#, in the .NET 9.0, I think, we don't explicitly set the namespace or class name; but later on we would need to do so ?
BTW you can also do this, where if you want to use the
Console
class in the system namespace. Instead of putting using System;
at the top of your file, you can also just write System.Console.WriteLine("Test");
in your code
It's generally not done, but you can do this when you happen to have two Console classes
But there are better ways to fix thisYou should always set an explicit namespace using
namespace My.Namespace.Here;
syntaxI would guess you use the minimal Program.cs file thingy?
I don't remember the actual name
Those don't have namespaces
yeah
the only exception to this rule is top level statements
Yeah this would be the only instance where you don't have it
yep, the only
what do we mean by top level statements, basic statements?
It's all "syntactic sugar" in this case
like Console.Write("hello"); ?
Well, the
Program.cs
file used to be way more complex
The way you write it now is a fairly new, way simpler way of doing it
But you actually end up witht he same thing, the compiler just does it for youC# 9.0 - Introduction To Top-Level Statements
In this article, you will learn about C# 9.0 - introduction to Top-Level Statements.
yep I see, later on though where I would need to create my own classes etc, I will have to use namespace etc ?
yes
The reason this was done is because everybody used the same pattern anyway
You don't have to, but you should definitely do it
https://sharplab.io/#v2:CYLg1APgAgTAjAWAFDKnAnACgEQBcCmAzrtgJQDcQA== <-- example of what top level statements compile into
ok ok noted, I should use namespace everywhere, then ? It's better, right ? even though compiler can simplify things for us, like the top level statements?
Top level statements is the only instance I can think off there explicitly leaving them out is better
Note the Program.cs file should be the entry point of your application. Nothing else should use methods or classes from it anyway
So putting it under a namespace has no benefit
yep I see, I have a clearer understandings, thanks guys, really appreciate, will come back if I have other questions
Feel free to ask more when you need to know something 👍
(you can't put top-levels statements into a namespace)
we can't use top level statements in several file though ?
correct
similar to how we can only have one main entry point per project?
TLS is only allowed in one file, because that file becomes your entry point
its the exact same thing
you cant have TLS + explicit main in another file
ah
I see
but hmm
can we have multiple main entry point if we don't have TLS? like if I have 2 classes, Person and Student, can I declare a main entry point in each of them ?
no
oh ok
how would that work? which one would start when you ran the program?
hmm no I mean if there are in different files?
Like 1 file containing class Person and the main method and another file containing class Student and its main method
how is that relevant?
when you start a dotnet program, you dont specify the class to run
you only specify the project
ahhhh
true
Well, you can, but you should not
when we write in the command line "dotnet run" , we are running the whole project, kind of ?
You can specify the startup object in a project, but that is generally not something you need to do anyway unless your main entrypoint is not in a place where it can be found
ok ok, so to recap, I should have one main entry point per project ?
Only the the projects that actually need to be run, as they turn into an executable
You can have other project types that don't have an entry point, like class libraries
These exist to provide code and features, but don't exist to be run individually
yep I see
Once you compile you always get an executable of the projects that have a main entrypoint. Additionally these would be configured automatically to indicate they are supposed to be run in general
Any other projects like class libraries are added to the output as
.dll
filesmost solutions (a collection of related projects) will have only one runnable project. If you have more than one, your compilaton results in more than one executable
Should you care, you can always change the type of a project in its properties
Should you make a mistake, or convert the project for example
Console
/Windows
applications are types that would generate an executable
But this drop down can have different options depending on what you're doingyep I see, makes more sense now, thanks guys, I have understood the concept of how all this works but still need to write a bit of code to see how it works, thanks !!!
Consider these guys, I try to use namespace instead of TLS. Notice my IDE is giving me the following: "Namespace does not correspond to file location". This mean, in every files I want to create, namespace should be named similar to my root folder ?
This is what I meant with that your editor can tell you to use the same format for your namespaces as the folders you use
In this case,
<projectName>
is Learning, so your namespace should be Learning
You can choose to ignore this warning, or turn it off. In the end the only real benefit is readability and consistency
Apparently Rider always points this out. In Visual Studio this is a separate analyzer option you have to enableyep I see, thanks !
By the way, every file I created in this project, that is in the Learning folder, should consist of the namespace? if we forget about the TLS
Of a namespace, yes
The namespace is usually the same as your folder structure as mentioned, but that is an optional pattern
By default, when you create a new class file, .NET will set the namespace based on the folder structure
The thing is the namespace I decide to use, whether it's learning or something else, every .cs file should have the same namespace for this particular project ?
hmm like when I create a new .cs file ?
wait will just try that out in rider
ahhh
true
Yep
when I create another file, the namespace of my entry point got appended at the top
Thanks !!