C# 6.0 in a Nutshell: The Definitive Reference, 6th Edition

C# 6.0 in a Nutshell: The Definitive Reference, 6th Edition

English | 2015 | ISBN: 978-1-4919-2706-9 | 1136 Pages | PDF, EPUB | 12 MB


When you have a question about C# 6.0 or the .NET CLR, this bestselling guide has precisely the answers you need. Uniquely organized around concepts and use cases, this updated sixth edition includes completely revised and updated information on all the new C# 6.0 language features.
Shaped by expert reviewers, this book has all you need to stay on track with C# 6.0. It’s widely known as the definitive reference on the language.

  • Get up to speed on C# language basics, including syntax, types, and variables
  • Explore advanced topics such as unsafe code and type variance
  • Dig deep into LINQ via three chapters dedicated to the topic
  • Learn about code contracts, dynamic programming, and parallel programming
  • Work with .NET features, including reflection, assemblies, memory management, security, I/O, XML, collections, networking, and native interoperability
+

In the first phase, a parser reads C# code and outputs syntax trees. A syntax tree is a DOM (Document Object Model) that describes source code in tree structure.

The second phase is where C#’s static binding takes place. Assembly references are read, and the compiler figures out, for instance, that “Console” refers to System.Console in mscorlib.dll. Overload resolution and type inference are a part of this, too.

The third phase produces the output assembly. If you plan to use Roslyn for code analysis or refactoring, you won’t use this functionality.

Visual Studio’s editor uses the output of the syntactic layer to color keywords, strings, comments, and disabled code (in blue, red, green, and gray, respectively), whereas it uses the output of the semantic layer to color resolved type names (in turquoise).

A syntax tree is a DOM for source code. The syntax tree API is completely separate from the System.Linq.Expressions API we discussed in “Expression Trees” on page 387 in Chapter 8, although the two have conceptual similarities. Both APIs can represent C# expressions in a DOM; however, a Roslyn syntax tree has the following unique features:

  • It can represent the entire C# language, not just expressions.
  • It can include comments, whitespace, and other “trivia,” and can round-trip with full fidelity back to the original source code.
  • It comes with a ParseText method that parses source code into a syntax tree.

Conversely, the System.Linq.Expressions API has the following unique features:

  • It’s built into the .NET Framework, and the C# compiler itself is programmed to emit System.Linq.Expression types when it encounters a lambda expression with an assignment conversion to Expression<T>.
  • It has a fast and lightweight Compile method that emits a delegate. In contrast, the semantic layer that compiles Roslyn syntax trees offers only the heavy‐weight option of compiling a complete program into an assembly.

Something that both APIs have in common is that syntax trees are immutable, so none of its elements can be altered once created. This means that applications such as Visual Studio and LINQPad must create a new syntax tree each time you press a key in the editor in order to update syntax highlighting and autocompletion services. This is less expensive than it sounds because the new syntax tree is able to reuse most of the elements of the old. And knowing that an object cannot change makes the API simpler to work with. It also allows for easier and faster parallelization, since multithreaded code can safely access all parts of a syntax tree without locks.