读书笔记: C# 7.0 in a nutshell (第 三 章 Creating Types in C#)

本文详细介绍了C#中创建类型的各个方面,包括类的定义、字段、方法、构造器、deconstructor、对象初始化、this关键字、属性、索引器、常量、静态构造器、静态类、finalizer、partial类、方法、继承、多态、装箱拆箱、静态和运行时类型检查、struct、访问修饰符、接口、枚举、嵌套类型以及泛型的使用和约束。内容涵盖C#的方方面面,是学习C#类型创建的重要参考资料。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

内容:

第三章: C#中创建类型

  1. 继承
  2. object类型
  3. struct
  4. 访问修饰符
  5. 接口
  6. 枚举
  7. 嵌套类
  8. 泛型

1. Classes

典型的class定义:

class YourClassName
{ }

除此之外:

位置 内容
class前 Attributes and class modifiers, 非嵌套类modifiers包括:
public,internal, abstract, sealed, static, unsafe, partial
类名之后 泛型类,基类,接口
大括号内 类成员,包括:
methods, properties, indexers, events, fields, constructors,overloaded operators, nested types, and a finalizer

1.1 字段

a.字段修饰符:
名称 修饰符
Static modifier static
Access modifiers public internal private protected
Inheritance modifier new
Unsafe code modifier unsafe
Read-only modifier readonly
Threading modifier volatile
b.readonly修饰符
static readonly int legs = 8, eyes = 2;   // 使用逗号定义多个

1.2 方法

a.方法修饰符:
名称 修饰符
Static modifier static
Access modifiers public internal private protected
Inheritance modifier new virtual abstract override sealed
Partial method modifier partial
Unmanaged code modifiers unsafe extern
Asynchronous code modifier async
b.Expression-bodied method

当只包含一条语句的时候:

int Foo (int x) => x * 2;
void Foo (int x) => Console.WriteLine (x);
c.方法重载

函数签名:函数名以及它对应的参数类型顺序(参数名不算, params也不算, 但是ref/out算)

当函数签名不同的时候,就可以重载; 重载中,参数是值传递还是引用传递也可以重载,比如:
Foo(int)可以和Foo(ref int)或者Foo(out int)共存,但是 Foo(ref int)Foo(out int)不可以共存。

d.local method

局部 函数只能被嵌套的函数看到。局部函数可以出现在 setter, 构造器, lambda表达式中。

不能使用static修饰局部方法,如果外部方法是static的,那么局部函数就会自动成为static

void WriteCubes()
{
Console.WriteLine (Cube (3));
Console.WriteLine (Cube (4));
Console.WriteLine (Cube (5));
int Cube (int value) => value * value * value;
}

1.3 构造器

class或者struct都有构造器。

a.构造器的修饰符:
名称 修饰符
Access Modifiers public internal private protected
unmanged code modifiers unsafe extern



从C#7.0开始,构造器也可以使用 expression-bodied:

public Panda (string n) => name = n;
b.调用其他构造器

this关键字和base关键字。被调用的会先执行

public class Wine{
    public Wine (decimal price) { Price = price; }
    public Wine (decimal price, int year) : this (price) { Year = year; }   //被调用的会先执行
}

传入构造器的参数可以是表达式,但是不能调用this,因为此时instance自身都没有创建;不过没有使用static方法。

c.隐式无参public构造器
d.字段初始化顺序

字段的初始化是比构造器初始化更早的,并且按照他们在class中的定义顺序

e.非public构造器

1.4 Deconstructor(C# 7.0)

把一个对象的结果返回给多个数据。Deconstructor也可以重载

class Rectangle
{
    public readonly float Width, Height;

    public Rectangle (float width, float height)
    {
        Width = width;
        Height = height;
    }
    ///deconstructor
    public void Deconstruct (out float width, out float height)
    {
        width = Width;
        height = Height;
    }
}
var rect = new Rectangle (3, 4);
(float width, float height) = rect; // Deconstruction
相当于:
float width, height;
rect.Deconstruct (out width, out height);

1.5 Object Initializers

Bunny b1 = new Bunny { Name="Bo", LikesCarrots=true, LikesHumans=false };
//相当于
Bunny temp1 = new Bunny();      // temp1 is a compiler-generated name
temp1.Name = "Bo";
temp1.LikesCarrots = true;
temp1.LikesHumans = false;
Bunny b1 = temp1;

编译产生了一个临时变量,这就保证初始化中如果出现错误,不会存在一个初始化了一半的对象。

1.6 this

1.7 Properties

a.修饰符
名称 修饰符
Static modifier static
Access modifiers public internal private protected
Inheritance modifiers new virtual abstract override sealed
Unmanaged code modifiers unsafe extern
b.read-only property
c. expression-bodied properties
decimal currentPrice, sharesOwned;

public decimal Worth => currentPrice * sharesOwned;   //get
public decimal Worth
{
    get => currentPrice * sharesOwned;
    set => sharesOwned = value / currentPrice;
}
d.自动属性

编译器自动会产生一个我们无法访问到的字段。

public class Stock
{
    ...
    
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值