C# 9.0 trong một cái nhìn tổng quan - Joseph Albahari

Khám phá nội dung chính của cuốn sách "C# 9.0 in a Nutshell" của Joseph Albahari trên Oceanofpdf, nơi cung cấp kiến thức lập trình hữu ích.

Trường đại học

O’Reilly Media, Inc.

Chuyên ngành

C# Programming

Người đăng

Ẩn danh

Thể loại

book

2021

1.7K
2
0

Phí lưu trữ

30 Point

Mục lục chi tiết

Copyright

Intended Audience

How This Book Is Organized

What You Need to Use This Book

Conventions Used in This Book

Using Code Examples

We’d Like to Hear from You

O’Reilly Online Learning

Acknowledgments

1. NET C#

1.1. Object Orientation

1.2. Type Safety

1.3. Memory Management

1.4. Platform Support

1.5. CLRs, BCLs, and Runtimes

1.6. Runtime architecture Common Language Runtime

1.7. Base Class Library

1.8. Runtimes

Tóm tắt

I. Tổng quan về C 9

C# 9.0 là phiên bản mới nhất của ngôn ngữ lập trình C#, được phát triển bởi Joseph Albahari. Phiên bản này mang đến nhiều tính năng mới và cải tiến, giúp lập trình viên dễ dàng hơn trong việc phát triển ứng dụng. C# 9.0 không chỉ đơn thuần là một ngôn ngữ lập trình, mà còn là một công cụ mạnh mẽ cho việc xây dựng các ứng dụng hiện đại.

1.1. Các tính năng nổi bật của C 9.0

C# 9.0 giới thiệu nhiều tính năng mới như 'record types', 'init-only properties', và 'top-level statements'. Những tính năng này giúp lập trình viên viết mã ngắn gọn và dễ hiểu hơn, đồng thời tăng cường khả năng bảo trì mã nguồn.

1.2. Lợi ích của việc sử dụng C 9.0

Việc sử dụng C# 9.0 mang lại nhiều lợi ích cho lập trình viên, bao gồm tăng năng suất, giảm thiểu lỗi và cải thiện khả năng đọc hiểu mã. Điều này giúp các dự án phát triển phần mềm trở nên hiệu quả hơn.

II. Những thách thức khi chuyển sang C 9

Mặc dù C# 9.0 mang lại nhiều cải tiến, nhưng việc chuyển đổi từ các phiên bản trước đó cũng gặp phải một số thách thức. Lập trình viên cần làm quen với các tính năng mới và cách thức hoạt động của chúng.

2.1. Khó khăn trong việc học các tính năng mới

Nhiều lập trình viên có thể gặp khó khăn trong việc nắm bắt các tính năng mới như 'record types' và 'init-only properties'. Việc này đòi hỏi thời gian và nỗ lực để làm quen.

2.2. Tương thích với mã nguồn cũ

Một thách thức lớn khác là đảm bảo mã nguồn cũ vẫn hoạt động tốt với C# 9.0. Việc này có thể yêu cầu lập trình viên phải điều chỉnh mã để tương thích với các thay đổi trong ngôn ngữ.

III. Phương pháp tối ưu hóa mã nguồn với C 9

Để tận dụng tối đa C# 9.0, lập trình viên cần áp dụng một số phương pháp tối ưu hóa mã nguồn. Những phương pháp này không chỉ giúp cải thiện hiệu suất mà còn làm cho mã dễ bảo trì hơn.

3.1. Sử dụng record types để giảm thiểu mã lặp

'Record types' cho phép lập trình viên định nghĩa các kiểu dữ liệu một cách ngắn gọn và hiệu quả. Điều này giúp giảm thiểu mã lặp và tăng cường khả năng bảo trì.

3.2. Tận dụng init only properties để bảo vệ dữ liệu

Việc sử dụng 'init-only properties' giúp bảo vệ dữ liệu trong các đối tượng, đảm bảo rằng chúng chỉ có thể được thiết lập một lần. Điều này làm tăng tính an toàn cho mã nguồn.

IV. Ứng dụng thực tiễn của C 9

