Добавил:
Upload Опубликованный материал нарушает ваши авторские права? Сообщите нам.
Вуз: Предмет: Файл:
CSharp_Prog_Guide.doc
Скачиваний:
16
Добавлен:
16.11.2019
Размер:
6.22 Mб
Скачать

Компиляция и выполнение

Для компиляции программы "Hello World!" можно создать проект в среде IDE Visual Studio или воспользоваться командной строкой. Используйте средство командной строки Visual Studio или вызовите vsvars32.bat, чтобы в пути в командной строке открыть средство Visual C#.

Компиляция программы из командной строки.

  • С помощью любого текстового редактора создайте исходный файл и сохраните его с именем Hello.cs. Файл исходного кода C# имеет расширение .cs.

  • Чтобы вызвать компилятор, введите следующую команду:

csc Hello.cs

Если программа не содержит ошибок компиляции, то компилятор создает файл Hello.exe.

  • Чтобы запустить программу, введите следующую команду:

Hello

General Structure of a C# Program

C# programs can consist of one or more files. Each file can contain zero or more namespaces. A namespace can contain types such as classes, structs, interfaces, enumerations, and delegates, in addition to other namespaces. The following is the skeleton of a C# program that contains all of these elements.

// A skeleton of a C# program

using System;

namespace YourNamespace

{

class YourClass

{

}

struct YourStruct

{

}

interface IYourInterface

{

}

delegate int YourDelegate();

enum YourEnum

{

}

namespace YourNestedNamespace

{

struct YourStruct

{

}

}

class YourMainClass

{

static void Main(string[] args)

{

//Your program starts here...

}

}

}

Общая структура программы на c#

Программа на языке C# может состоять из одного или нескольких файлов. Каждый файл может содержать ноль или более пространств имен. Пространство имен может включать такие элементы, как классы, структуры, интерфейсы, перечисления и делегаты, а также другие пространства имен. Ниже приведена скелетная структура программы C#, содержащая все указанные элементы.

--

Main() and Command Line Arguments

The Main method is the entry point of your program, where you create objects and invoke other methods. There can only be one entry point in a C# program.

class TestClass

{

static void Main(string[] args)

{

// Display the number of command line arguments:

System.Console.WriteLine(args.Length);

}

}

Overview

  • The Main method is the entry point of your program, where the program control starts and ends.

  • It is declared inside a class or struct. It must be static and it should not be public. (In the example above it receives the default access of private.)

  • It can either have a void or int return type.

  • The Main method can be declared with or without parameters.

  • Parameters can be read as zero-indexed command line arguments.

  • Unlike C and C++, the name of the program is not treated as the first command line argument.