Chapter 4: Using Types Flashcards
What happens during a narrowing conversion that fails for floats? (Double to float)
The result float is set to infinity. Check for this with float.IsInfinity (static method)
What type does the “checked” block work for and what does it do?
Only for integer. It throws an exception when a narrowing conversion fails.
Are implicit conversions allowed for narrowing conversions?
No
If x were an Int32, what would you get by calling:
BitConverter.GetBytes(x)
An array of bytes, 4 actually.
How is a cast handled from float to int?
Truncate
System.convert.toInt32 does what to a float-to-int conversion?
Rounds to nearest int.
Once you cast a reference type to an ancestor class, can you use it again later as the original type?
Yes. It never really changes, it’s a reference (address) after all. You can simply “convert” it back, and no values are lost.
Converting to an ancestor class is what kind of conversion? (Widening or narrowing?)
Widening. An explicit cast is required to go the other direction.
Will the following code throw?
Person person = new Person(); Employee employee = (Employee) person;
Yes, the underlying type is actually a Person not an employee.
The is operator will returns true for an object if…
The type being checked is in the class hierarchy of the type being tested.
What will an as operator return if the types are incompatible.
A null object.
What are the three main methods for converting values?
Parsing methods
System.Convert
System.BitCoverter
Parsing methods of conversion only apply to what kind of input and output?
Strings as input and primitive data types as output.
How can you get some parsing methods to accept non-numeric characters?
Give some examples
System.Globalization.NumberStyles
AllowLeadingSign
AllowLeadingParenthesis
AllowDecimalPoint
By default the decimal parser enables what NumberStyles?
Thousands and decimals (points)
In C# U.S. formats always apply to what regardless of the locale?
Literal values
What kind of rounding does ToIntXX, and ToUIntX use, and how does it work?
Banker’s rounding
Round to nearest integer, when tied go with nearest even.
How would you do a conversion where you won’t know the type until runtime?
Use Convert.ChangeType(value,type)
Once you have an array of bytes can you convert these into integers?
Yes, BitConverter has methods like ToInt
Define Boxing and Unboxing
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.
What are the two most common ways to engage interopperability?
COM Interop Platform Invoke (PInvoke)
Helper attribute for PInvoke to help manage data types
MarshallAs
When C# does not understand a data type in a dll pulled in using COM, this type helps pacify the compiler.
dynamic type
How does the dot net framework store chars?
UTF-16
Are c# strings immutable?
Yes. When you think you are manipulating them, they are actually being created and assigned to the reference.
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.
What is the purpose of StringBuilder.EnsureCapacity?
To allow the memory allocation up front, instead of having to do it later, essentially twice.
A what point does StringBuilder become more efficient than concatenating strings.
Around 7 concatenations.
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.
What is StringReader used for.
Reading strings sequentially. Plus StringReader implements TextReader.
Can ToString be given formats?
Yes.
Standard Numeric Format String: C or c
Currency
Standard Numeric Format String: D or d
Decimal (Integer Types only)
Standard Numeric Format String: E or e
Scientific Notation
Standard Numeric Format String: F or f
Fixed-point
Standard Numeric Format String: G or g
General (fixed-point or scientific, whichever is shorter.)
Standard Numeric Format String: N or n
Number (with decimal and thousands separators)
Standard Numeric Format String: P or p
Percent (multiplied by 100 and % added)
Standard Numeric Format String: X or x
Hexadecimal (integer types only)
Standard DateTime Format String: d
short date 3/14/2014
Standard DateTime Format String: D
Long Date Friday, March 14, 2012
Standard DateTime Format String: f
“Full” with short time
Friday, March 14, 2012 2:15 PM
Standard DateTime Format String: F
“Full” with long time
Friday, March 14, 2012 2:15:16 PM
Standard DateTime Format String: g
“General” with short time
3/14/2014 2:15 PM
Standard DateTime Format String: G
“General” with longtime
3/14/2014 2:15:16 PM
Standard DateTime Format String: m or M
Month/Day
Standard DateTime Format String: t
short time 2:15 PM
Standard DateTime Format String: T
long time 2:15:16 PM
Standard DateTime Format String: y or Y
Year/Month March, 2014
CLR
Common Language Runtime (CLR) A virtual machine that manages execution of C# (and other .NET) programs.
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]}.
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.
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.