C
C#2w ago
Faker

✅ namespace and using keyword in C#

Hello guys, I have a quick question. I don't understand, when do we use the using keyword and the namespace keyword... can they be used interchangeably ? (don't remember if I once saw a namespace along with a library name... is that even allowed?) . Now, the namespace, sometimes I saw something like that: namespace {...}like we have the curly braces but sometimes we don't. My IDE always do the heavy lifting but I wanted to understand what the syntax is :c. Do we use the keyword using when we need to use certain libraries? While the keyword namespace is used to related to certain folders/cs files? I'm a bit confused here :c... what about the namespace {...} syntax please
C#
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.Logging;
using Microsoft.EntityFrameworkCore.Diagnostics;

namespace MainProject.Context;
C#
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.Logging;
using Microsoft.EntityFrameworkCore.Diagnostics;

namespace MainProject.Context;
6 Replies
reflectronic
reflectronic2w ago
the namespace keyword defines the namespace for the classes that you write in that file the using keyword allows you to access the classes defined in another namespace
Faker
FakerOP2w ago
oh ok, so is it correct to say that namespace is the thing that allows us to organize our project. Any new cs file created should have a namespace, so that later on we can refer to the classes/methods found in those files using the using keyword? So, using keyword is to use classes/methods from other cs file namespace keyword is to organize our cs file so that we can refer to them later on? what about the syntax namespace {...} please
reflectronic
reflectronic2w ago
yes the namespace {...} does the same thing the namespace ...; is just a short-cut for doing namespace { the rest of the file }
Faker
FakerOP2w ago
ah ok I see, thanks ! using namespace {}, we can have several namespace within 1 cs file ?
reflectronic
reflectronic2w ago
yes
Faker
FakerOP2w ago
yep I see, thanks !

Did you find this page helpful?