Добавил:
Upload Опубликованный материал нарушает ваши авторские права? Сообщите нам.
Вуз: Предмет: Файл:
C# ПІДРУЧНИКИ / c# / Hungry Minds - Visual C# Blueprint.pdf
Скачиваний:
101
Добавлен:
12.02.2016
Размер:
9.71 Mб
Скачать

C#

VIEW THE MAIN METHOD

The MDE window automates some code generation so you can better use your time writing the meat of your code.

After you create a new C# program, the MDE window creates the basic structure of your program based on the program you want to create. For example, if you create a console application, then the MDE window will create one class with a Main method included so you can perform functions such as specify variables, perform calculations, and output results on the screen.

The Main method is the block of code where you perform many of your functions. Without a Main method your C#

program cannot run, so no matter what type of C# project you want to create, the MDE window will always include a skeleton Main method — a Main method that does not have any functional code within it.

The default state for the Main method is void — the method returns no values of its own. Instead, the method processes the code within it. For example, you can add two numbers and output the result.

Depending on the type of project you create, the Main method contains comments that tell you to replace the comments with the functional code.

VIEW THE MAIN METHOD

¤ Click New Project in the Start page.

The New Project window appears.

Click the Console Application icon in the Templates pane.

Type a name for your file.

ˇ Click OK.

WORKING WITH VISUAL C# BASICS 3

The Length property lets you test the Main method that contains the string arguments to see if the method works as it should.

TYPE THIS:

using System; class Class1;

{

public static void Main(string[] args) if (args.Length == 0)

{

Console.Writeline("Please enter a numeric argument: ");

return 1;

}

}

RESULT:

Please enter a numeric argument: 1

The return statement is the last statement in the Main method and returns the number 1 as the output.

Solution Explorer...

Form1.cs

The form appears in the parent window.

Á Click the View Code button in the Solution Explorer window.

Scroll down the code window until you reach the bottom.

The Main method appears that tells the application to run the form.

39

C#

COMBINE PROGRAM TYPES

C# categorizes the elements it uses, such as numbers and characters, into types. These types include predefined basic types, such as numeric and Boolean

types, and user-defined types that include structs and enumerated types. Basic types include numbers and the type the number belongs to identifies the kind of number it is. For example, a number that is an integer can only be a whole number in a range from –2,147,643,848 to 2,147,483,647. Integers cannot have decimal places; numbers with decimal places belong in the decimal type. You declare these types when you equate a number with a variable, such as declaring that the number 5 is an integer.

As with other programming languages, C# requires that you declare the correct type for its associated number.

Numeric types belong to the struct category that is one of the two large C# type categories. The other is the enumeration type. The enumeration type lets you specify a list of constants and then assigns numbers to those constants so you can select one of the constants for use in your program. For example, you can specify months of the year in an enumeration and then output a month on the screen by calling the enumeration number associated with that month.

COMBINE PROGRAM TYPES

Click New Project in the Start page.

The New Project window appears.

Click the Console Application icon in the Templates pane.

Type a name for your file.

ˇ Click OK.

WORKING WITH VISUAL C# BASICS 3

You can determine the value of your constants by assigning constants to the first enumeration element.

TYPE THIS:

using System;

public EnumClass

{

enum WeekDays {Mon=1, Tues, Wed, Thurs, Fri, Sat, Sun}

public static void Main()

{

int x = (int) WeekDays.Wed;

Console.WriteLine("The Wednesday enum value is {0}", x);

}

}

RESULT:

The Wednesday enum value is 3.

The Class1.cs code appears in the parent window.

Note: You can make more room for your Start page by clicking and dragging the right edge of the Start page until you reach the maximum size for the Start page.

Á Delete the comments within the Main method.

Type the code that defines some numeric and string types, adds the numeric types, and outputs the result.

° Run the program by

· Save the program as the

pressing the F5 key.

filename.

The combined numeric

 

types and string types appear

 

on the screen.

 

41

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