惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

酷 壳 – CoolShell
酷 壳 – CoolShell
H
Hacker News: Front Page
P
Palo Alto Networks Blog
T
ThreatConnect
Apple Machine Learning Research
Apple Machine Learning Research
博客园_首页
T
True Tiger Recordings
P
Privacy & Cybersecurity Law Blog
B
Blog
IT之家
IT之家
Last Week in AI
Last Week in AI
F
Full Disclosure
Hacker News: Ask HN
Hacker News: Ask HN
C
Comments on: Blog
Microsoft Azure Blog
Microsoft Azure Blog
C
Cybersecurity and Infrastructure Security Agency CISA
Microsoft Security Blog
Microsoft Security Blog
博客园 - 【当耐特】
N
News and Events Feed by Topic
NISL@THU
NISL@THU
腾讯CDC
雷峰网
雷峰网
Security Latest
Security Latest
李成银的技术随笔
M
Microsoft Research Blog - Microsoft Research
L
LangChain Blog
L
Lohrmann on Cybersecurity
cs.CL updates on arXiv.org
cs.CL updates on arXiv.org
C
Check Point Blog
Y
Y Combinator Blog
Recent Announcements
Recent Announcements
博客园 - Franky
N
News | PayPal Newsroom
V
V2EX
A
About on SuperTechFans
The Register - Security
The Register - Security
月光博客
月光博客
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
Google Online Security Blog
Google Online Security Blog
MyScale Blog
MyScale Blog
Cisco Talos Blog
Cisco Talos Blog
Vercel News
Vercel News
WordPress大学
WordPress大学
C
Cyber Attacks, Cyber Crime and Cyber Security
The Hacker News
The Hacker News
IntelliJ IDEA : IntelliJ IDEA – the Leading IDE for Professional Development in Java and Kotlin | The JetBrains Blog
IntelliJ IDEA : IntelliJ IDEA – the Leading IDE for Professional Development in Java and Kotlin | The JetBrains Blog
爱范儿
爱范儿
A
Arctic Wolf
L
LINUX DO - 最新话题
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More

博客园 - 世纪末の魔术师

坚毅,是一种缓慢修理自己的方式 语言的边界,与软件的命运 人类与AI协同进化 《坚毅》第一部分读书笔记 用 System.CommandLine 构建工程级 CLI 工具 用 Command 模式构建可扩展的命令行工具 从哎呦”到语言宇宙 ——读《What Is ChatGPT Doing … And Why Does It Work?》 ⏱️ 深入理解定时器中的【时间轮算法】 🚫 为什么「定时器」不应该是线程安全的? C# AOT编译后——调用其类库方法因顺序出错? UnitTask中的Forget()与 CTS 光线追踪和球体追踪 二十、异常与状态管理(Exception&State Management) 二十八、IO绑定的异步操作(IO-Bound Async) 二十二、CLR寄宿与AppDomain(CLR Hosting and App Domains ) 二十九、原始线程同步构造(Primitive Thread Synchronization Constructs ) 二十六、线程与并发(Thread Basic) 二十七、计算密集型异步操作(Compute-Bound Asynchronous Operations) 二十三、程序集加载与反射(Assembly Loading and Reflection)
八、方法(method)
世纪末の魔术师 · 2025-08-26 · via 博客园 - 世纪末の魔术师

CLR via C# 第八章总结:方法(Methods)

📌 1. 方法定义基础

  • 方法是类行为的表现,用于封装逻辑。
  • 语法结构:
[访问修饰符] [static] [返回类型] 方法名([参数列表])
{
    // 方法体
}

📌 2. 参数传递方式

✅ 值传递(默认):

方法获得的是变量的副本,不影响原值。

void Foo(int x) { x++; }

✅ 引用传递(ref):

方法可修改原值,必须在调用前初始化。

void Foo(ref int x) { x++; }

✅ 输出参数(out):

方法内部必须赋值,调用时可未初始化。

void Bar(out int x) { x = 10; }

✅ 可变参数(params):

接收不定数量的参数,等价数组。

void Print(params int[] numbers) { ... }

📌 3. 局部函数(Local Functions)

  • 可定义在另一个方法内部,仅供内部使用
void Outer() {
    void Inner() { Console.WriteLine("内部方法"); }
    Inner();
}

📌 4. 可选参数 & 命名参数

✅ 可选参数

提供默认值:

void Greet(string name = "Guest") { ... }

✅ 命名参数

允许按名称传参,顺序无关:

Greet(name: "Alice");

📌 5. 方法重载(Overloading)

  • 相同方法名,参数签名不同
void Print(int x) { ... }
void Print(string s) { ... }

📌 Mermaid 图示:方法概念总览

graph TD A[方法] --> B[值传递] A --> C[ref] A --> D[out] A --> E[params] A --> F[局部函数] A --> G[重载] A --> H[可选/命名参数]

🧠 面试题 & 解析(不少于5题)

1️⃣ ref vs out 有何区别?

ref 要求变量已初始化out 不需要;方法中 out 必须赋值

2️⃣ params 参数的使用限制?

  • 必须是最后一个参数
  • 只能有一个 params
  • 不能与 ref/out 共用

3️⃣ voidTaskTask<T> 区别?

void 普通同步方法 Task 异步方法无返回 Task<T> 异步方法有返回值
返回类型 用途

4️⃣ 方法重载 vs 方法重写?

重载 同类中方法名相同、参数不同 无需关键词 重写 子类中重写父类虚方法 virtual + override
概念 作用 关键词

5️⃣ 局部函数用途?

  • 封装仅在该方法内部使用的小逻辑
  • 减少代码冗余,提高代码可读性

✅ 总结表格

参数传递方式 refoutparams 方法重载 相同方法名、参数签名不同 局部函数 方法内部定义方法 可选/命名参数 提高调用灵活性、可读性 返回类型设计 voidTaskTask<T>
概念 关键点