简体   繁体   中英

c# syntax checking

I create a project, and add a file. I can build an exe and so on, works fine.

Question is: what if I have multiple files in my project and I want to check the syntax of my code in one single file, and don't want to build an exe or dll?

When programming c, I press ctrl+F7 in the Visual Studio IDE and that compiles my code. I understand that there are no object files in c#. Microsoft c# reference states: "There are no object (.obj) files created as a result of invoking the C# compiler; output files are created directly.".

Is there a csc.exe switch to tell me 'ok your code looks fine', or 'the expression at line xx doesn't make sense'?

Note: please do not give the link to Command-line Building With csc.exe. All the sample command lines are for creating an exe or dll, from one or multiple files. Or on how to exclude a file from building. I don't want that.

The compiler is the syntax checker. It will tell you if your C# code is valid or not when it compiles it. Any errors or warnings will be available at the end of the attempted compilation. If there are none, then it's valid code.

You can individually compile any arbitrary piece of C# code any time you like. However , if that code depends on other code then in order for it to be valid that other code will need to be included in the compilation as well. Code which uses otherwise undefined symbols (such as classes defined in other code files) isn't valid. Those symbols need to be defined.

In the comments above you indicate a concern that such compilation of an entire project might take a long time. This would be addressed by organizing your code into smaller components. If you have one enormous C# project that takes a long time to compile, then what you have is a mess. Break it apart into smaller components. Each of those components can be in their own projects which can be compiled separately. Different projects will depend on each other, and dependent projects will be included in that compilation. But that dependency graph also shouldn't be too large and unwieldy. If it is, you still have a mess but just on a different scale. Keep the dependency graph shallow and maintainable between your projects.

Sort of answering my own question. When I posted this I wasn't aware that splitting definition and implementation in c#, as you did in c or c++, was not possible. https://social.msdn.microsoft.com/Forums/en-US/64800602-4ff2-4747-8c42-a4e1980d4124/c-no-header-files-all-code-is-written-inline-why?forum=csharplanguage states that Interfaces do this, but not in a way I asked in the original post. Looks like in a multi file project, only the IDE gives me this piece of information (syntax checking), other than building the whole project.

It appears that ctrl+F7 on a single file (Visual Studio), or -c or -fsyntax-only switches (on gcc) are not possible with c#.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM