✅ how does compiler connect several c# codes into 1?
I've noticed that classes that i created in other file are accessible in the main class (all in 1 project of course), why???
29 Replies
because they are all part of the same project
Thats just how C# works. It's not file-based, it's project based.
where can i get more info about it??
soo, as i understand, project is the real one what is getting compiled, runned, and all the files are just several parts of that project?
ohhh
Sounds about right
everytime i create new class in the same project it doesnt create anything new, s e p e r a t e d, it just looks like its other file, while in reality its all 1 big file (project) that was separated in order for code to be easier to maintain?
Well, it is different files on your file system
its just that the compiler will gather them all up when compiling the project
like its 1 big file
if you prefer to think of it that way, sure
okie
thanks!!
and by the way, everytime when i add new file by RC project -> clicking add, it always creates class with namespace
Yeah.
You should rarely if ever not have namespaced code.
You can read more at https://learn.microsoft.com/en-us/dotnet/csharp/fundamentals/types/namespaces
Organizing types in namespaces
Learn how namespaces help you organize related types.
in theory, if i get rid of that namespace, than i can use all the class from that file in other files?
don't get rid of namespace. you can just add a
using
for the namespace insteadthanks, i read it, just wanna make sure, devil is in the details 😛
yeah, some experimenting in order to be crystal clear
For example, its standard for a namespace to begin with the project name
so if your project is
MyLearningProject
, your program.cs
with the Main method would likely be...
if you create a folder/directory inside a project and then create a class inside that folder/directory, it will be namespaced with the foldername
okie, thanks
thats why i was able to use "MessageBox.Show("show")" when i created seperate class in the WinForms Project
is that because that are in the same project -> classes are accessible for each other (file)?
and also, can i somehow make class only accessible in its own file?
Access Modifiers - C# Programming Guide
All types and type members in C# have an accessibility level which controls whether they can be used from other code. Review this list of access modifiers.
public
, internal
, private
etc
public means "anyone can access this"
internal means "anyone in the same project can access this"
private means "only I can access this"
private classes are pretty rare, unless they are nested or similarprivate class sounds dumb
i just havent found the one which states "only this file can access it"
stop thinking about files
or ig there is no such thing as "file"
files dont matter
its just deviding code into several pieces
Sure, separate logical units
thanks man, you helped me
if you truly want "this file only", which you shouldn't unless you are writing a source generator, look at https://learn.microsoft.com/en-us/dotnet/csharp/language-reference/keywords/file
file keyword - C# Reference
file modifier: Declare types whose scope is the file in which it's declared
looks like i dont 😛 , thank you one more time, have a good day
👋