3

I am trying to self teach myself C# and wondering if anyone can help me with what seems to be a basic C# question. I created a C# file with this code, started debugging but don’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 have checked the Console and the Browser. Is there some setup that needs to be done to properly debug C# files. I am probably missing the big picture as to how C# works. I am use to PHP where I can just do something like this...

<?php
   include 'my file';
   echo 'my file included';
?> 

Any help would be much appreciated. Thanks.

EDIT:

Thanks everybody for all of the help. You have all helped me understand and realize a number of things about C# / .NET. After extensive troubleshooting it is evident that the problem is not a mater of the debugging working, but the fact that my C# file doesn't appear to be properly hooked/included (not sure what its called in .NET terms) to the rest of the project. Anyways I am accepting keyboardP's answer as he answered first and technically gave me all the right answers. Also thanks to dasblinkenlight who was also extra helpful.

Additional Solution:

After insight from SO users. This article helped point me in the right direction. http://www.asp.net/mvc/tutorials/getting-started-with-aspnet-mvc3/cs/intro-to-aspnet-mvc-3

khollenbeck
  • 16,028
  • 18
  • 66
  • 101

5 Answers5

11

I'm guessing it's because the command prompt window is immediately closing. You can avoid this by adding Console.ReadLine(); after your WriteLine statement. This will wait for you to press return before closing the prompt window.

Alternatively, assuming you're using Visual Studio, you can run the build without a debugger attached by pressing CTRL + F5.

Edit - Based on the extra information added that you're using ASP.NET and not a console application.

Firstly, what are you trying to achieve? If you want to output debug information, then you can Debug.WriteLine instead of Console.WriteLine

System.Diagnostics.Debug.WriteLine("Hello World");

This will output the text to the "Output" window at the bottom of Visual Studio (by default).

Edit 2 Since you just want to write random text to the page, you can use

HttpContext.Current.Response.Write("Hello World");

There are sometimes issues with Response.Write but it should be okay for what you want to do here.

Community
  • 1
  • 1
keyboardP
  • 68,824
  • 13
  • 156
  • 205
  • You should read his comments in his original post. he did not make a console app, it is a ASP.NET page. – Scott Chamberlain Feb 06 '13 at 16:17
  • I most be missing something fundamentally obvious because this is not working for me. The message should appear in the output, is that correct? – khollenbeck Feb 06 '13 at 16:18
  • @japanFour You are not seeing it written to the console because you *did not make a console application* you made a web page. – Scott Chamberlain Feb 06 '13 at 16:20
  • 1
    @japanFour - I've updated the answer. What are you trying to achieve? Are you trying to output debug information or are you trying to display text on a webpage that users will see? – keyboardP Feb 06 '13 at 16:22
  • Preferably I would like to do both at some point. But ultimately I am just trying to make sure I am actually using the file to test my code. – khollenbeck Feb 06 '13 at 16:27
  • @japanFour - I've updated the answer. Try using `Response.Write`. – keyboardP Feb 06 '13 at 16:32
  • I inserted Response.Write inside my class and VS tells me "The name 'Response' does not exist in the current context. – khollenbeck Feb 06 '13 at 16:37
  • @japanFour Try using `HttpContext.Current.Response.Write()`. – keyboardP Feb 06 '13 at 16:39
  • Hmm. No errors. But still no message anywhere. I really appreciate all the help regardless. – khollenbeck Feb 06 '13 at 16:43
  • @japanFour - I just tried `Response.Write` and it works fine. Your project setup just seems incorrect and you shouldn't have a `Main` method present. Are you sure you created an `ASP.NET MVC` project and not a `Console/WinForm` one? I suggest going through [tutorials first](http://www.asp.net/mvc/tutorials/getting-started-with-aspnet-mvc3/cs/intro-to-aspnet-mvc-3) – keyboardP Feb 06 '13 at 16:55
  • Positive I created a ASP.NET MVC project. I think the issue is my new C# file isn't properly hooked into the project. Cause you are right, when I take the Response.Write() and place it in one of the other files it works. – khollenbeck Feb 06 '13 at 17:01
3

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:

Breakpoint is set

Now run your program in debug mode by clicking the button with the green triangle or pressing F5. The program will run, producing the output in the console (a separate window). Once it hits your breakpoint, you can examine the console for the output, like this:

Breakpoint is hit

Sergey Kalinichenko
  • 714,442
  • 84
  • 1,110
  • 1,523
  • When I try to insert a breakpoint I get this message. "A breakpoint could not be inserted at this location". – khollenbeck Feb 06 '13 at 16:15
  • @japanFour You may need to re-build your program in order for the VS-2010 to "understand" that it is OK to set the breakpoint on the line where you are setting it. – Sergey Kalinichenko Feb 06 '13 at 16:16
  • Still no luck after re-build. – khollenbeck Feb 06 '13 at 16:17
  • 2
    You should read his comments in his original post. he did not make a console app, it is a ASP.NET page. – Scott Chamberlain Feb 06 '13 at 16:17
  • @japanFour "I did my project as ASP.NET MVC3 using VS 2010" That's the problem then. You need to create a "console application" project in order to see the output to console. – Sergey Kalinichenko Feb 06 '13 at 16:19
  • Okay well I guess maybe I am mistaken as to how I should debug. I would like to use MVC3 so is there a different way I should test my pages in an ASP.NET MVC3 project? – khollenbeck Feb 06 '13 at 16:22
  • @japanFour Your program will be different with ASP.NET MVC3: there will be no `Main` in the traditional sense; you will need to use `System.Diagnostics.Debug.WriteLine()` instead of `Console.WriteLine`, and so on. Breakpoints would continue to work, though. – Sergey Kalinichenko Feb 06 '13 at 16:25
  • I was able to insert the breakpoint but I still get nothing when using System.Diagnostics.Debug.WriteLine("Hello World"); – khollenbeck Feb 06 '13 at 16:35
  • @japanFour Has your breakpoint been hit (it would look like the second picture, with the yellow arrow in the red circle). The output of `System.Diagnostics.Debug.WriteLine` goes to the debug window of VS-2010; you wouldn't see it unless your code comes to that breakpoint, though. – Sergey Kalinichenko Feb 06 '13 at 16:40
  • Nope it doesn't appear to have been hit. – khollenbeck Feb 06 '13 at 16:44
  • @japanFour Work through the tutorial for ASP.NET (see Scott Chamberlain's answer for a link) to see where you need to put your code in order for it to be executed and breakpoints to be hit. – Sergey Kalinichenko Feb 06 '13 at 16:45
  • I will take a look at it. I was hoping to try to learn as much as I could from the web. But I might have to break down and buy a book. – khollenbeck Feb 06 '13 at 16:47
  • @japanFour The web version of that tutorial seems fine, you may not need a paper book if you work through it. – Sergey Kalinichenko Feb 06 '13 at 16:54
2

You are reading a tutorial for Console Application, however you are trying to create a ASP.NET application. I would reccomend reading a tutorial for ASP.NET

Scott Chamberlain
  • 124,994
  • 33
  • 282
  • 431
0

Like many before said: it goes by too fast, so either use breakpoints, or use a Read...

You can also write to your Visual Studio output window with System.Diagnostics.Debug.Write

Arcturus
  • 26,677
  • 10
  • 92
  • 107
-1

you need to put break point over the line which you want to debug short cut to placing break point is ctrl D,n then you can step over or step into the code with f10 and f11 function keys