Thursday, July 3, 2014

Hello World C#

I don't know why but if you want learn a new programming language, the first program is always a "Hello World" program. So let the first posting of this blog is another "Hello World" program written in a C# programming language.

Today we write 2 different "Hello World" applications. First in a console application, then in a windows form application. We develop our applications using Visual Studio 2012 in .net Framework 4.5.

Let's start with Console Application.

Console Application


Start Visual Studio and from the menu bar select FILE, New, Project.



The New Project dialog box opens. From the left side menu select Installed, Templates, Visual C#



Change the name of the project to HelloWorldConsoleApplication and choose the OK button. The project we have created appears in Solution Explorer. Program.cs opens in the Code Editor. If it is not open select it from the Solution Explorer and open it.


As seen in the Code Editor, Program class contains a Main method. A console application in C# must contain a static Main method where we create objects and call other methods. Now let's fill the Main method with appropriate codes that prints the line "Hello World!" to a command prompt. Main method should seem like this:



        static void Main(string[] args)
        {
            //Prints the string to the console window
            Console.WriteLine("Hello World!");

            /*
             * Keeps the window open.
             * If any key is hit then console window closes.
             */
            Console.WriteLine("Press any key to exit.");
            Console.ReadKey();
        }

After this changes, press F5 key to run the project or run it from the menu bar select DEBUG, Start Debugging. A console window appears that contains two lines "Hello World!" and "Press any key to exit.".



Comment Lines

The line start with characters // is a comment line in C# programming language. 
//Prints the string to the console window

If you want to comment out more than one line of text or a block of text, you can write the text between /* and */ characters as shown in the code.
/*
 * Keeps the window open.
 * If any key is hit then console window closes.
 */

Console Methods

Two of the console methods are used in this example. Console.WriteLine method prints the parameter to the console window. Console.ReadKey method gets the next character or function key pressed by the user. Also the pressed key is printed to the console window. In debug mode Console.ReadKey method prevents the console window to be closed until the user hits a key. If this line is not exists in Main method, console window will close immediately that we cannot realize even the parameter is printed to the console window.



Windows Form Application

Start Visual Studio and from the menu bar select FILENewProject.
The New Project dialog box opens. From the left side menu select Installed, Templates, Visual C#



Change the name of the project to HelloWorldWindowsForm and choose the OK button. The project we have created appears in Solution Explorer. A windows form named Form1 opens in Designer View. If it is not open select it from the Solution Explorer and open it. To view the code of the form press F7 or from Solution Explorer right click on Form1.cs file and choose View Code or in Designer View right click on form itself and choose View Code.




Return back to the Designer View and from Toolbox (if toolbox windows not open from the menu bar select VIEW, Toolbox to open it.) under the Common Controls select Button, drag it on to the form and put it to an appropriate place. Add another button to the form. Now there are two buttons on the form that named as button1 and button2. To rename a control on the form select the control on the form and from the Properties window change the (Name) option for the control. If a control has a text on it, it can be usually changed from optioın Text under the Properties window. Change the name of the buttons as btnHelloWorld and btnExit. Also change the text properties of the buttons as "Hello World" and "Exit".




Functionalities of the controls in a form are provided by defining events for the controls. Double click on a button to implement its click event or double click on empty field near the Click event from the Events under the Properties. By doing so code of the form opens in Code Editor.



Replace the method implementations like below and run the project by pressing the key F5.


        private void btnHelloWorld_Click(object sender, EventArgs e)
        {
            var message = "Hello World";
            MessageBox.Show(message);
        }

        private void btnExit_Click(object sender, EventArgs e)
        {
            Application.Exit();
        }


Form1 appears by the start of the project. In form it is expected that by pressing the "Hello World" button a message will appear with message "Hello World" and by pressing Exit button form will be closed and project will stop working. Let's try it.



This is all for the applications Hello World. Now we have different Hello World applications.

No comments:

Post a Comment