Chapter 4: Using Types Flashcards

(53 cards)

0
Q

What happens during a narrowing conversion that fails for floats? (Double to float)

A

The result float is set to infinity. Check for this with float.IsInfinity (static method)

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

What type does the “checked” block work for and what does it do?

A

Only for integer. It throws an exception when a narrowing conversion fails.

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

Are implicit conversions allowed for narrowing conversions?

A

No

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

If x were an Int32, what would you get by calling:

BitConverter.GetBytes(x)

A

An array of bytes, 4 actually.

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

How is a cast handled from float to int?

A

Truncate

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

System.convert.toInt32 does what to a float-to-int conversion?

A

Rounds to nearest int.

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

Once you cast a reference type to an ancestor class, can you use it again later as the original type?

A

Yes. It never really changes, it’s a reference (address) after all. You can simply “convert” it back, and no values are lost.

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

Converting to an ancestor class is what kind of conversion? (Widening or narrowing?)

A

Widening. An explicit cast is required to go the other direction.

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

Will the following code throw?

Person person = new Person();
Employee employee = (Employee) person;
A

Yes, the underlying type is actually a Person not an employee.

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

The is operator will returns true for an object if…

A

The type being checked is in the class hierarchy of the type being tested.

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

What will an as operator return if the types are incompatible.

A

A null object.

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

What are the three main methods for converting values?

A

Parsing methods
System.Convert
System.BitCoverter

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

Parsing methods of conversion only apply to what kind of input and output?

A

Strings as input and primitive data types as output.

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

How can you get some parsing methods to accept non-numeric characters?
Give some examples

A

System.Globalization.NumberStyles
AllowLeadingSign
AllowLeadingParenthesis
AllowDecimalPoint

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

By default the decimal parser enables what NumberStyles?

A

Thousands and decimals (points)

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

In C# U.S. formats always apply to what regardless of the locale?

A

Literal values

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

What kind of rounding does ToIntXX, and ToUIntX use, and how does it work?

A

Banker’s rounding

Round to nearest integer, when tied go with nearest even.

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

How would you do a conversion where you won’t know the type until runtime?

A

Use Convert.ChangeType(value,type)

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

Once you have an array of bytes can you convert these into integers?

A

Yes, BitConverter has methods like ToInt

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

Define Boxing and Unboxing

A

Boxing is converting a value type into an object or an interface that is supported by the value’s type. Unboxing is the converting a boxed value back into its original value.

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

What are the two most common ways to engage interopperability?

A
COM Interop
Platform Invoke (PInvoke)
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
21
Q

Helper attribute for PInvoke to help manage data types

22
Q

When C# does not understand a data type in a dll pulled in using COM, this type helps pacify the compiler.

23
Q

How does the dot net framework store chars?

24
Are c# strings immutable?
Yes. When you think you are manipulating them, they are actually being created and assigned to the reference.
25
Describe the intern pool and how It applies to strings
Each new string created is an entry into the intern pool. The intern pool holds a reference to each unique string. Since strings are I'm unable concatenating strings results in a lot of overhead, as each concatenation involves a new string creation and another entry into the intern pool.
26
What is the purpose of StringBuilder.EnsureCapacity?
To allow the memory allocation up front, instead of having to do it later, essentially twice.
27
A what point does StringBuilder become more efficient than concatenating strings.
Around 7 concatenations.
28
What is the benefit/limitations of StringWriter vs. StringBuilder.
stringWriter is only for appending. It's faster while StringBuilder is more flexible. Also, StringWriter implements TextWriter.
29
What is StringReader used for.
Reading strings sequentially. Plus StringReader implements TextReader.
30
Can ToString be given formats?
Yes.
31
Standard Numeric Format String: C or c
Currency
32
Standard Numeric Format String: D or d
Decimal (Integer Types only)
33
Standard Numeric Format String: E or e
Scientific Notation
34
Standard Numeric Format String: F or f
Fixed-point
35
Standard Numeric Format String: G or g
General (fixed-point or scientific, whichever is shorter.)
36
Standard Numeric Format String: N or n
Number (with decimal and thousands separators)
37
Standard Numeric Format String: P or p
Percent (multiplied by 100 and % added)
38
Standard Numeric Format String: X or x
Hexadecimal (integer types only)
39
Standard DateTime Format String: d
short date 3/14/2014
40
Standard DateTime Format String: D
Long Date Friday, March 14, 2012
41
Standard DateTime Format String: f
"Full" with short time | Friday, March 14, 2012 2:15 PM
42
Standard DateTime Format String: F
"Full" with long time | Friday, March 14, 2012 2:15:16 PM
43
Standard DateTime Format String: g
"General" with short time | 3/14/2014 2:15 PM
44
Standard DateTime Format String: G
"General" with longtime | 3/14/2014 2:15:16 PM
45
Standard DateTime Format String: m or M
Month/Day
46
Standard DateTime Format String: t
short time 2:15 PM
47
Standard DateTime Format String: T
long time 2:15:16 PM
48
Standard DateTime Format String: y or Y
Year/Month March, 2014
49
CLR
Common Language Runtime (CLR) A virtual machine that manages execution of C# (and other .NET) programs.
50
composite format
composite format A format item used by String.Format to indicate how an argument should be formatted. The basic syntax is {index[,length][:formatString]}.
51
immutable
immutable A data type is immutable if its value cannot be changed after it has been created. The String class is immutable. String methods that seem to modify a String, such as Replace and ToUpper, actually replace the String with a new value containing the modified contents.
52
Unicode
Unicode is a standard for encoding characters used by scripts in various locales around the world. It enables a program to display English, Chinese, Kanji, Arabic, Cyrillic, and other character sets. The .NET Framework uses the UTF-16 encoding, which uses 16 bits to represent each character.