C# 9.0 đã được áp dụng rộng rãi trong nhiều lĩnh vực phát triển phần mềm, từ ứng dụng web đến ứng dụng di động. Những tính năng mới của nó giúp lập trình viên phát triển các ứng dụng hiện đại và hiệu quả hơn.

4.1. C 9.0 trong phát triển ứng dụng web

C# 9.0 được sử dụng phổ biến trong phát triển ứng dụng web với ASP.NET Core. Những tính năng mới giúp lập trình viên xây dựng các ứng dụng web nhanh chóng và dễ dàng hơn.

4.2. C 9.0 trong phát triển ứng dụng di động

C# 9.0 cũng được áp dụng trong phát triển ứng dụng di động thông qua Xamarin. Điều này cho phép lập trình viên viết mã một lần và triển khai trên nhiều nền tảng khác nhau.

V. Kết luận và tương lai của C 9

C# 9.0 không chỉ là một bước tiến lớn trong ngôn ngữ lập trình mà còn mở ra nhiều cơ hội mới cho lập trình viên. Tương lai của C# 9.0 hứa hẹn sẽ còn nhiều điều thú vị với các tính năng và cải tiến mới.

5.1. Dự đoán về các phiên bản tiếp theo của C

Các phiên bản tiếp theo của C# có thể sẽ tiếp tục cải tiến tính năng và hiệu suất, đồng thời mở rộng khả năng tương thích với các công nghệ mới.

5.2. Tầm quan trọng của C trong ngành công nghiệp phần mềm

C# vẫn giữ vai trò quan trọng trong ngành công nghiệp phần mềm, nhờ vào tính linh hoạt và khả năng mở rộng của nó. Điều này giúp C# duy trì vị thế của mình trong tương lai.

15/07/2025

Trích đoạn nội dung tài liệu

0 in a Nutshell The Definitive Reference Joseph Albahari C# 9.0 in a Nutshell by Joseph Albahari Copyright © 2021 Joseph Albahari. All rights reserved. Printed in the United States of America. Published by O’Reilly Media, Inc., 1005 Gravenstein Highway North, Sebastopol, CA 95472.

