Изменения документа Преобразование типов
Редактировал(а) Alexandr Fokin 2023/06/26 11:34
От версии 1.9
отредактировано Alexandr Fokin
на 2023/02/25 13:22
на 2023/02/25 13:22
Изменить комментарий:
К данной версии нет комментариев
К версии 1.6
отредактировано Alexandr Fokin
на 2023/02/25 13:12
на 2023/02/25 13:12
Изменить комментарий:
К данной версии нет комментариев
Сводка
-
Свойства страницы (1 изменено, 0 добавлено, 0 удалено)
Подробности
- Свойства страницы
-
- Содержимое
-
... ... @@ -1,18 +1,16 @@ 1 1 |(% style="width:138px" %)AS|(% style="width:869px" %)Выполняет попытку приведения к родительскому или дочернему типу (относительно типа переменной) интерфейсному типу. 2 -В случае неудачи возвращает null, не выбрасывает ошибку|(% style="width:440px" %){{code language="c#"}}ParentType p1 = ChildVar as ParentType; 3 -InterfaceType p2 = instanceVar as InterfaceType; 4 -p1 as ChildType{{/code}} 2 +В случае неудачи возвращает null, не выбрасывает ошибку|(% style="width:440px" %){{code language="c#"}}parentType p1 = childVar as parentType; 3 +interfaceType p2 = instanceVar as interfaceType;{{/code}} 5 5 |(% style="width:138px" %)IS|(% style="width:869px" %)Проверяет является ли сущность определенным типом. 6 -true/false.|(% style="width:440px" %){{code language="c#"}}bool p1 = childVar is ParentType;7 -bool p2 = instanceVar is InterfaceType;5 +true/false.|(% style="width:440px" %){{code language="c#"}}bool p1 = childVar is parentType; 6 +bool p2 = instanceVar is interfaceType; 8 8 9 -if (instanceVar is InterfaceType interfaceTypeVar)8 +if (instanceVar is interfaceType interfaceTypeVar) 10 10 { 11 11 interfaceTypeVar.InterfaceMethod(); 12 12 }{{/code}} 13 -|(% style="width:138px" %)Upcasting and Dowcasting|(% style="width:869px" %)Также как и AS выполняет попытку приведения к типу, но в случае ошибки бросает Exception.|(% style="width:440px" %){{code language="none"}}ParentType p1 = (ParentType)childVar; 14 -InterfaceType p2 = (InterfaceType)instanceVar; 15 -(ChildType)childVar;{{/code}} 12 +|(% style="width:138px" %)Upcasting and Dowcasting|(% style="width:869px" %)Также как и AS выполняет попытку приведения к типу, но в случае ошибки бросает Exception.|(% style="width:440px" %){{code language="none"}}parentType p1 = (parentType)childVar; 13 +interfaceType p2 = (interfaceType)instanceVar;{{/code}} 16 16 |(% style="width:138px" %)implicit and explicit operator|(% style="width:869px" %)((( 17 17 Механизм который при вызове может выглядеть словно (Upcasting and Dowcasting), но на самом деле выполняющий преобразование одного типа в другой (подразумевает логику создания экземпляра целевого типа и наполнение его данными на основе переданной сущности). 18 18 В теле класса может быть определен метод как преобразование экземпляров класса в экземпляры внешнего типа, так и экземпляр внешнего типа в экземпляр класса. ... ... @@ -22,15 +22,15 @@ 22 22 )))|(% style="width:440px" %){{code language="c#"}}class MyClass1 23 23 { 24 24 public static implicit operator Type1 (MyClass1 instance) 25 - { / *...*/}23 + { //... } 26 26 } 27 27 class MyClass2 28 28 { 29 29 public static explicit operator Type1 (MyClass2 instance) 30 - { / *...*/}28 + { //... } 31 31 } 32 32 33 -static void Action(Type1 param){ / *...*/}31 +static void Action(Type1 param){ // ... } 34 34 35 35 MyClass1 myClass1Var; 36 36 MyClass2 myClass2Var; ... ... @@ -50,3 +50,5 @@ 50 50 void Method1<T>(T param) 51 51 where T : ParameterInterfaceType 52 52 }{{/code}} 51 + 52 +