I am trying to learn some C #, one of the code snippets I found:
(CheckBox)c
(where c was the result of foreach)
I understand that this makes the compiler know that c will behave like a flag, my question is: what is called this type of construct? I would like Google to better understand it.
Thank!
. , , , c . MSDN. foreach - IEnumerable, , , , :
c
foreach
IEnumerable
for (CheckBox c in xs) { } for (var c in xs.Cast<CheckBox>()) { }
c CheckBox , . , , , ( , .NET 1.1 Java).
CheckBox
c CheckBox. , c, CheckBox, , "IsChecked", "Name" ..
.
:
bool isItChecked = c.IsChecked; // this would not compile because object does not have a property "IsChecked"
, , , c CheckBox, :
var checkBoxC = (CheckBox)c; // The Cast bool isItChecked = checkBoxC.IsChecked; // access the casted items properties.
- , " ", , .
PS: , # .
"". :
( #)
, casting. . MSDN. :
casting
- , boxing and unboxing. MSDN
boxing and unboxing
, object c checkbox. checkbox.
object c
checkbox
. , , "c" . , c , InvalidCastException.
, # , , Generics
: , .
It is called " Casting " or " UnBoxing "
static private void TestBoxingAndUnboxing() { int i = 123; object o = i; // Implicit boxing i = 456; // Change the contents of i int j = (int)o; // Unboxing (may throw an exception if the types are incompatible) } //this function is about 2*Log(N) faster static private void TestNoBoxingAndUnboxing() { int i = 123; i = 456; // Change the contents of i int j = i; // Compatible types }
Understand the difference between the two, although they do the same.
Source: https://habr.com/ru/post/1534782/More articles:Sympy: Conjugate real functions - pythonMaven-war-plugin doubles manifest.mf - javahttps://translate.googleusercontent.com/translate_c?depth=1&pto=aue&rurl=translate.google.com&sl=ru&sp=nmt4&tl=en&u=https://fooobar.com/questions/1534779/python-functions-access-to-global-variables&usg=ALkJrhjpc8IVLH2uK9mykZ2Mhm83LSVXagFor loop and sleep in bash script - linuxМежду клиентом и службой существует несоответствие типов. Ошибка после обновления лазурного хранилища - c#Using Photoshop PSD template for Android - androidMake Java class visible only within the project / app workspace - javaHow to keep integral float number in Lua 5.3 - luaОшибка JSON: ошибка - jsonПример приложения или учебного примера SQLNotificationRequest - c#All Articles