top of page

A simple game written in C# using .NET

 

Example: "Guess the Number" Game in .NET (C# Console App)

 Requirements:

  • .NET SDK installed

  • Any C# IDE (like Visual Studio or VS Code)

  • Console application

Object of Game:

The program randomly selects a number between 1 and 100.

The player has to guess it, and the game provides feedback on whether the guess is too high, too low, or correct.

Code: Program .cs

using System;

namespace GuessTheNumberGame
{
    class Program
    {
        static void Main(string[] args)
        {
            var random = new Random();
            int targetNumber = random.Next(1, 101); // 1 to 100
            int guess = 0;
            int attempts = 0;

            Console.WriteLine("🎯 Welcome to Guess the Number!");
            Console.WriteLine("I'm thinking of a number between 1 and 100...");
            
            while (guess != targetNumber)
            {
                Console.Write("Enter your guess: ");
                string input = Console.ReadLine();

                if (!int.TryParse(input, out guess))
                {
                    Console.WriteLine("❌ Please enter a valid number.");
                    continue;
                }

                attempts++;

                if (guess < targetNumber)
                {
                    Console.WriteLine("🔼 Too low! Try again.");
                }
                else if (guess > targetNumber)
                {
                    Console.WriteLine("🔽 Too high! Try again.");
                }
                else
                {
                    Console.WriteLine($"🎉 Correct! You guessed the number in {attempts} attempts.");
                }
            }

            Console.WriteLine("Thanks for playing!");
        }
    }
}

 

Steps to Run:

1. Create a new console project:

    dotnet new console -n GuessTheNumberGame cd GuessTheNumberGame

2. Replace the content of Program.cs with the code above.

 

3. ​Run the game:

    dotnet run

Selecting a date and displaying it in a calendar - https://www.tutorialspoint.com/asp.net/asp.net_calenders.htm

 

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Default.aspx.cs" Inherits="calendardemo._Default" %>

 

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

 

<html xmlns="http://www.w3.org/1999/xhtml" >

 

   <head runat="server">

      <title>

         Untitled Page

      </title>

   </head>

  

   <body>

      <form id="form1" runat="server">

     

         <div>

            <h3> Your Birthday:</h3>

            <asp:Calendar ID="Calendar1" runat="server  SelectionMode="DayWeekMonth" onselectionchanged="Calendar1_SelectionChanged">

            </asp:Calendar>

         </div>

        

         <p>Todays date is: 

            <asp:Label ID="lblday" runat="server"></asp:Label>

         </p>

        

         <p>Your Birthday is: 

            <asp:Label ID="lblbday" runat="server"></asp:Label>

         </p>

        

      </form>

   </body>

</html>

Retrieve data through a database - https://www.tutorialspoint.com/asp.net/asp.net_database_access.htm

 

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="dataaccess.aspx.cs"

   Inherits="datacaching.WebForm1" %>

 

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"

   "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

 

<html xmlns="http://www.w3.org/1999/xhtml" >

 

   <head runat="server">

      <title>

         Untitled Page

      </title>

   </head>

  

   <body>

      <form id="form1" runat="server">

         <div>

        

            <asp:SqlDataSource ID="SqlDataSource1" runat="server"

               ConnectionString= "<%$   ConnectionStrings:ASPDotNetStepByStepConnectionString%>"

               ProviderName= "<%$ ConnectionStrings:

                  ASPDotNetStepByStepConnectionString.ProviderName %>"

               SelectCommand="SELECT [Title], [AuthorLastName],

                  [AuthorFirstName], [Topic] FROM [DotNetReferences]">

            </asp:SqlDataSource>

           

            <asp:GridView ID="GridView1" runat="server"

               AutoGenerateColumns="False" CellPadding="4"

               DataSourceID="SqlDataSource1" ForeColor="#333333"

               GridLines="None">

               <RowStyle BackColor="#F7F6F3" ForeColor="#333333" />

           

               <Columns>

                  <asp:BoundField DataField="Title" HeaderText="Title"

                     SortExpression="Title" />

                  <asp:BoundField DataField="AuthorLastName"

                     HeaderText="AuthorLastName" SortExpression="AuthorLastName" />

                  <asp:BoundField DataField="AuthorFirstName"

                     HeaderText="AuthorFirstName" SortExpression="AuthorFirstName" />

                  <asp:BoundField DataField="Topic"

                     HeaderText="Topic" SortExpression="Topic" />

               </Columns>

               <FooterStyle BackColor="#5D7B9D"

                  Font-Bold="True" ForeColor="White" />

               <PagerStyle BackColor="#284775"

                  ForeColor="White" HorizontalAlign="Center" />

               <SelectedRowStyle BackColor="#E2DED6"

                  Font-Bold="True" ForeColor="#333333" />

               <HeaderStyle BackColor="#5D7B9D" Font-Bold="True" 

                  ForeColor="White" />

               <EditRowStyle BackColor="#999999" />

               <AlternatingRowStyle BackColor="White" ForeColor="#284775" />

            </asp:GridView>

         </div>

      </form>

   </body>

</html>

bottom of page