Добавил:
Upload Опубликованный материал нарушает ваши авторские права? Сообщите нам.
Вуз: Предмет: Файл:
Лаба №1 / books / csharp_ebook.pdf
Скачиваний:
51
Добавлен:
03.03.2016
Размер:
3.69 Mб
Скачать

Programmers Heaven: C# School

language constructs, and many of those are very repetitive; e.g., to handle the data in memory you can either use variable, pointer or reference. C# at its core is very simple yet powerful modern language which has been made while keeping in mind the experience of developers over the years and their current needs. One of the biggest concerns in the development market is to make programs that are re-usable, maintainable, scalable, portable and easy to debug. C# comes right with these issues. Every single C++ developer knows how difficult it is to manage bigger C++ program and debug them. It can be a nightmare to find the reason why a program crashes randomly. The sole reason for this, to me, is the backwards-compatibility curse. They made C++ on the structure of C, a structured programming language, so it never became a true object oriented programming language only, and if the compiler allows me to go in the structured way of solving problem, who are you to make me take an object oriented approach? Also Bjarne Stroustrup, the founder of C++ said, "C++ is a multi-paradigm language not only OO language". The prime advantages of using C# include support for the common language runtime, Framework class library, and a new, clean object oriented design free of the backwards-compatibility curse.

The Visual Studio.Net IDE

Microsoft Visual Studio.Net is an Integrated Development Environment (IDE), which is the successor of Visual Studio 6. It eases the development process of the .Net Applications (VC#.Net, VB.Net, VC++.Net, JScript.Net, J#.Net, ASP.Net, and more). The revolutionary approach in this new improved version is that for all the Visual Studio.Net Compliant Languages use the same IDE, debugger, project and solution explorer, class view, properties tab, tool box, standard menu and toolbars. The key features of Visual Studio.Net include: The IDE provides various useful development tools such as:

Keyword and syntax highlighting

Intellisense (auto complete), which helps by automatically completing the syntax as you type a dot (.) with objects, enumerations, namespaces and when you use the “New” keyword.

Project and solution management with solution explorer that helps to manage applications consisting of multiple files.

Help building user interface with simple drag and drop support.

Properties tab that allow you to set different properties for multiple windows and web controls.

Standard debugger that allows you to debug your program using putting break points for observing run-time behavior.

Hot compiler that checks the syntax of your code as you type it and error notification.

Dynamic Help on a number of topics using the Microsoft Development Network (MSDN) library.

Compiling and building applications.

Program Execution with or without the debugger.

Deploying your .Net application over the Internet or to disk.

Projects and Solutions

A Project is a combination of executable and library files that make an application or module. A project's information is usually placed in a file with the extention '.csproj' where 'cs' represents C-Sharp. Similarly, VB.Net

21

Programmers Heaven: C# School

projects are stored as '.vbproj' files. There are several different kinds of projects such as Console Applications, Windows Applications, ASP.Net Web Applications, Class Libraries and more.

A solution on the other hand is a placeholder for different logically related projects that make some application. For example, a solution may consist of an ASP.Net Web Application project and a Windows Form project. The information for a solution is stored in '.sln' files and can be managed using Visual Studio.Net's Solution Explorer. Solutions are similar to VB 6's Project Group and VC++ 6's workspace.

Toolbox, Properties and Class View Tabs

Now there is a single toolbox for all the Visual Studio.Net's languages and tools. The toolbox (usually present on the left hand side) contains a number of common controls for windows, web and data applications like the text box, check box, tree view, list box, menus, file open dialog, etc.

The Properties Tab (usually present on the right hand side in the IDE) allows you to set the properties on controls and forms without getting into code.

The Class View Tab shows all the classes that your project contains along with the methods and fields in tree hierarchy. This is similar to VC++ 6's class view.

Writing Your First Hello World Console Application in C#

In the following text, we will build our first C# application with, and then without, Visual Studio.Net. We will see how to write, compile, and execute the C# application. Later in the chapter, we will explain the different concepts in the program.

Working Without Visual Studio.Net

Open Notepad, or any other text editor, and write the following code:

using System;

namespace MyHelloWorldApplication

{

class HelloWorld

{

static void Main(string[] args)

{

Console.WriteLine("Hello World");

}

}

}

22

Programmers Heaven: C# School

Save this with any file name with the extension ".cs". Example: 'MyFirstApplication.cs' To compile this file, go to command prompt and write:

csc MyFirstApplication.cs

This will compile your program and create an .exe file (MyFirstApplication.exe) in the same directory and will report any errors that may occur.

To run your program, type:

MyFirstApplication

This will print Hello World as a result on your console screen. Simple, isn't it? Let's do the same procedure with Visual Studio.Net:

With Visual Studio.Net

Start Microsoft Visual Studio.Net and select File - New - Project; this will show the open file dialog. Select Visual C# Project from Project Type and select Console Application from Template. Write MyHelloWorldApplication in the name text box below and click OK.

23

Programmers Heaven: C# School

This will show you the initial default code for your hello world application:

Remove the documentation comments (lines starting with ///), change the name of class from Class1 to HelloWorld and write

Console.WriteLine("Hello World"); in place of //TODO: Add code....

to make the picture look like

using System;

namespace MyHelloWorldApplication

{

class HelloWorld

{

[STAThread]

static void Main(string[] args)

{

Console.WriteLine("Hello World");

}

}

}

24

Соседние файлы в папке books