Skip to main content

My First C# Program

C# is a popular programming language produced and mainteined by Microsoft.

This page is included basics of C# programming language and my learning steps with some code snippets.

On following lines, there is my first program's codes. It runs in "console" application.

using System;
namespace Basics
{
    class Program
    {
        static void Main(string[] args)
        {
            Console.Title = ("Started to Learn!");          // Set a title for CMD window.
        basla:                                             // a flag to return here back.
            Console.WriteLine("What is your name? ");       // Printing to the screen
 
            string ad = Console.ReadLine();                 // set a variable named "ad" and waiting for user input.
            Console.WriteLine("\nHow old are you");         // Text which was printed on the screen. "\n" flag is for jump the next line
 
            int yas = Convert.ToInt32(Console.ReadLine());  // set a variable named "yas". We convert the input from string to integer 
            Console.WriteLine("\n\nHi, " + yas + " years old " + ad + "\n\nLet's change the color of the text.");    //Kullanıcının girdiği bilgileri ekrana yazdırıyoruz."\n" ile yazının bir alt satırda çıkmasını sağlıyoruz.
        RenkSecimi:                                     // a flag to return back here
            Console.WriteLine("Choose a color and write its name on the screen: yellow, red, blue");
            string YaziRengi = Console.ReadLine(); // set a variable which named "YaziRengi".
 
            //Below, there is an if else statement sample. We are going to get an input from user to set the text's color
 
            if (YaziRengi == "yellow")
            {
                Console.ForegroundColor = ConsoleColor.Yellow;
            }
            else if (YaziRengi == "red")
            {
                Console.ForegroundColor = ConsoleColor.Red;
            }
            else if (YaziRengi == "blue")
            {
                Console.ForegroundColor = ConsoleColor.Blue;
            }
            else
            {
                Console.WriteLine("\nError! You can choose only one of the following colors.");
                goto RenkSecimi;
            }
            Console.WriteLine("By this, the color of text is going to be " + YaziRengi);
            Console.WriteLine("\n\nTo return back to beginning write 'again', \nTo return back to the color choosing section write 'color', \nPress any key to quit the program.");
 
            string Secim = Console.ReadLine();
            //Below, there is a control structure for returning back to section of the program.
 
            if (Secim == "again")
            {
                Console.Clear(); // This will clear the screen
                goto basla;
            }
            else if (Secim == "color")
            {
                Console.Clear(); // This will clear the screen
                goto RenkSecimi;
            }
            else
            {
                Console.Clear();
                Console.WriteLine("\n\nGood Bye!\n\n\n");
            }
        }
    }
}
 

Download this code.


Comments