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

Входные данные

2

98.6

Пример результатов выполнения.

Please select the convertor

1. From Celsius to Fahrenheit.

2. From Fahrenheit to Celsius.

:2

Please enter the Fahrenheit temperature: 98.6

Temperature in Celsius: 37.00

Дополнительные примеры результатов могут выглядеть следующим образом.

Please select the convertor

1. From Celsius to Fahrenheit.

2. From Fahrenheit to Celsius.

:1

Please enter the Celsius temperature: 37.00

Temperature in Fahrenheit: 98.60

How to: Know the Difference Between Passing a Struct and Passing a Class Reference to a Method

This example shows that when a struct is passed to a method, a copy of the struct is passed, but when a class instance is passed, a reference is passed.

The output of the following example shows that only the value of the class field is changed when the class instance is passed to the ClassTaker method. The struct field, however, does not change by passing its instance to the StructTaker method. This is because a copy of the struct is passed to the StructTaker method, while a reference to the class is passed to the ClassTaker method.

Example

class TheClass

{

public string willIChange;

}

struct TheStruct

{

public string willIChange;

}

class TestClassAndStruct

{

static void ClassTaker(TheClass c)

{

c.willIChange = "Changed";

}

static void StructTaker(TheStruct s)

{

s.willIChange = "Changed";

}

static void Main()

{

TheClass testClass = new TheClass();

TheStruct testStruct = new TheStruct();

testClass.willIChange = "Not Changed";

testStruct.willIChange = "Not Changed";

ClassTaker(testClass);

StructTaker(testStruct);

System.Console.WriteLine("Class field = {0}", testClass.willIChange);

System.Console.WriteLine("Struct field = {0}", testStruct.willIChange);

}

}

Class field = Changed

Struct field = Not Changed

Определение различия между передачей структуры и ссылки класса в метод

В этом примере показывается, что при передаче структуры в метод передается копия структуры, а при передаче экземпляра класса передается ссылка.

В результате выполнения следующего примера видно, что при передаче экземпляра класса в метод ClassTaker изменяется только значение в поле класса. Однако при передаче экземпляра структуры в метод StructTaker ее поле не изменяется. Это происходит потому, что в метод StructTaker передается копия структуры, а в метод ClassTaker передается ссылка на класс.

Пример

----

Anonymous Types

Anonymous types provide a convenient way to encapsulate a set of read-only properties into a single object without having to first explicitly define a type. The type name is generated by the compiler and is not available at the source code level. The type of the properties is inferred by the compiler. The following example shows an anonymous type being initialized with two properties called Amount and Message.

var v = new { Amount = 108, Message = "Hello" };

Anonymous types are typically used in the select clause of a query expression to return a subset of the properties from each object in the source sequence.

Anonymous types are created by using the new operator with an object initializer.

Anonymous types are class types that consist of one or more public read-only properties. No other kinds of class members such as methods or events are allowed.

The most common scenario is to initialize an anonymous type with some properties from another type. In the following example, assume a class that is named Product that includes Color and Price properties together with several other properties that you are not interested in. Products is a collection of Product objects. The anonymous type declaration starts with the new keyword. It initializes a new type that uses only two properties from Product. This causes a smaller amount of data to be returned in the query.

If you do not specify member names in the anonymous type, the compiler gives the anonymous type members the same name as the property being used to initialize them. You must provide a name to a property that is being initialized with an expression.