Warning: Declaration of Db::query($query) should be compatible with mysqli::query($query, $resultmode = NULL) in /var/www/u1300519/data/www/acoptex.com/_lib/class.Db.php on line 29

Deprecated: Function get_magic_quotes_runtime() is deprecated in /var/www/u1300519/data/www/acoptex.com/_lib/CacheLite/Lite.php on line 757

Deprecated: Function get_magic_quotes_runtime() is deprecated in /var/www/u1300519/data/www/acoptex.com/_lib/CacheLite/Lite.php on line 757

Warning: Use of undefined constant MYSQL_ASSOC - assumed 'MYSQL_ASSOC' (this will throw an Error in a future version of PHP) in /var/www/u1300519/data/www/acoptex.com/_lib/class.Db.php on line 64

Warning: mysqli_fetch_array() expects parameter 2 to be int, string given in /var/www/u1300519/data/www/acoptex.com/_lib/class.Db.php on line 64

Deprecated: Function get_magic_quotes_runtime() is deprecated in /var/www/u1300519/data/www/acoptex.com/_lib/CacheLite/Lite.php on line 757

Deprecated: Function get_magic_quotes_runtime() is deprecated in /var/www/u1300519/data/www/acoptex.com/_lib/CacheLite/Lite.php on line 757

Warning: Cannot modify header information - headers already sent by (output started at /var/www/u1300519/data/www/acoptex.com/_lib/class.Db.php:0) in /var/www/u1300519/data/www/acoptex.com/_config/config.php on line 168

Warning: session_start(): Cannot start session when headers already sent in /var/www/u1300519/data/www/acoptex.com/_config/config.php on line 169

Warning: Use of undefined constant MYSQL_NUM - assumed 'MYSQL_NUM' (this will throw an Error in a future version of PHP) in /var/www/u1300519/data/www/acoptex.com/_lib/class.Db.php on line 92

Warning: mysqli_fetch_array() expects parameter 2 to be int, string given in /var/www/u1300519/data/www/acoptex.com/_lib/class.Db.php on line 92

Warning: Use of undefined constant MYSQL_ASSOC - assumed 'MYSQL_ASSOC' (this will throw an Error in a future version of PHP) in /var/www/u1300519/data/www/acoptex.com/_lib/class.Db.php on line 64

Warning: mysqli_fetch_array() expects parameter 2 to be int, string given in /var/www/u1300519/data/www/acoptex.com/_lib/class.Db.php on line 64
jobs.html_title
0

members

Easy CBASIC Lesson 4 Building Your First Console App Program

of Acoptex.com in Csharp

CBASIC Learning C# basics

Tags: C#, Csharp, C# fundamentals, C# basics

These lessons are designed to keep you engaged with the process of learning C# basics. Our main focus will be on learning C# basics. 

Lesson 4 Building Your First Console App Program

Attachment: ConsoleApp1.zip

Once Visual Studio is successfully installed, launch it in order to build your first console application using C#.

If you are using Windows 10, you can find Visual Studio by typing in the search textbox beside the Start Menu icon - Visual Studio 2017

 You can also do right-click and select Pin to Taskbar as a convenient way for you to launch the application from the Windows taskbar.

You will see that the IDE is loading.

1. Getting Familiar with Visual Studio

Once Visual Studio launches you will be lead to an account login prompt. This second login is for synching up settings from other installs of Visual Studio that you may have signed into previously. This includes settings such as fonts, colors, window placement and so on.

After logging in you will see a Start Page that has a few interesting features:

(1) - dedicated to providing news related items or links into MSDN (Microsoft Developer Network). This news section features valuable resources, such as, articles and videos that can help you learn more about programming in Visual Studio and keep up to date with the latest features.

(2) - convenient way to create a new project, open an existing project or access a list of recent projects, which is a list that will grow larger as we add new projects over the coming lessons.

(3) - important notifications. If you click it, you will see whether or not there are any suggested updates that you should apply. 

A simple adjustment to make code lines easier to follow, is to enabling line numbers for the left margin. To do so, go to the Tools -> Options.

Select Text Editor -> All Languages -> Line Numbers

 This is to enable the line numbers appearing in the left margin when programming for easy reference.

You can also adjust the font size. Go to Environment -> Fonts and Colors.

2. Make a New Console Application

