T
Tim Roberts
I need a patient explanation.
As background, I've been programming for about 35 years. I consider myself
a C++ guru and a Python expert, and I'm just now ramping up on C#. There
is something that confuses me. Well, there are many things that confuse
me, but more on that later.
In C++, when one source file needs to refer to things compiled in another
source file, we have to use prototypes and declarations to let the compiler
know about it. In Python, if I need to refer to a class in another module,
I have to import the module. In virtually every language I've ever used, I
have to take some kind of explicit action to tell the compiler where to
find external modules.
In C#, there is apparently some kind of magic going on. As an example,
let's say I have code in one.cs that wants to instantiate an object of
class Two, something like:
Two myObject = new Two();
myObject.GoDoSomething();
OK, now let's say that class Two is defined in two.cs.
If I do "csc one.cs", I'll get an error, because Two is not known. But if
I say "csc one.cs two.cs", then everybody is happy. Why does that work?
There's nothing in one.cs that tells the compiler where to find "Two". The
compiler can't finish compiling "one.cs" until it sees "two.cs"; it won't
even know that Two has a method called GoDoSomething until it finishes with
"two.cs".
How does the compiler handle this? Does it just concatenate all of the
source modules into one big compilation unit, and scan for class
definitions before compiling the code?
As background, I've been programming for about 35 years. I consider myself
a C++ guru and a Python expert, and I'm just now ramping up on C#. There
is something that confuses me. Well, there are many things that confuse
me, but more on that later.
In C++, when one source file needs to refer to things compiled in another
source file, we have to use prototypes and declarations to let the compiler
know about it. In Python, if I need to refer to a class in another module,
I have to import the module. In virtually every language I've ever used, I
have to take some kind of explicit action to tell the compiler where to
find external modules.
In C#, there is apparently some kind of magic going on. As an example,
let's say I have code in one.cs that wants to instantiate an object of
class Two, something like:
Two myObject = new Two();
myObject.GoDoSomething();
OK, now let's say that class Two is defined in two.cs.
If I do "csc one.cs", I'll get an error, because Two is not known. But if
I say "csc one.cs two.cs", then everybody is happy. Why does that work?
There's nothing in one.cs that tells the compiler where to find "Two". The
compiler can't finish compiling "one.cs" until it sees "two.cs"; it won't
even know that Two has a method called GoDoSomething until it finishes with
"two.cs".
How does the compiler handle this? Does it just concatenate all of the
source modules into one big compilation unit, and scan for class
definitions before compiling the code?