Question

I have some C# files that were designed by people who do not work with me anymore.

Not all of the C# files contain a Main method, though they build and execute.

Why is this the case?

Aside, the code has very few comments. I need to read the code to work out what it's doing. How can I do this more efficiently?

Come updates.

I used breakpoints to follow the control flow. But, I found that the flow went through some methods that do not have any meaningful definitions in some classes. For example,

 class my Class
 {
     protected void function1()
     {

     }
     protected void function12()
     {

     }

 }

function1() is called and then the program exit from function12().

I cannot find the definations of these functions in VS.

Any help would be appreciated.

Thanks

Was it helpful?

Solution

From http://msdn.microsoft.com/en-us/library/acy3edy3.aspx :

The Main method is the entry point of a C# console application or windows application. (Libraries and services do not require a Main method as an entry point.). When the application is started, the Main method is the first method that is invoked.

It could be that not every C# file you have is the entry point for a console application or windows application. The files might be a part of a console/windows application, but not the entry point. The files could also belong to completely different types of projects.

As for smart/efficient ways of working out what code does, without comments, you're pretty much resigned to reading the code, unless you can trust that things like method names accurately describe the body of the method. Even then, how can you be sure without reading it first?

Edit: As JBeck commented, you could always run your code in Visual Studio, breakpointing the parts that you're not understanding can help.

OTHER TIPS

The entrypoint can be anything.. as long as it is static (it can even be private).

If you are really having trouble finding out.. your other option (other than looking at the project properties) is to decompile the assemblies (using ILDasm or a viewer like ILSpy, DotPeek, etc) and look for the .entrypoint opcode in the IL.

The Main method is for a console application, so it is normal that you will not see this in every project, in fact you will probably rarely see it in your projects. Most projects you will run across will either be Windows (WinForms or WPF) or web (WebForms, ASP.NET MVC, etc.).

Windows applications have a start form, which is a setting in the project itself. The web applications will have a start page, which again is a setting in the project and/or web server.

The most important thing to master is the Visual Studio debugger. Know how to set break points, add variables to the watch window, read the stack trace, use the immediate window to enter object names and interrogate the state of objects in the system as the program is running.

Whenever I get a new piece of code to maintain, improve or expand with new features, the first thing I do is to draw on a whiteboard the forms or pages of the system and then decide what are the possible entry points for each of these forms/pages. Once you have this drawing, then you can decide where you want to put break points for code that you are not sure what order it is called in or even what it does.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top