Skip to Content
C# 12 核心要点
book

C# 12 核心要点

by Joseph Albahari
May 2025
Intermediate to advanced
1086 pages
14h 54m
Chinese
O'Reilly Media, Inc.
Content preview from C# 12 核心要点

第 6 章 .NET 基础 .NET基础知识

本作品已使用人工智能进行翻译。欢迎您提供反馈和意见:translation-feedback@oreilly.com

编程时需要的许多核心功能不是由 C# 语言提供的,而是由 .NET BCL 中的类型提供的。在本章中,我们将介绍有助于完成基本编程任务的类型,如虚拟相等比较、顺序比较和类型转换。我们还将介绍基本的 .NET 类型,如StringDateTimeEnum

本节中的类型位于System 命名空间,但以下情况除外:

  • StringBuilder 是在 中定义的,System.Text文本编码的类型也是如此。

  • CultureInfo 和相关类型在 中定义。System.Globalization

  • XmlConvert 在 中定义。System.Xml

字符串和文本处理

Char

C#char 表示单个 Unicode 字符,并别名System.Char 结构。在第 2 章中,我们介绍了如何表达char 字面量:

char c = 'A';
char newLine = '\n';

System.Char 定义了一系列处理字符的静态方法,如 、 和 。您可以通过 类型或其 别名调用这些方法:ToUpper ToLower IsWhiteSpace System.Char char

Console.WriteLine (System.Char.ToUpper ('c'));    // C
Console.WriteLine (char.IsWhiteSpace ('\t'));     // True

ToUpper 和 会尊重最终用户的地域,这可能会导致一些微妙的错误。在土耳其,以下表达式的值为 :ToLower false

char.ToUpper ('i') == 'I'

原因是在土耳其,char.ToUpper ('i') 就是'İ' (注意上面的点!)。为了避免这个问题,System.Char (和System.String )还提供了以 "Invariant"(不变)结尾的文化不变版本ToUpperToLower 。这些版本始终适用英语文化规则:

Console.WriteLine (char.ToUpperInvariant ('i'));    // I

这是一个快捷方式:

Console.WriteLine (char.ToUpper ('i', CultureInfo.InvariantCulture))

有关本地化和文化的更多信息,请参阅"格式化和解析"。

char其余的静态方法大多与字符分类有关。表 6-1列出了这些方法。

表 6-1. 字符分类的静态方法
静态方法 人物包括 统一码类别包括
IsLetter A-Z、a-z 和其他字母表中的字母 UpperCaseLetterLowerCaseLetterTitleCaseLetterModifierLetterOtherLetter
IsUpper 大写字母 UpperCaseLetter
IsLower 小写字母 LowerCaseLetter
IsDigit 0-9 加上其他字母的数字 DecimalDigitNumber
IsLetterOrDigit 字母加数字 (IsLetter,IsDigit)
IsNumber 所有数字加上 Unicode 分数和罗马数字符号 DecimalDigitNumberLetterNumberOtherNumber
IsSeparator ...
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.

Read now

Unlock full access

More than 5,000 organizations count on O’Reilly

AirBnbBlueOriginElectronic ArtsHomeDepotNasdaqRakutenTata Consultancy Services

QuotationMarkO’Reilly covers everything we've got, with content to help us build a world-class technology community, upgrade the capabilities and competencies of our teams, and improve overall team performance as well as their engagement.
Julian F.
Head of Cybersecurity
QuotationMarkI wanted to learn C and C++, but it didn't click for me until I picked up an O'Reilly book. When I went on the O’Reilly platform, I was astonished to find all the books there, plus live events and sandboxes so you could play around with the technology.
Addison B.
Field Engineer
QuotationMarkI’ve been on the O’Reilly platform for more than eight years. I use a couple of learning platforms, but I'm on O'Reilly more than anybody else. When you're there, you start learning. I'm never disappointed.
Amir M.
Data Platform Tech Lead
QuotationMarkI'm always learning. So when I got on to O'Reilly, I was like a kid in a candy store. There are playlists. There are answers. There's on-demand training. It's worth its weight in gold, in terms of what it allows me to do.
Mark W.
Embedded Software Engineer

You might also like

Programming C# 12

Programming C# 12

Ian Griffiths
C# 12 in a Nutshell

C# 12 in a Nutshell

Joseph Albahari
C# 6 for Programmers, Sixth Edition

C# 6 for Programmers, Sixth Edition

Paul Deitel, Harvey Deitel
Head First C#, 4th Edition

Head First C#, 4th Edition

Andrew Stellman, Jennifer Greene

Publisher Resources

ISBN: 9798341657038