H4-Using Types Flashcards
what is the “is” operator
check if instance is of certain type. if (user is Employee)…
what is the “as” operator
check if instance is of type otherwise result null.
Employee emp = user as Employee;
if (emp != null)
{…
how can you use “as” to do a cast?
RadioButton rad = sender as RadioButton;
How to cast child array to parent array
parentArrayType MyRef = childArrayInstance
cast back parentarray to childarray 3 examples
ChildArryType MyRef = (ParentArrayType) instance;
if(instance is ChildArryType) {…
MyRef = instance as ChildArryType
How copy an array
Array.Copy(
How to convert a value to/from an array of bytes
BitConverter.GetBytes or BitConverter.ToInt..
How can you force an exception beeing thrown when casting to an int (under/overflow)
checked { int big = 1000000; short small = (short)big; }
what is a risk when using “checked”
only direct code is checked, if a methode is called in the checked body then no check is done inside the methode.
do you get an exception when you get an overflow/underflow on an int type?
No, application keeps running, so alway check this using the “checked” body.
do you get an exception when you get an overflow/underflow on an float type?
No, float goes to infinity, this can be checked with float.IsInfinity(floatInstanceUsed)
How can you parse a string to an int when not using a try/catch block?
int.TryParse(weightTextBox.Text, out weight)
Note: out is a ref to the output var, in this case weight.
How can you alter the allowed parsing text. e.g.int.Parse(“$1000,00”)
Use System.Globalization.NumberStyles
example :
decimal amount = decimal.Parse(“$123,456.78”,
NumberStyles.AllowCurrencySymbol |
NumberStyles.AllowThousands |
NumberStyles.AllowDecimalPoint);
what is : banker’s rounding?
means values are rounded to the nearest integer, but if
there’s a tie, with the value ending in exactly .5, the value is rounded to the nearest
even integer.
which rounding type does the “Math.Round” use?
banker’s rounding