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
what can go wrong when you box a valuetype
the boxes valuetype does not point to the valuetype, therefore if you change the valuetype the boxed type does not change.
What is an “intern pool”
Location where all strings are stored. Multiple refs with same string value point to the same string location, so there is only one of each kind/value.
How can you create an empty string
String.Empty
what is a dynamic type?
Type that does not get evaluated during compilation.
// Make an array of numbers. int[] array1 = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 }; // This doesn't work because array1.Clone is an object. //int[] array2 = array1.Clone(); // This works. int[] array3 = (int[])array1.Clone(); array3[5] = 55; // This also works. dynamic array4 = array1.Clone(); array4[6] = 66; array4[7] = "This won't work";
How to check if a string is empty.
String.IsNullOrEmpty String.Empty
StringRef = String.Empty;
give an example of a string.format
{index[,length][:formatString]}
The format string is {0} = {1,4} or 0x{2:X}. This string has three format items that mean:
- {0} displays argument 0 with default formatting
- {1,4} displays argument 1 in a field at least four characters wide
- {2:X} displays argument 2 with format string X (which displays an integer in hexadecimal)