I’m trying to learn C#. A lot of the tutorials I’ve found write to the console, but I’d like to practice it in the context of a web app, and write my results to the page in some kind of control (literal, label, or something). I’m starting out with a simple FizzBuzz program, but for some reason it’s only printing the last number in the loop (99). Any ideas how I can write out each iteration to the page? Thanks in advance
My code:
<asp:Literal ID="myLit" runat="server"></asp:Literal>
namespace practice
{
public partial class fizzbuzz_csharp : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
for (int i = 0; i < 100; i++)
{
myLit.Text = i.ToString();
}
}
}
}
You are writing the text directly to the control and not appending it.
At the end of the loop al previous values are overwritten and only the last one is shown
Change it to