O’Reilly books may be purchased for educational, business, or sales promotional use. Online editions are also available for most titles (http://oreilly. For more information, contact our corporate/institutional sales department: 800-998-9938 or corporate@oreilly. Acquisitions Editor: Amanda Quinn Development Editor: Corbin Collins Production Editor: Kristen Brown Copyeditor: Charles Roumeliotis Proofreader: Piper Editorial Consulting, LLC Indexer: WordCo Indexing Services, Inc.

Interior Designer: David Futato Cover Designer: Karen Montgomery Illustrator: Kate Dullea March 2021: First Edition Revision History for the First Edition 2021-02-26: First Release See http://oreilly.com/catalog/errata.csp?isbn=9781098100964 for release details. The O’Reilly logo is a registered trademark of O’Reilly Media, Inc.0 in a Nutshell, the cover image, and related trade dress are trademarks of O’Reilly Media, Inc. The views expressed in this work are those of the author, and do not represent the publisher’s views. While the publisher and the author have used good faith efforts to ensure that the information and instructions contained in this work are accurate, the publisher and the author disclaim all responsibility for errors or omissions, including without limitation responsibility for damages resulting from the use of or reliance on this work.

Use of the information and instructions contained in this work is at your own risk. If any code samples or other technology this work contains or describes is subject to open source licenses or the intellectual property rights of others, it is your responsibility to ensure that your use thereof complies with such licenses and/or rights.0 represents the eighth major update to Microsoft’s flagship programming language, positioning C# as a language with unusual flexibility and breadth. At one end, it offers high-level abstractions such as query expressions and asynchronous continuations, whereas at the other end, it allows low-level efficiency through constructs such as custom value types and optional pointers. The price of this growth is that there’s more than ever to learn.

Although tools such as Microsoft’s IntelliSense—and online references—are excellent in helping you on the job, they presume an existing map of conceptual knowledge. This book provides exactly that map of knowledge in a concise and unified style—free of clutter and long introductions. Like the past six editions, C# 9.0 in a Nutshell is organized around concepts and use cases, making it friendly both to sequential reading and to random browsing. It also plumbs significant depths while assuming only basic background knowledge, making it accessible to intermediate as well as advanced readers.

This book covers C#, the Common Language Runtime (CLR), and the .NET 5 Base Class Library (BCL). We’ve chosen this focus to allow space for difficult and advanced topics without compromising depth or readability. Features recently added to C# are flagged so that you can also use this book as a reference for C# 8 and C# 7. Intended Audience This book targets intermediate to advanced audiences.

No prior knowledge of C# is required, but some general programming experience is necessary. For the beginner, this book complements, rather than replaces, a tutorial- style introduction to programming. This book is an ideal companion to any of the vast array of books that focus on an applied technology such as ASP.NET Core or Windows Presentation Foundation (WPF).0 in a Nutshell covers the areas of the language and .NET that such books omit, and vice versa. If you’re looking for a book that skims every .NET technology, this is not for you.

This book is also unsuitable if you want to learn about APIs specific to mobile device development. How This Book Is Organized Chapters 2 through 4 concentrate purely on C#, starting with the basics of syntax, types, and variables, and finishing with advanced topics such as unsafe code and preprocessor directives. If you’re new to the language, you should read these chapters sequentially. The remaining chapters focus on .NET 5’s Base Class Libraries, covering such topics as Language-Integrated Query (LINQ), XML, collections, concurrency, I/O and networking, memory management, reflection, dynamic programming, attributes, cryptography, and native interoperability.

You can read most of these chapters randomly, except for Chapters 5 and 6, which lay a foundation for subsequent topics. You’re also best off reading the three chapters on LINQ in sequence, and some chapters assume some knowledge of concurrency, which we cover in Chapter 14. What You Need to Use This Book The examples in this book require. You will also find Microsoft’s .NET documentation useful to look up individual types and members (which is available online).

Although it’s possible to write source code in Notepad and build your program from the command line, you’ll be much more productive with a code scratchpad for instantly testing code snippets, plus an integrated development environment (IDE) for producing executables and libraries. For a Windows code scratchpad, download LINQPad 6 from www. LINQPad fully supports C# 9.0 and is maintained by one of the authors. For a Windows IDE, download Visual Studio 2019: any edition is suitable for what’s taught in this book.

For a cross-platform IDE, download Visual Studio Code. NOTE All code listings for all chapters are available as interactive (editable) LINQPad samples. You can download the entire lot in a single click: at the bottom left, click the LINQPad’s Samples tab, click “Download more samples,” and then choose “C# 9.” Conventions Used in This Book The book uses basic UML notation to illustrate relationships between types, as shown in Figure P-1. A slanted rectangle means an abstract class; a circle means an interface.

A line with a hollow triangle denotes inheritance, with the triangle pointing to the base type. A line with an arrow denotes a one- way association; a line without an arrow denotes a two-way association. Sample diagram The following typographical conventions are used in this book: Italic Indicates new terms, URIs, filenames, and directories Constant width Indicates C# code, keywords and identifiers, and program output Constant width bold Shows a highlighted section of code Constant width italic Shows text that should be replaced with user-supplied values NOTE This element signifies a general note. WARNING This element indicates a warning or caution.

Using Code Examples Supplemental material (code examples, exercises, etc.) is available for download at http://www. If you have a technical question or a problem using the code examples, please send email to bookquestions@oreilly. This book is here to help you get your job done. In general, if example code is offered with this book, you may use it in your programs and documentation.

You do not need to contact us for permission unless you’re reproducing a significant portion of the code. For example, writing a program that uses several chunks of code from this book does not require permission. Selling or distributing examples from O’Reilly books does require permission. Answering a question by citing this book and quoting example code does not require permission.

Incorporating a significant amount of example code from this book into your product’s documentation does require permission. We appreciate, but generally do not require, attribution. An attribution usually includes the title, author, publisher, and ISBN.0 in a Nutshell by Joseph Albahari (O’Reilly). Copyright 2021 Joseph Albahari, 978-1-098-10096-4.” If you feel your use of code examples falls outside fair use or the permission given above, feel free to contact us at permissions@oreilly.

We’d Like to Hear from You Please address comments and questions concerning this book to the publisher: O’Reilly Media, Inc. 1005 Gravenstein Highway North Sebastopol, CA 95472 800-998-9938 (in the United States or Canada) 707-829-0515 (international or local) 707-829-0104 (fax) We have a web page for this book, where we list errata, examples, and any additional information. You can access this page at: https://oreil.ly/c-sharp-nutshell-9 Code listings and additional resources are provided at: http://www.com/nutshell Email bookquestions@oreilly.com to comment or ask technical questions about this book. For news and information about our books and courses, visit http://oreilly.

Find us on Facebook: http://facebook.com/oreilly Follow us on Twitter: http://twitter.com/oreillymedia Watch us on YouTube: http://youtube.com/oreillymedia O’Reilly Online Learning NOTE For more than 40 years, O’Reilly Media has provided technology and business training, knowledge, and insight to help companies succeed. Our unique network of experts and innovators share their knowledge and expertise through books, articles, and our online learning platform. O’Reilly’s online learning platform gives you on-demand access to live training courses, in-depth learning paths, interactive coding environments, and a vast collection of text and video from O’Reilly and 200+ other publishers. For more information, please visit http://oreilly.

Acknowledgments As always, it’s been an honor to have superb technical reviewers on the team. I’d like to extend particular thanks to Fred Silberberg and Stephen Toub from Microsoft for providing invaluable feedback at a busy time. The book was built on previous editions, whose technical reviewers I owe a similar honor: in recent editions, Vitek Karas, Stephen Toub, Paulo Morgado, Aaron Robinson, Jan Vorlicek, Sam Gentile, Rod Stephens, Jared Parsons, Matthew Groves, Dixin Yan, Lee Coward, Bonnie DeWitt, Wonseok Chae, Lori Lalonde, and James Montemagno. And previously, Eric Lippert, Jon Skeet, Stephen Toub, Nicholas Paldino, Chris Burrows, Shawn Farkas, Brian Grunkemeyer, Maoni Stephens, David DeWinter, Mike Barnett, Melitta Andersen, Mitch Wheat, Brian Peek, Krzysztof Cwalina, Matt Warren, Joel Pobar, Glyn Griffiths, Ion Vasilian, Brad Abrams, and Adam Nathan.

I appreciate that many of the technical reviewers are accomplished individuals at Microsoft, and I particularly thank you for taking the time to raise this book to the next quality bar. I want to thank Ben Albahari and Eric Johannsen, who contributed to previous editions, and the O’Reilly team—particularly my efficient and responsive editor Corbin Collins. Finally, my deepest thanks to my wonderful wife, Li Albahari, whose presence kept me happy throughout the project.NET C# is a general-purpose, type-safe, object-oriented programming language. The goal of the language is programmer productivity.

To this end, C# balances simplicity, expressiveness, and performance. The chief architect of the language since its first version is Anders Hejlsberg (creator of Turbo Pascal and architect of Delphi). The C# language is platform neutral and works with a range of platform-specific runtimes. Object Orientation C# is a rich implementation of the object-orientation paradigm, which includes encapsulation, inheritance, and polymorphism.

Encapsulation means creating a boundary around an object to separate its external (public) behavior from its internal (private) implementation details. Following are the distinctive features of C# from an object-oriented perspective: Unified type system The fundamental building block in C# is an encapsulated unit of data and functions called a type. C# has a unified type system in which all types ultimately share a common base type. This means that all types, whether they represent business objects or are primitive types such as numbers, share the same basic functionality.

For example, an instance of any type can be converted to a string by calling its ToString method. Classes and interfaces In a traditional object-oriented paradigm, the only kind of type is a class. In C#, there are several other kinds of types, one of which is an interface. An interface is like a class that cannot hold data.

This means that it can define only behavior (and not state), which allows for multiple inheritance as well as a separation between specification and implementation. Properties, methods, and events In the pure object-oriented paradigm, all functions are methods. In C#, methods are only one kind of function member, which also includes properties and events (there are others, too). Properties are function members that encapsulate a piece of an object’s state, such as a button’s color or a label’s text.

Events are function members that simplify acting on object state changes. Although C# is primarily an object-oriented language, it also borrows from the functional programming paradigm, specifically: Functions can be treated as values Using delegates, C# allows functions to be passed as values to and from other functions.

Nội dung được bảo vệ bản quyền — Tải xuống đầy đủ