H4-Using Types Flashcards

1
Q

what is the “is” operator

A

check if instance is of certain type. if (user is Employee)…

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

what is the “as” operator

A

check if instance is of type otherwise result null.

Employee emp = user as Employee;
if (emp != null)
{…

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

how can you use “as” to do a cast?

A

RadioButton rad = sender as RadioButton;

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

How to cast child array to parent array

A

parentArrayType MyRef = childArrayInstance

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

cast back parentarray to childarray 3 examples

A

ChildArryType MyRef = (ParentArrayType) instance;
if(instance is ChildArryType) {…
MyRef = instance as ChildArryType

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

How copy an array

A

Array.Copy(

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

How to convert a value to/from an array of bytes

A

BitConverter.GetBytes or BitConverter.ToInt..

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

How can you force an exception beeing thrown when casting to an int (under/overflow)

A
checked
{
int big = 1000000;
short small = (short)big;
}
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

what is a risk when using “checked”

A

only direct code is checked, if a methode is called in the checked body then no check is done inside the methode.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

do you get an exception when you get an overflow/underflow on an int type?

A

No, application keeps running, so alway check this using the “checked” body.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

do you get an exception when you get an overflow/underflow on an float type?

A

No, float goes to infinity, this can be checked with float.IsInfinity(floatInstanceUsed)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

How can you parse a string to an int when not using a try/catch block?

A

int.TryParse(weightTextBox.Text, out weight)

Note: out is a ref to the output var, in this case weight.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

How can you alter the allowed parsing text. e.g.int.Parse(“$1000,00”)

A

Use System.Globalization.NumberStyles
example :

decimal amount = decimal.Parse(“$123,456.78”,
NumberStyles.AllowCurrencySymbol |
NumberStyles.AllowThousands |
NumberStyles.AllowDecimalPoint);

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

what is : banker’s rounding?

A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

which rounding type does the “Math.Round” use?

A

banker’s rounding

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

what can go wrong when you box a valuetype

A

the boxes valuetype does not point to the valuetype, therefore if you change the valuetype the boxed type does not change.

17
Q

What is an “intern pool”

A

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.

18
Q

How can you create an empty string

A

String.Empty

19
Q

what is a dynamic type?

A

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";
20
Q

How to check if a string is empty.

A

String.IsNullOrEmpty String.Empty

StringRef = String.Empty;

21
Q

give an example of a string.format

A

{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)