Share E-Book
Scan to open this page

Scan with your phone to open this page

AuthorJoseph Albahari, Ben Albahari

Looking for quick answers for using C# 12? This tightly focused and practical guide tells you exactly what you need to know without long intros or bloated samples. Succinct and easy to browse, this pocket reference is an ideal quick source of information. If you know Java, C++, or an earlier C# version, this guide will help you get rapidly up to speed. All programs and code snippets are available as interactive samples in LINQPad. You can edit these samples and instantly see the results without needing to set up projects in Visual Studio.

AI Reading Assistant

Whole-book reading guide from stratified index samples; jump to passages in the text

AI guide
【One-Line Pitch】 A compact, no-fluff reference for C# 12 that gets you up to speed fast if you already know Java, C++, or an earlier C# version—ideal for quick lookups and hands-on practice with LINQPad samples. 【Book Arc】 - **Opening (~0%–10%)**: Introduces C# 12 as a type-safe, object-oriented language built for .NET 8, then covers the absolute basics: reserved keywords (and how to escape them with `@`), contextual keywords, and the symmetry between predefined types (like `int`) and custom types (like a `UnitConverter` class). This stage solves the "where do I start?" problem by grounding you in syntax and type fundamentals. - **Early (~10%–23%)**: Dives into numeric types (int, double, decimal, float), including literal inference, suffixes, and special values like NaN and infinity—plus the critical `double` vs `decimal` distinction for scientific vs financial work. Then moves to strings: escape sequences, verbatim literals (`@"..."`), and raw string literals (`"""..."""`) for JSON/XML. This stage builds your data-handling toolkit. - **Early (~23%–32%)**: Covers variables and parameters in depth: passing by value vs by reference (`ref`), implicitly typed variables (`var`), and target-typed `new` expressions (C# 9+). Then shifts to operators and control flow—switch expressions (including tuple switching), and iteration statements like `while`, `do-while`, `for`, and `foreach`. This stage makes you fluent in everyday C# syntax. - **Early (~32%–39%)**: Explains namespaces (hierarchical, independent of assemblies) and classes—the core reference type. Covers class modifiers, fields (including `readonly`), constructors (including nonpublic ones for controlled instantiation), and deconstructors (C# 7+) that reverse constructor logic. This stage is the heart of object-oriented C#. - **Middle (~39%–48%)**: Explores advanced class features: primary constructors (C# 12) with their limitations, partial types and methods for splitting definitions across files (great for autogenerated code), and inheritance—upcasting/downcasting, reference conversions, and sealing functions/classes. Also covers boxing/unboxing and struct construction semantics, including the implicit parameterless constructor and `default` keyword. This stage rounds out your OOP mastery. 【Key Takeaways】 - **Keywords and identifiers** (Early): Reserved keywords can be used as identifiers with the `@` prefix (e.g., `@class`), and contextual keywords (like `record`) don't need it. This saves you from naming headaches in real code. - **Numeric literal rules** (Early): Literals with a decimal point or `E` are `double`; otherwise, the compiler picks the first fitting type from `int`, `uint`, `long`, `ulong`. Suffixes like `M` for `decimal` give explicit control—critical for precision-sensitive code. - **Special float/double values** (Early): NaN, +∞, −∞, and −0 behave unexpectedly: NaN never equals itself with `==` (use `double.IsNaN`), but `object.Equals` treats two NaNs as equal. This prevents subtle bugs in scientific computing. - **String literal options** (Early): Verbatim strings (`@"..."`) avoid backslash escaping, and raw string literals (`"""..."""`) handle JSON/XML/regex without escaping at all—huge for readability in modern C#. - **Passing by reference** (Early): The `ref` modifier makes parameters share memory with the caller's variable, so changes persist. Without it, reference types copy the reference (not the object), so setting a parameter to `null` doesn't affect the original—a common source of confusion. - **Target-typed `new`** (Early): `StringBuilder sb = new();` infers the type from the declaration, reducing repetition, especially when initializing fields in constructors. It's a C# 9+ feature that makes code cleaner. - **Primary constructors (C# 12)** (Middle): They simplify simple classes but have limits—no extra initialization code, and validation logic is hard unless properties are read-only. They also displace the default parameterless constructor, so use them deliberately. - **Struct construction semantics** (Middle): Even with custom parameterless constructors (C# 11+), the implicit one still exists via `default`, which bitwise-zeroes fields. This lets you bypass initializers—useful for arrays and performance-sensitive scenarios. 【Reading Tips】 - **Skim the keyword lists and operator tables** (Early): These are reference material, not prose—scan them when you need a quick lookup, don't read them cover-to-cover. - **Deep-read the numeric and string sections** (Early): These have subtle rules (NaN, literal inference, raw strings) that trip up even experienced devs. Work through the LINQPad samples to see results instantly. - **Focus on the `ref` and `var` examples** (Early): They're short but dense; trace through the code mentally to internalize how memory and type inference work. - **Pay extra attention to primary constructors and partial types** (Middle): These are newer C# features with specific trade-offs—understand their limitations before using them in production. - **Use the book as a lookup, not a tutorial**: It's a pocket reference, so jump to the section you need (e.g., "Inheritance" for casting rules) rather than reading sequentially. 【Coverage Limits】 This guide covers the core language up to structs and inheritance, but the excerpts don't include later chapters on records, tuples, LINQ, or async/await—those are beyond the sampled material.
Excerpt 1
checked const implicit record unsafe continue in ref ushort decimal int return using default interface sbyte virtual delegate internal sealed void do is shor...
View in text
Excerpt 2
1\r\nLine 2" as follows: string multiLineRaw = """ Line 1 Line 2 30 | C# 12 Pocket Reference All array indexing is bounds-checked by the runtime. ...
View in text
Excerpt 3
A class is the most common kind of reference type. The sim‐ plest possible class declaration is as follows: class Foo { } A more complex class optionally has...
View in text
Excerpt 4
by further subclasses. In our earlier virtual function mem‐ ber example, we could have sealed House’s implementation of Liability, preventing a class that de...
View in text
Excerpt 5
m the left delegate operand. For example: d -= SomeMethod1; Invoking d will now cause only SomeMethod2 to be invoked. Calling + or += on a delegate variable ...
View in text
Excerpt 6
e, the compiler will generate an error): System.Collections.IEnumerable System.Collections.IEnumerator System.Collections.Generic.IEnumerable<T> System.Colle...
View in text
Excerpt 7
ifference Element Picks a single element from a sequence No Aggregation Performs a computation over a sequence, No returning a scalar value (typically a numb...
View in text
Excerpt 8
em.Text.StringBuilder(); dynamic d = o; d.Append ("hello"); Console.WriteLine (o); // hello Dynamic Conversions The dynamic type has implicit conversions t...
View in text
Tags
AI categories
Programming LanguageProgrammingTechnology
Publisher: O'Reilly Media
Publish Year: 2023
Language: English
File Format: PDF
File Size: 1.8 MB
Text Preview (First 20 pages)
Registered users can read the full content for free

Register as a Gaohf Library member to read the complete e-book online for free and enjoy a better reading experience.

Generating text preview…