Chapter 4: Using Types Flashcards

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

A

MarshallAs

22
Q

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

A

dynamic type

23
Q

How does the dot net framework store chars?

A

UTF-16

24
Q

Are c# strings immutable?

A

Yes. When you think you are manipulating them, they are actually being created and assigned to the reference.

25
Q

Describe the intern pool and how It applies to strings

A

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
Q

What is the purpose of StringBuilder.EnsureCapacity?

A

To allow the memory allocation up front, instead of having to do it later, essentially twice.

27
Q

A what point does StringBuilder become more efficient than concatenating strings.

A

Around 7 concatenations.

28
Q

What is the benefit/limitations of StringWriter vs. StringBuilder.

A

stringWriter is only for appending. It’s faster while StringBuilder is more flexible. Also, StringWriter implements TextWriter.

29
Q

What is StringReader used for.

A

Reading strings sequentially. Plus StringReader implements TextReader.

30
Q

Can ToString be given formats?

A

Yes.

31
Q

Standard Numeric Format String: C or c

A

Currency

32
Q

Standard Numeric Format String: D or d

A

Decimal (Integer Types only)

33
Q

Standard Numeric Format String: E or e

A

Scientific Notation

34
Q

Standard Numeric Format String: F or f

A

Fixed-point

35
Q

Standard Numeric Format String: G or g

A

General (fixed-point or scientific, whichever is shorter.)

36
Q

Standard Numeric Format String: N or n

A

Number (with decimal and thousands separators)

37
Q

Standard Numeric Format String: P or p

A

Percent (multiplied by 100 and % added)

38
Q

Standard Numeric Format String: X or x

A

Hexadecimal (integer types only)

39
Q

Standard DateTime Format String: d

A

short date 3/14/2014

40
Q

Standard DateTime Format String: D

A

Long Date Friday, March 14, 2012

41
Q

Standard DateTime Format String: f

A

“Full” with short time

Friday, March 14, 2012 2:15 PM

42
Q

Standard DateTime Format String: F

A

“Full” with long time

Friday, March 14, 2012 2:15:16 PM

43
Q

Standard DateTime Format String: g

A

“General” with short time

3/14/2014 2:15 PM

44
Q

Standard DateTime Format String: G

A

“General” with longtime

3/14/2014 2:15:16 PM

45
Q

Standard DateTime Format String: m or M

A

Month/Day

46
Q

Standard DateTime Format String: t

A

short time 2:15 PM

47
Q

Standard DateTime Format String: T

A

long time 2:15:16 PM

48
Q

Standard DateTime Format String: y or Y

A

Year/Month March, 2014

49
Q

CLR

A

Common Language Runtime (CLR) A virtual machine that manages execution of C# (and other .NET) programs.

50
Q

composite format

A

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
Q

immutable

A

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
Q

Unicode

A

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.