✅ using keyword as a directive
Hello guys, I'm a bit confused of how the
using
keyword is used with a namespace. Say I created a new project. At the root of that project we have a solution and inside that solution we have our project folder. Now, the namespace refers to the individual project folder we have?
Consider the picture for example. The namespace is the same for every .cs file in a particular project? Why can't we do using LibraryManagement.BankAccount
? What I'm trying to say is, look inside the LibraryManagementFolder and import the BankAccount class but this is incorrect to interpret it like that?
Second thing, consider the following code:
When we don't use top-level statement, the namespace should explicitly been set? What the difference between a namespace
and the using
keyword directive here please
6 Replies
BankAccount
isn't a namespace, its a class.
using
for namespaces just "imports" that namespace so you can access it without fully qualifing
ie, without using LibraryManagement
you could still use BankAccount
, but you'd have to write LibraryManagement.BankAccount account = new LibraryManagement.BankAccount();
etc
files and directories have no direct connection to namespaces, its just convention to align them most of the time, but there are valid exceptionsone thing I don't understand, why even though BankAccount is in the same namespace, we need to use the line
using LibraryManagement
Like our IDE doesn't already know we are in the LibraryManagement project/namespace?TLS files don't have a namespace
they are namespace-less
ahh
I thought behind the scenes we were already saying "using ...."
nope.
yeep I see, it's clearer now, thanks !