Errata

C# 6.0 in a Nutshell

Errata for C# 6.0 in a Nutshell

Submit your own errata for this product.

The errata list is a list of errors and their corrections that were found after the product was released.

The following errata were submitted by our customers and have not yet been approved or disproved by the author or editor. They solely represent the opinion of the customer.

Color Key: Serious technical mistake Minor technical mistake Language or formatting error Typo Question Note Update

Version Location Description Submitted by Date submitted
Printed Page 29
Integral Overflow

The text of the book states: At runtime, arithmetic operations on integral types can overflow. By default, this happens silently--no exception is thrown, and the result exhibits "wraparound" behavior, as though the computation was done on a larger integer type and the extra significant bits discarded.

The example, however, should not be expected to do that due to two's complement binary representation of integers. Depending on the specific implementation, it seems more likely that such a specific case might occur from incrementing a signed integer set at -1. Here's what I'm thinking in some rather hideous looking code. The unchecked block is just to show decrementing int.MinValue, which should get you int.MaxValue regardless of whether the value is treated as signed or unsigned. Technically overflow I guess, but not in the same way as the text of the book describes.

uint x = 0xFFFFFFFF;
System.Console.WriteLine((int)x == -1); // true
System.Console.WriteLine(x+1); // 0 or maybe 0x100000000 truncated
System.Console.WriteLine((int)x+1); // same
System.Console.WriteLine((long)x + 1); // 0x100000000
unchecked
{
int a = (int)0x80000000; // int.MinValue
System.Console.WriteLine(a == int.MinValue); // true
int b = (int)0x7FFFFFFF; // int.MaxValue
System.Console.WriteLine(b == int.MaxValue); // true
System.Console.WriteLine(--a == b); // true
}

Anonymous  Nov 30, 2016 
Printed Page 34
Conditional Operators

On page 34, under "Conditional Operators", it is written:

"The && and || operators test for and and or conditions."

In my opinion, it should be written:

"The && and || operators test for and and or conditions respectively."

Truly yours,

Paul Feher

Paul Feher  Feb 06, 2018 
PDF Page 60
3rd paragraph

You can switch on char too.

Bhavesh  Oct 15, 2016 
Printed Page 74
Listing of modifiers

In Pages 74, 75,80 and 85, the access modifiers listed do not include "protected internal" ('protected" and "internal" are only mentioned separately). However, page 102 mentions that type members can have this access modifier.

Can you please clarify why "protected internal" was not included?

Anonymous  Jan 16, 2017 
PDF Page 123
5th paragraph

The book says -

Calling Wash with a stack of bears would generate a compile-time error. One workaround is to redefine the Wash method with a constraint:

class ZooCleaner
{
public static void Wash<T> (Stack<T> animals) where T : Animal { ... }
}

We can now call Wash as follows:
Stack<Bear> bears = new Stack<Bear>();
ZooCleaner.Wash (bears);

I have tested and found out that there is no need to apply constraint on the Wash<T> function.

Below is my code that works perfectly.

public class Animal { }
public class Bear : Animal { }
public class Camel : Animal { }
public class ZooCleaner
{
public static void Wash<T>(MyStack<T> animals) { }
}

MyStack<Bear> bears = new MyStack<Bear>();
ZooCleaner zClearner = new ZooCleaner();
ZooCleaner.Wash<Bear>(bears);

Thank you.
Yogi

Yogi  Jun 24, 2017 
PDF Page 220
Ordinal versus culture comparison

string[] arr = { "Atom", "atom", "Zamia" };
Array.Sort(arr, StringComparer.InvariantCulture);
foreach (var item in arr)
Console.Write(item + " ");

Output is ordered as:
//atom Atom Zamia

And not:
Atom, atom, Zamia

Bhavesh  Jun 04, 2017 
Printed Page 309
Figure 7-4

The Figure 7-4 shows the inner structure of a LinkedList<T>, with rectangles representing references and arrows representing to where each reference points.

Although there are nulls (correctly) placed before the first item's Previous reference and after the last item's Next reference, the arrows for all Previous and Next references seem to point to the wrong side.

The Next references should point to the right, to the *next* reference (as the name states), not the previous one (to the left). Similarly, Previous references should point to the left, to the *previous* reference, not to the next one (to the right).

Rui Oliveira Pinheiro  Dec 30, 2017 
Printed Page 491
after the last w.WriteEndElement() statement.

There's a line of code missing which means that the the appropriate Xml will not even be printed. The WriteXml(XmlWriter w) method should have the Close() method called as follows:

public void WriteXml(XmlWriter w)
{
foreach (XCustomer c in Customers)
{
w.WriteStartElement(XCustomer.XmlName);
c.WriteXml(w);
w.WriteEndElement();
}

foreach (Supplier s in Suppliers)
{
w.WriteStartElement(Supplier.XmlName);
s.WriterXml(w);
w.WriteEndElement();
}

w.Close();
}

Nicholas Patsaris  Feb 15, 2017 
Printed Page 593
Returning Values -> "...a few seconds later, write the answer of 216815"

I do believe the answer should be 216816.

Using your code:

Task<int> primeNumberTask = Task.Run(() => Enumerable.Range(2, 3000000).Count(n => Enumerable.Range(2, (int)Math.Sqrt(n)-1).All(i => n % i > 0)));

Gives an answer of 216816

I think you accidentally used the code

Task<int> primeNumberTask = Task.Run(() => Enumerable.Range(2, 3000000).Count(n => Enumerable.Range(2, (int)Math.Sqrt(n-1)).All(i => n % i > 0)));

Which does generate 216815

Where instead of doing Math.sqrt(n) you did Math.sqrt(n-1) instead.

Jeffrey Robinson  Apr 17, 2017 
PDF Page 759
-

Quote from the book: "This manufactures a new key pair and stores it to a file called MyApp.snk."

Error: "MyApp.snk" should be "MyKeyPair.snk"

Anonymous  Jun 23, 2017 
PDF Page 910
paragraph starts with "So, you might lock private field a within your class x"

"you might lock private field a within your class x , unaware that your caller (or
caller’s caller) has already locked field b within class y . Meanwhile, another thread is doing the reverse—creating a deadlock. "
************************************************
how "another thread" doing the reverse(lock b while already locked a) while "a" is a private field?
If your class X has a public function that lock a in it(let's say FuncA), then another thread still cannot do it while running this code:
FuncA();
lock(b);
because when FuncA is over, "lock(a) {}" in it is over too.
So i think this scenario is not correct.



Lu Shangfei  Feb 17, 2017