Make a New project by either following the link on the Start page or from the file menu: File -> New -> Project

 You will see the New Project window. Go to the Installed Templates-> Visual C# -> You can select any template (Windows Form App or Console App or ASP.NET Web Application). We need to select the Console App (.NET Framework)framework version (for example 4.7.2), change the name of your project to ConsoleApp1.

Click on OK button to create the console project. Program.cs will be created as default a C# file in Visual Studio where you can write your C# code in Program class as shown below. (The .cs is a file extension for C# file.)

A C# program consists of the following parts:

  • Namespace declaration
  • A class
  • Class methods
  • Class attributes
  • A Main method
  • Statements and Expressions
  • Comments

Let's write our code which will be used to display the string "Hello World!!" in the console application. All the below code needs to be entered into the Program.cs file. The code will be used to write "Hello World!!" when the console application runs.

using System;

using System.Collections.Generic;

using System.Linq;

using System.Text;

using System.Threading.Tasks;

namespace ConsoleApp1

{

    class Program

    {

        static void Main(string[] args)

        {

            string message = "Hello World!!";

            Console.WriteLine(message);

            Console.ReadKey();

            //Console.Write(message);

            //Console.ReadLine();

            //my first program in C#

            /* my first program in C# */

        }

    }

}

Code Explanation:

  • The first lines of code are default lines entered by Visual Studio. The 'using' statement is used to import existing .Net modules in our console application. These modules are required for any .Net application to run properly. They contain the bare minimum code to make a code work on a Windows machine.
  • The next line has the namespace declaration. A namespace is a collection of classes. The ConsoleApp1 namespace contains the class Program.
  • Every application belongs to a class. C# is an object-oriented language, and hence, all code needs to be defined in a self-sustaining module called a 'Class.' In turn, every class belongs to a namespace. A namespace is just a logical grouping of classes.
  • Classes generally contain multiple methods. Methods define the behavior of the class. However, the Program class has only one method Main - it is the entry point for all C# programs. The Main method states what the class does when executed.
  • WriteLine is a method of the Console class defined in the System namespace. This statement causes the message "Hello, World!!" to be displayed on the screen.
  • The last line Console.ReadKey(); is for the VS.NET Users. This makes the program wait for any key press and it prevents the screen from running and closing quickly when the program is launched from Visual Studio .NET.
  • The next lines // and /*...*/ are ignored by the compiler and it is put to add comments in the program.

Notes:

  • Every line or statement in C# must end with a semicolon (;).
  • C# is case sensitive.
  • The program execution starts at the Main method.
  • Unlike Java, program file name could be different from the class name.
  • Console.WriteLine(message) or Console.Write(message). The difference is that with the Console.WriteLine(message) you go automatically to next line and with Console.Write(message) you stay on the same line. 
  • Console.ReadKey() or Console.ReadLine(). Console.ReadKey() makes the program wait for any key press and it prevents the screen from running and closing quickly when the program is launched from Visual Studio .NET. Console.ReadLine() makes the program wait for enter key press and it prevents the screen from running and closing quickly when the program is launched from Visual Studio .NET.

3. Compile and Execute the Program

Click the Run button or press F5 key to execute the project. A Command Prompt window appears that contains the line Hello World!!.

If you did not use Console.ReadKey() or Console.ReadLine() in your program press Ctrl + F5 or select from the main menu Debug ->Start Without Debugging. The console will not close after program execution. If you press F5 or Run it will close quickly after execution.

You can find compiled application named ConsoleApp1.exe in subfolder bin/Debug which is inside of your project folder saved on your PC.

You can compile a C# program by using the command-line instead of the Visual Studio IDE:

  • Open a text editor and add the above-mentioned code.
  • Save the file as helloworld.cs
  • Open the command prompt tool and go to the directory where you saved the file.
  • Type csc helloworld.cs and press enter to compile your code.
  • If there are no errors in your code, the command prompt takes you to the next line and generates helloworld.exe executable file.
  • Type helloworld to execute your program.
  • You can see the output Hello World printed on the screen.

Next -> CBASIC Lesson 5 Basic Syntax



Other projects of Acoptex.com
Medium Basics: Project 083w Sipeed Maixduino board - Using PlatformIO IDE of Acoptex.com in Sipeed Maixduino 08-08-2019
Medium Basics: Project 083e Sipeed Maixduino board - Uploading MaixPy of Acoptex.com in Sipeed Maixduino 04-08-2019
Medium Basics: Project 083f Sipeed Maixduino board - Using MycroPython of Acoptex.com in Sipeed Maixduino 04-08-2019

jobs.published_at
jobs.viewed