Getting started with C #, how to debug a .NET 4.0 MVC3 project?
I'm trying to teach myself C # myself and wondering if anyone can help me with what seems to be a core C # question. I created a C # file with this code, started debugging but can't see "Hello World" anywhere.
using System;
class Hello
{
static void Main() {
Console.WriteLine("hello, world");
}
}
http://msdn.microsoft.com/en-us/library/aa664628 (v = vs .71) .aspx
So I guess my question is this. Where should I expect to see "Hello World"? I checked the Console and Browser. Is there some kind of setup that needs to be done to properly debug C # files. I probably don't see a big picture of how C # works. I am using PHP where I can just do something like this ...
<?php
include 'my file';
echo 'my file included';
?>
Any help would be much appreciated. Thank.
EDIT:
Thanks everyone for the help. You all helped me understand and understand a lot of things about C # /. NET. After thorough troubleshooting, it is obvious that the problem is not the mother of debugging, but the fact that my C # file does not seem to be properly connected / included (not sure what it called in .NET terms) until the rest of the project. Anyway, I accept keyboardP's answer when it answered first and technically gave me all the correct answers. Also thanks to dasblinkenlight which was helpful too.
Additional solution:
After reading SO users. This article helped me in the right direction. http://www.asp.net/mvc/tutorials/getting-started-with-aspnet-mvc3/cs/intro-to-aspnet-mvc-3
source to share
I am assuming that the command prompt window closes immediately. You can avoid this by adding Console.ReadLine();
after your operator WriteLine
. This will wait for you to hit return before closing the prompt window.
Alternatively, assuming you are using Visual Studio, you can run the build without a debugger attached by pressing CTRL+ F5.
Edit . Based on additional information added to using ASP.NET rather than a console application.
First, what are you trying to achieve? If you want to display debug information, you can Debug.WriteLine
instead ofConsole.WriteLine
System.Diagnostics.Debug.WriteLine("Hello World");
This will cause the text to exit to the Exit window at the bottom of Visual Studio (the default).
Edit 2 . Since you just want to write some random text to the page, you can use
HttpContext.Current.Response.Write("Hello World");
There are sometimes problems with Response.Write
, but that should be good for what you want to do here.
source to share
Use breakpoints. Set a breakpoint at the end of your method by clicking in the gutter area. A red circle will appear that looks like this:
Now run the program in debug mode by clicking the green triangle button or clicking F5. The program will run, producing output in the console (separate window). Once it hits a breakpoint, you can check the console for output, for example:
source to share
Like many previous ones: this is too fast, so use breakpoints or use Read ...
You can also write to the Visual Studio output window with System.Diagnostics.Debug.Write
source to share