So I am starting to learn how to program and picked up a c# book and downloaded Xamarin Studio on my Mac to get started. The book I am using inevitably uses Visual Studio but I figured it would be essentially the same, but I have already come across a few issues:
When I first create a new console application, it automatically starts up with a hello world template. Is there any way to make the IDE start without this line, as I'm sure it can become somewhat of an annoyance down the road:
Console.WriteLine("Hello World!");
In other words, can I modify the template that Xamarin Studio starts up in?
When I click on Run ---> Start Debugging with the program below, I get the same result as when I run the program without debugging. In both cases, the application fully works as expected, but in my book the author mentions the screen should flash for a split second, unless you include Console.ReadKey();
Is there anything I am missing? And when I include that extra line, I get the expected result of having to push a key twice to end the program regardless of whether I run with or without debugging.
Here is the exact source code:
using System;
namespace test
{
class MainClass
{
public static void Main(string[] args)
{
Console.WriteLine("Hello World!");
}
}
}
When I first create a new console application, it automatically starts up with a hello world template. Is there any way to make the IDE start without this line, as I'm sure it can become somewhat of an annoyance down the road
Thats interesting thought but as the template itself suggests that its black console app, it should have some code which runs successfully.
When I click on Run ---> Start Debugging with the program below, I get the same result as when I run the program without debugging. In both cases, the application fully works as expected, but in my book the author mentions the screen should flash for a split second, unless you include Console.ReadKey(); Is there anything I am missing? And when I include that extra line, I get the expected result of having to push a key twice to end the program regardless of whether I run with or without debugging.
Well, this is just how Xamarin Studio works. The answer to your question lies in how Xamarin Studio is configured. By default Xamarin studio handles some things, there is an option which paused the console by default. If you want to see Console.ReadKey() in action, just right click on your project and go to options -> Run -> General -> Uncheck (Pause Console Output).
But as there is no Console built in Xamarin Studio the external Console (by default Terminal) is used you wont be able to see the flash effect.
Hope this helps. Thanks and happy coding :)
Related
Consider this C# 10 / .NET 6.0 / Visual Studio 2022 "Hello, World" console application:
// See https://aka.ms/new-console-template for more information
foreach (int _ in Enumerable.Range(0, 100))
{
Console.WriteLine("Hello, World!");
Task.Delay(1000).Wait();
}
If I run this program with debugging, or without debugging, changing the World to Earth and then saving the file and clicking "Hot Reload" (Apply Code Changes) button does nothing. The program continues to write to console Hello, World!.
I can force the application to change the string without restarting without using Hot Reload, by using Edit and Continue instead, i.e. by making changes while the application is in break mode.
I did enable all the options for Hot Reload:
Why doesn't the Hot Reload work? Am I doing something wrong? This blog post states it should also work for Console apps.
If you change Task.Delay(1000).Wait() to await Task.Delay(1000) then hot reload on save will work. It seems when a thread is blocked, hot reload doesn't want to work.
I'm on Elementary OS and just installed "MonoDevelop". I'm forced to use C#. However, it came up with a simple Hello World default program:
using System;
namespace HelloCsharp{
class MainClass{
public static void Main (){
Console.WriteLine ("Hello World!");
}
}
}
The problem is that my "Application Output" panel is empty.
At least it creates a new line (yet an empty line). I've selected it and thus it is shown as a "blue bar" in the following screenshot.
The checkboxes "Run on external console" and "Pause console output" are checked.
Anyone has any suggestions?
Am I right that this code normally should give me an output in this panel?
If you tick Run on external console, MonoDevelop will start your program...in an external console. By default this is /usr/bin/xterm. You can create a symlink to your preferred console application by creating a symlink to it in /usr/bin/ and naming it xterm.
If you wish to receive the output to the Application Output pad instead, untick Run on external console.
I just wrote a clever program called helloworld. It's a C#/.NET 4.5 console app. Deep within the twisted nested mazes of logic there's use of Console.WriteLine().
When I'm running this at a command line, it runs and I see the output. I can do other commands and mess around a bit, and later scroll up to see the output again.
Now I'm in Visual Studio, tweaking the source ("Hi" is more efficient than "Hello") and testing by tapping F5. What happens is a console window pops up and immediately vanishes. I have no idea what the program printed. How can I see the output?
I don't want to modify my source at all. After searching for solutions, I find some who say to use Console.ReadKey() - but then it would suck to be using the program at the command line. There's no real reason the user should have to tap a key when the program has already done its work. Even if i go with this, there's the problem of the output disappearing when the console window closes after a key tap.
I don't want to use Debug.WriteLine() which does write to the output window in VS, but doesn't write ordinary output for the end user to see.
I have discovered ctrl-F5, which runs the program as if it had a final Console.ReadKey() line, but there's still the problem of when I tap any key, all the output vanishes along with the window. Three minutes later, I'm thinking "Oh wait, did it print 'Hello' or 'Helo'?" No way to check.
Seems like the Visual Studio IDE should somehow capture all that a freshly built program writes to its stdout or the Microsoft equivalent thereof, and show it in its "Output" panel, or some panel, for later scrutiny. Maybe it does do this, and I don't yet know the trick to it? Seems like this would be a common desire among millions of C# developers.
If you're working on a .NET Core console application (or .NET Framework via the .NET SDK) using Visual Studio 2019, the behaviour of leaving the console window open after the program has executed will now happen by default:
Specifically:
This should prevent the need to add Console.Read() calls to console apps to prevent the console window from closing immediately after the program has finished executing. The launched console window is also re-used for subsequent runs, so if you’re used to using ctrl+f5, you won’t have lots of console windows to close after you’ve launched your application a few times.
The reason it closes automaticly is because it's done running the program. If you want to see what it did, make it need a new command like hitting any key. The Console.ReadKey(); pauses it and waits for a User to hit a key to continue. Put that command after the commands of instruction you are doing and it will pause it until you hit any key.
Console.ReadKey(); // Pauses until you hit any key
You can also run your program pressing F10 (executes one line by one), with F11 (goes inside a function).
The other option you have is to set breakpoints in Visual Studion and run the program by pressing F5 - it will stop at the next breakpoint. And the breakpoints can have conditions - i.e. conditional breakpoints.
Some options are:
1. wrap #if DEBUG around Console.ReadKey()
2. run directly from an open terminal
3. create a Test project - but again you'll need Console.ReadKey() to stop it closing.
Working on a project that keeps crashing. No errors and showing up, my memory monitor tells me that everything is normal and as far as I can see there is nothing aberrant in my code it just stops working, freezes. Is there a way to see a history in visual studio of function calls so I can ascertain accurately the last thing my program does before it breaks?
Thanks!
menu bar -> DEbug -> Windows -> Call Stack
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
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.
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 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:
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
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
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