-1

I've written a lot of Python (and a bit of C/C++ many years ago) but am now staring on C# (.NET).

I'm using VSCode as my IDE.

I have the following folder structure

test/
├─ Program.cs
├─ Car.cs

with

//Program.cs

class Progam
{
    static void Main(string[] args)
    {

        Console.Write("Hello SO!");
    }

}

and that runs fine when I press CTRL+F5 (Run without debugging).

I've then moved into classes in C#, which is the Car.cs file.

I have an error (I forgot a semicolon)

//Car.cs

class Car
{
    string color = "red";
    int n_wheels = 4 // Wups, missing semicolon
}

Note, I haven't changed the Program.cs file, but now when I press CTRL+F5 I get the error

C:\Users\Me\Documents\C#\test\Car.cs(8,21): error CS1002: ; expected [C:\Users\Me\Documents\C#\test\Test.csproj]

I might still quite have understood how .NET/C# compiles the stuff and runs the program, but I just wonder, why it throws an error in the Car.cs when that class isn't used at all in the Program.cs, which is the only file that contains a Main function.

I could understand if I've imported the Car file but I do not - I don't use it anywhere and it does not have a Main function.

So my question is; how do I avoid such stuff, when I have files that I'm currently developing (but am not using) that might have some bugs/issues due to it being under construction?

CutePoison
  • 4,679
  • 5
  • 28
  • 63
  • 1
    It requires all code to compile. It doesn't mean it will run all code (until you call it). How to avoid it? Add the semicolon :) – user2740650 Oct 03 '22 at 04:15
  • Thats just the way it works. Why would you want to run code that wont compile? Also Car.cs is part of the same component... I would argue if you have a component that is not referenced then maybe there is a way.... But it would have to be in a different component. – Jonathan Alfaro Oct 03 '22 at 04:21
  • @JonathanAlfaro that is exactly my point; I don't use the `Car.cs` file at all i.e why does it bother if it complies or not, since it is not used. – CutePoison Oct 03 '22 at 04:25
  • 1
    If it worked the way you're proposing one might then wonder "Why didn't it notify me of the missing semicolon in `Car.cs` before now?" when `Car` does finally get used. Further, what if you _are_ using `Car` but via reflection (`Activator.CreateInstance(null, "MyNamespace.Car")`)? Consider the depth of analysis the compiler would have to do to determine if a type is truly being used or not, and it still wouldn't be 100% accurate. It's much easier to just have the compiler...compile all code fed to it. If you don't want the compiler to check a file, then take steps to prevent its compilation. – Lance U. Matthews Oct 03 '22 at 05:05
  • Does this answer your question? [Why does the c# compiler still read compile code from unused methods?](https://stackoverflow.com/questions/11652109/why-does-the-c-sharp-compiler-still-read-compile-code-from-unused-methods) or [Will the compiler only compile code that can get executed?](https://stackoverflow.com/q/10192076/150605) or [C# Compiler optimization - Unused methods](https://stackoverflow.com/q/5204667/150605) or [Why can't dead code detection be fully solved by a compiler?](https://stackoverflow.com/q/33266420/150605) – Lance U. Matthews Oct 03 '22 at 05:16
  • I can think of many reasons... But one reason is that in C# just because you don't reference a class or type it does not mean you are not going to use it. In C# is perfectly valid to instantiate a type during runtime using reflection... you can even load entire components(libraries) dynamically. – Jonathan Alfaro Oct 04 '22 at 08:40

1 Answers1

1

Following up on the comments, and after viewing the OP's bio I can see where this question is coming from.

You are comparing python's execution path to C#'s.

In python (yes, I know python is compiled - I'm simplifying):
The interpreter is going over the commands in a sequential manner and executing them.

i.e. If you have an import within a function - the interpreter will not know or care about the import until it is required, and thus code with bad syntax in another module (= file, for C# speakers) will not affect your program.

C# works a bit differently. The first step is compiling the code and creating a binary file which will be executed.
That file is the binary representation of all the source code files referenced in your project. Hence:

  1. Any file that has syntax error will fail the project compilation
  2. The binary will not build.
  3. You will not be able to execute the binary.

This is why a syntax error in any file will not allow you to execute your code.

Another way to think about is is that in python the imports are done in run time, while in C# its in compile time.
In both cases a malformed reference file will break the process.
C# simply does it earlier.

So my question is; how do I avoid such stuff

Well, you can fix the syntax error, or comment that part out until you get to it...

Avi Turner
  • 10,234
  • 7
  • 48
  • 75