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 核心要点

第 3 章 在 C# 中创建类型 用 C# 创建类型

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

在本章中,我们将深入探讨类型和类型成员。

班级

类是最常见的引用类型。最简单的类声明如下:

class YourClassName
{
}

一个更复杂的类可选择具有以下功能:

在关键字之前class 属性类修饰符。非嵌套类修饰符是publicinternal,abstract,sealed,static,unsafe, 和partial
后续行动YourClassName 通用类型参数约束基类接口
括号内 类成员(包括方法属性索引器事件字段构造函数重载操作符嵌套类型终结器)。

除了属性、运算符函数和unsafe 关键字外,本章将介绍所有这些构造,它们将在第 4 章中介绍。以下各节将列举每个类的成员。

字段

字段是作为类或结构体成员的变量,例如

class Octopus
{
  string name;
  public int Age = 10;
}

字段允许使用以下修饰符:

静态修改器 static
访问修改器 public internal private protected
继承修改器 new
不安全代码修改器 unsafe
只读修改器 readonly
穿线修改器 volatile

私有字段有两种流行的命名约定:驼峰形(如firstName )和带下划线的驼峰形 (_firstName)。通过后一种命名方式,可以立即将私有字段与参数和局部变量区分开来。

只读修改器

readonly 修饰符可防止字段在构造后被修改。只读字段只能在其声明中或外层类型的构造函数中分配。

字段初始化

字段初始化是可选的。未初始化的字段有一个默认值(0,'\0',null,false )。字段初始化器在构造函数之前运行:

public int Age = 10;

字段初始化器可以包含表达式和调用方法:

static readonly string TempFolder = System.IO.Path.GetTempPath();

同时声明多个字段

为方便起见,您可以用逗号分隔的列表声明多个相同类型的字段。这样可以方便地让所有字段共享相同的属性和字段修饰符:

static readonly int legs = 8,
                    eyes = 2;

常数

常量在编译时进行静态评估,编译器会在使用时替换常量的值(类似于 C++ 中的宏值)。常量可以是bool,char,string, 任何内置数字类型或枚举类型。

常量用const 关键字声明,必须用一个值进行初始化。例如

public class Test
{
  public const string Message = "Hello World";
}

常量的作用与static readonly 字段类似,但它在可使用的类型和字段初始化语义方面限制更多。常量与static readonly 字段的区别还在于,常量的评估是在编译时进行的,因此

public static double Circumference (double radius)
{
  return 2 * System.Math.PI * radius;
}

编译为

public static double Circumference (double radius) { return 6.2831853071795862 ...
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