I'm going through this C# intro tutorial by MS, and in the section "Fix the "format" error" I'm supposed to delete code in the namespace, but I'm not seeing that namespace since I'm in the "top-level statements" mode. How do I reveal the boilerplate code that is underneath?
As described by Top-level statements - programs without Main methods:
Starting in C# 9, you don't have to explicitly include a Main method in a console application project. Instead, you can use the top-level statements feature to minimize the code you have to write. In this case, the compiler generates a class and Main method entry point for the application.
Here's a Program.cs file that is a complete C# program in C# 10:
Console.WriteLine("Hello World!");
Top-level statements let you write simple programs for small utilities such as Azure Functions and GitHub Actions. They also make it simpler for new C# programmers to get started learning and writing code.
This means that the Top-Level statements is a feature of a comiler, not of the editor. The Main method and the class (things you call "boilerplate") are not present in the source code - they are generated by the compiler.
Adding to the above answer is its Compiler feature.
The compiler generates a method to serve as the program entry point
for a project with top-level statements. The name of this method isn't
actually Main, it's an implementation detail that your code can't
reference directly. The signature of the method depends on whether the
top-level statements contain the await keyword or the return
statement.
The following table shows what the method signature would look like, using the method name Main in the table for convenience.
Reference:
https://learn.microsoft.com/en-us/dotnet/csharp/fundamentals/program-structure/top-level-statements#implicit-entry-point-method
Whoever updated the tutorial to .NET 6 with top-level statements (see the commit) forgot to update the part in question. To see the full previous version of the code you can switch tutorial to VS 2019 version either by using dropdown at the top of table of contents:
Or just by following this link.
The previous version also should work, you should be able to replace the contents of your Program.cs file with code from this section and proceed with tutorial from here.
Related
I am new to the C# world but have seen other programming languages give access to command-line arguments from places like main function/method, sys.argv, etc.
It was unusual to see the following statement placed globally in the boilerplate ASP.NET Core Web App MVC (Version 6.0) Program.cs file of Visual Studio:
var builder = WebApplication.CreateBuilder(args);
Hovering over that parameter inside my IDE does seem to suggest it is indeed string[] args, but where is the entry point?
I hope someone brings in further interesting details, but as a minimum, a bit of searching yielded the following result (short answer — Top-level Statements) from Microsoft Docs:
Starting in C# 9, you can omit the Main method, and write C# statements as if they were in the Main method [...] For information about how to write application code with an implicit entry point method, see Top-level statements.
The page covering top-level statements makes it more clear:
args
Top-level statements can reference the args variable to access any command-line arguments that were entered. The args variable is never null but its Length is zero if no command-line arguments were provided.
Is this possible in C# to ensure that method/class have given signature.
For example I want to ensure that some method is public and static.
When isn't I want to this method red underlined.
I need it because I'm using this with component test runner app which uses reflection and expects public static bool methods from dlls. I want to force programmers to write public static bools component test methods. Is this a possibility to force them compile time? Or maybe force them build time by adding another simple app that checks it by reflection during post build event?
Is this possible? Maybe by method attributes? By reflection? But how?
Unfortunately, you cannot change/extend the C# compiler to achieve what you want here. However, there is a Microsoft project called Roslyn which exposes a public API for implementing your own extensions to the C# compiling pipeline.
Using Roslyn's structures, it should be easy to traverse your source code syntax trees looking for methods that are not public/static. Once you find them, you could generate a code issue reporting the problem; those issues are shown both in the code editor (wavy underline) and in the Errors List panel. From Roslyn's official documentation:
The code issue provider makes it easy to surface an error or suggestion to the user as a wavy underline in the editor or appear in the Error List window.
Look around for examples of CodeIssueProviders; it could be useful for what you need to do.
I have a question regarding the C# code generation capabilities of Enterprise Architect.
How is it possible to add "using" statements?
Say if we want to have System.Thread as a namespace in our file.
Where do we specify this in the model?
I already tried to backward engineer the code with an inserted "using System.Thread", but could not detect this information anywhere in the model.
You don't add the "using" statements explicitly, they are inserted implicitly by the code generation facility where needed (the same applies to "imports" in Java). EA works it out by determining whether the referred class is in the same package.
If you want the exact details on how it does so, the code generation scripts are actually available for reading and editing (not recommended!) from Settings - Code Generation Templates. Select the language in the upper left corner, then start reading from the "File" script, which is the one that calls all the others. This is not an errand for the faint-hearted, but it's there if you want it.
I'm not sure whether "using" statements are generated correctly for classes which you'd normally consider part of standard libraries, such as System.Thread. You may need to add a System package with a Thread class in it (in a separate namespace) to get that to work properly.
With python, I can use if __name__ == "__main__": for using the module both as a library and a program.
Can I mimic this feature in C#?
I see a class in C# can have a 'static void Main()', but I'm not sure if every class can have a Main() without a problem.
ADDED
/m:CLASS_NAME is a way to specify the class to run the Main().
You can put a Main method in as many classes as you like, although only one can be an entry point for an application. (For talks, I often have a main method in every class, and use a helper library to present all of those pseudo-entry-points when I run the project.)
Likewise you can definitely add a reference to a .exe assembly and treat it like a library. For example, you could make a unit testing assembly work like a class library in most cases, but also write a main method so that you could just run it to execute the tests without a GUI or whatever.
You can compile a C# project as a program (executable) with a Main() method, and you'd still be able to use it as a library. No special syntax required.
You could add a Main() method to every class, but I doubt it's useful.
.NET applications usually have different structures than Python ones; trying to fit the same programming model is unlikely to get you good results.
C# project files specify a startup object when multiple entry points are available.
See this article for more info.
I am using Resharper for refactoring. What is this type of feature called when you have the same code in around 5 different events being called but you want to replace them with a method call? And place that code in that method.
All done automatically.
Thanks
I've been working on a Resharper plugin that does what you are asking. That is, it scans your code, searching for sections that can be replaced by an existing method call. A section can be a whole method or just a part of a method. When it finds one, the lightbulb pops up and offers to replace said section with a call to the existing method.
(source: landofjosh.com)
I call it AgentRalph. At this point it's not ready for production use, but I've been making a lot of progress and hope to make a release soon.
Extract Method.
See our C# CloneDR. While it doesn't replace redundant code with function calls, it does tell you where they are across very large system, and forms the essential abstraction (procedure body and parameters). The web link has example clone analyses for the C# equivalent of Hibernate (NHibernate).