C#参数使用场景简要说明

C#参数使用场景简要说明

1、传值参数

方法、类成员的初始化

2、输出参数

方法返回值不能满足,需要多个返回值时;

3、引用参数

方法需要修改变量需带回原变量时;

4、具名参数

代码可读性高,参数可交换位置

5、方法扩展(this参数)

方法扩展时,目前不存在满足的方法

csharp 复制代码
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Linq;
//this参数(扩展方法)
namespace PamametersExample9
{
    class Program
    {
        static void Main(string[] args)
        {
            List<int> vs = new List<int>() { 11, 21, 13, 1, 15 };
            bool b = AllCreateThanTen(vs);
            //lambda表达式
            bool b1 = vs.All(i => i > 10);
            Console.WriteLine(b);
            Console.WriteLine(b1);
        }
        static bool AllCreateThanTen( List<int> vs)
        {
            foreach (var item in vs)
            {
                if (item <= 10)
                {
                    return false;
                }
            }
            return true;
        }
    }
}

6、可选参数

参数拥有默认值

7、数组参数

简化方法的调用,不需要额外声明数组

相关推荐
小码编匠15 小时前
C# 工控上位机必备:数据转换工具类与十个核心模块
后端·c#·.net
唐青枫3 天前
别再乱用 StartNew:C#.NET TaskFactory 任务调度实战详解
c#·.net
Artech3 天前
[MAF预定义的AIContextProvider-03]ChatHistoryMemoryProvider——赋予Agent从经验中学习的能力
ai·c#·agent·memory·maf
Scout-leaf5 天前
C#摸鱼实录——IoC与DI案例详解
c#
咕白m6255 天前
使用 C# 在 Excel 中应用多种字体样式
后端·c#
Artech5 天前
[MAF预定义的AIContextProvider-02]AgentSkillsProvider——将Agent Skills引入MAF
ai·c#·agent·agent skills·maf
2601_962072556 天前
李梦娇常识4600问|题库|打印版
sql·华为od·华为·c#·华为云·.net·harmonyos
m0_547486666 天前
《C#语言程序设计与实践》 全套PPT课件
c语言·c#·c语言程序设计
叶帆6 天前
【YFIOs】用C#开发硬件之设备上云
开发语言·unity·c#
IT方大同6 天前
(嵌入式操作系统)信号量
嵌入式硬件·c#