|
You have selected free tutorial of the Microsoft Corporation for the Microsoft Technology Associate (MTA) :
98-361: MTA: Software Development Fundamentals (C#) : Module 1: Introduction to Programming :
WRITE A C# PROGRAM
There are two ways to create, compile, and run a C# program.
- The first method is to
use the Visual Studio IDE,
- and the second method is to use the command-line compiler
csc.exe. First you’ll look at using the Visual Studio IDE.
Using the Visual Studio IDE
To write a C# program in the Visual Studio 2013 IDE,
- launch the Visual Studio IDE
- select File, New
Project or press Ctrl+Shift+N. A list of .NET-supported languages is displayed:
- Visual Basic—Developers who are familiar with Visual Basic or a similar language
use this language to build type-safe, object-oriented applications.
- Visual C#—This option is designed for developers who are familiar with C-style
languages, such as C, C++, and Java. Developers can use this language to rapidly
develop type-safe, object-oriented applications.
- Visual C++—Specifically designed for C++ developers, this language can be used
to build .NET managed applications as well as independent Windows-based
applications that do not require .NET support.
- Visual F#—This language makes the task of developing applications related to
science, engineering, and mathematical computations much easier.
- Because you are interested in developing C# applications, select the Visual C#
language from the left pane. After selecting a language, a list of project types that are
supported by that language is displayed on the right. Here’s a brief description of commonly
used project types:
- Windows Forms Application—Creates a standalone Windows
application–GUI-based application based on Windows Forms technology.
- WPF Application—Creates a project that uses a powerful Windows Presentation
Foundation framework.
- Console Application—Creates command-line application executables. These
applications don’t support a graphical user interface and are executed at the
command prompt.
- ASP.NET Web Forms Application—Creates web applications that can be
deployed to a web server.
- Class Library—Creates reusable classes and components that can be shared with
other projects. Specifically, this template enables you to build assemblies that can
be shared with other applications.
- Portable Class Library—Creates and manages assemblies that work on more
than one .NET Framework platform. That is, the assemblies can work on .NET 4,
.NET 4.5, Silverlight 5, Windows Phone 8, and other platforms without
modifications.
- ASP.NET MVC 3/4 Web Application—Creates a web application based on a
web application development framework that splits applications into three layers:
the model, the view, and the controller.
- Silverlight Application—Creates applications based on the application framework
Microsoft Silverlight for developing rich Internet applications. These applications
support multimedia, graphics, animation, languages, and development
tools.
- Silverlight Class Library—Creates a Silverlight class library project that can be
used by Silverlight applications.
- Windows Forms Control Library—Creates custom controls to use on Windows
Forms.
- From the list of project types displayed, select the Console Application type. Specify
the name of the application as ConsoleApp, and then select the OK button .
- The new console application appears, In the console application dialogue box, notice
certain windows and toolbars. Here’s a description of them:
- Solution Explorer—In the Solution Explorer window (docked on the right side),
you can see the default files that are created by the IDE. Only the editable files are
displayed; other IDE-generated files are hidden by default. To display all the files,
including the hidden ones, click the Show All Files icon located at the top of the
Solution Explorer window. If it’s hidden, select View, Solution Explorer to make
the Solution Explorer window visible. Each project type that is selected while
creating a new project provides a specific default code. The main file of the console
application is Class1.cs, and it automatically shows up in the Editor window
when the application is loaded. The Properties folder provides access to the
project-level settings. References are a collection of assemblies that the application
depends on. The project template automatically adds to the project references to
commonly used class libraries
- Properties Window—Located below the Solution Explorer, this window displays
the properties for the currently selected form, control, or object in the IDE.
Properties display information about the selected item, such as its ID, text, size,
and color. The left column lists the properties, and the right column displays
the value of each property. You can modify the properties of the selected item
as per your requirements, and the IDE automatically generates the related code.
You can sort the properties either alphabetically by clicking the Alphabetical icon
or category wise by clicking the Categorized icon. If the Properties window is
hidden, select View, Properties Window to display it.
- Error List Window—This window displays errors in the code if any exist and
helps in pointing out and removing them. After you double-click the error that is
shown in this window, the cursor jumps to the location of the error in the source
code so you can correct it. The code that is causing the error is specifically
marked with red wavy lines. The location of the error is specified as a line number.
By default, line numbers aren’t displayed in the VS text editor. To display the
line numbers, select Tool, Options. From the Options dialog box, expand the Text
Editor Node and select C# node and check the Line Numbers check box
- ToolBox—This shows the tools, also called controls, divided into related categories.
If it’s invisible, ToolBox can be displayed by selecting View, Toolbox. To use
a control in GUI applications, just drag it from the desired category and drop it
on the form, and the IDE automatically generates code for that tool. By default,
the AutoHide feature is enabled for the ToolBox; consequently, the ToolBox
appears when you click its title. The ToolBox appears over the Design window
(in case of Forms) or the Editor window, thereby hiding the controls behind the
ToolBox. Also, it makes it difficult to add controls to the form. To switch off the
AutoHide feature—in other words, to keep the ToolBox visible on the left of the
Design or Editor window—click the pushpin button in the upper-right corner of
the ToolBox.
- Menu Bar and Toolbar—Menu Bar contains the groups of related commands
used for managing and manipulating the IDE and the applications. When you
choose a menu item from a menu, the designated task for that menu item is performed.
The commonly used menu items are provided in the form of tools in the
toolbar. The tools in the toolbar appear as icons. When you hover the mouse, a
ToolTip appears indicating the task that the tool can perform.
- The default code provided in the file Class1.cs is
File
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ConsoleApp
{
public class Class1
{
}
}
You want to display the message Hello World! through this application. For this, add two statements, Console.WriteLine and Console.ReadLine with the Main method, to the preceding code. The Main method is an entry point of a C# console or Windows application. The file after adding the two statements appears as
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ConsoleApp
{
public class Class1
{
static void Main(string[] args)
{
Console.WriteLine("Hello World!");
Console.ReadKey();
}
}
}
The Console.WriteLine() method is meant for displaying a message or a result of processing
on the screen. In contrast, the Console.ReadKey() method is used for getting a
key from the user through the keyboard.
Basically, when you run a console-based application in the Visual Studio IDE, the output
appears in a console window, and control immediately comes back to Visual Studio
IDE. (That is, the console window does not wait for sufficient time for the user to
see the program output and goes back to the IDE immediately; consequently, you will
not be able to view the output of the program.) To make the console window wait for
enough time, enabling the user to view the output of the program, you need to add the
Console.ReadKey() method to the program. The Console.ReadKey() method waits for
the user to press any key on the keyboard, thereby displaying the output of the program.
Only when the user presses a key does the console window disappear. To compile
and run the program, select Debug, Start Debugging or press the F5 key. You get
the output shown in Figure 1.6.
You can see that the text message Hello World! is displayed on the screen.
|
Your Salary Above $ 66000... Click ...
Ohh! You want More.... be game developer of your choice $ 102000 ....
|