C
C#2y ago
wcasa

✅ 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
RubyNovaDev
RubyNovaDev2y ago
because they are all part of the same project
Pobiega
Pobiega2y ago
Thats just how C# works. It's not file-based, it's project based.
wcasa
wcasa2y ago
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
Pobiega
Pobiega2y ago
Sounds about right
wcasa
wcasa2y ago
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?
Pobiega
Pobiega2y ago
Well, it is different files on your file system its just that the compiler will gather them all up when compiling the project
wcasa
wcasa2y ago
like its 1 big file
Pobiega
Pobiega2y ago
if you prefer to think of it that way, sure
wcasa
wcasa2y ago
okie thanks!!
wcasa
wcasa2y ago
and by the way, everytime when i add new file by RC project -> clicking add, it always creates class with namespace
Pobiega
Pobiega2y ago
Yeah. You should rarely if ever not have namespaced code.
Pobiega
Pobiega2y ago
Organizing types in namespaces
Learn how namespaces help you organize related types.
wcasa
wcasa2y ago
in theory, if i get rid of that namespace, than i can use all the class from that file in other files?
Pobiega
Pobiega2y ago
don't get rid of namespace. you can just add a using for the namespace instead
wcasa
wcasa2y ago
thanks, i read it, just wanna make sure, devil is in the details 😛 yeah, some experimenting in order to be crystal clear
Pobiega
Pobiega2y ago
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...
namespace MyLearningProject;

public class Program
{
public void Main(string[] args)
{
...
namespace MyLearningProject;

public class Program
{
public void Main(string[] args)
{
...
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
namespace MyLearningProject.FolderName;

public class Class1
{
...
namespace MyLearningProject.FolderName;

public class Class1
{
...
wcasa
wcasa2y ago
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?
Pobiega
Pobiega2y ago
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.
Pobiega
Pobiega2y ago
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 similar
wcasa
wcasa2y ago
private class sounds dumb i just havent found the one which states "only this file can access it"
Pobiega
Pobiega2y ago
stop thinking about files
wcasa
wcasa2y ago
or ig there is no such thing as "file"
Pobiega
Pobiega2y ago
files dont matter
wcasa
wcasa2y ago
its just deviding code into several pieces
Pobiega
Pobiega2y ago
Sure, separate logical units
wcasa
wcasa2y ago
thanks man, you helped me
Pobiega
Pobiega2y ago
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
wcasa
wcasa2y ago
looks like i dont 😛 , thank you one more time, have a good day
Pobiega
Pobiega2y ago
👋
Want results from more Discord servers?
Add your server
More Posts