当前位置: 首页 > news >正文

C#自定义扩展方法 及 EventHandler<TEventArgs> 委托

有自定义官方示例链接:

如何实现和调用自定义扩展方法 - C# | Microsoft Learn

1.静态类

2.静态方法

3.第一参数固定为this + 要修改的类型,后面才是自定的参数

AI给出的一个示例:没有自定义参数 、有自定义参数的

using System;
using System.Collections.Generic;
using CustomExtensions;namespace ExtensionMethodExample
{class Program{static void Main(string[] args){// 创建一个整数列表List<int> numbers = new List<int> { 1, 2, 3, 4, 5 };// 调用扩展方法 Sum,计算列表中所有数字的总和int total = numbers.Sum();// 输出结果Console.WriteLine($"Sum of all numbers in the list is: {total}");}}
}namespace CustomExtensions
{// 定义一个静态类来存放扩展方法public static class ListExtensions{// 定义一个扩展方法 Sum,用于计算 List<int> 中所有数字的总和public static int Sum(this List<int> list){int total = 0;foreach (int number in list){total += number;}return total;}}
}
using System;
using System.Collections.Generic;
using CustomExtensions;namespace ExtensionMethodExample
{class Program{static void Main(string[] args){// 创建一个整数列表List<int> numbers = new List<int> { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };// 指定阈值int threshold = 5;// 调用扩展方法 Sum,计算大于阈值的数字的总和int total = numbers.Sum(threshold);// 输出结果Console.WriteLine($"Sum of numbers greater than {threshold} is: {total}");}}
}namespace CustomExtensions
{// 定义一个静态类来存放扩展方法public static class ListExtensions{// 定义一个扩展方法 Sum,用于计算 List<int> 中大于指定阈值的数字的总和public static int Sum(this List<int> list, int threshold){int total = 0;foreach (int number in list){if (number > threshold){total += number;}}return total;}}
}

EventHandler<TEventArgs> 委托 (System) | Microsoft Learn

相关文章:

  • 【linux驱动】【设备树】按键设备树讲解
  • WaterStamp —— 一个实用的网页水印生成器开发记
  • 【周输入】510周阅读推荐-3
  • c/c++数据类型转换.
  • 二:操作系统之进程控制块(PCB)
  • Selinux权限问题处理指导文档分享
  • 菱形继承原理
  • 中国与全球电子取证行业市场报告(公开信息版)
  • 暴雨大讲堂:高性能计算面临的芯片挑战
  • 牛客网NC210769: 字母大小写转换问题解析
  • HJ5 进制转换【牛客网】
  • Python 中二维列表(list)(嵌套列表)详解
  • uWSGI是什么?
  • Java中关于方法的调用和递归
  • 【cursor】有效解决
  • Appium自动化测试环境搭建及配置
  • 西门子1200/1500博图(TIA Portal)寻址方式详解
  • AI 制作游戏美术素材流程分享(程序员方向粗糙版)
  • CCpro工程编程软件
  • Git从入门到精通
  • 外交部发言人就第78届世界卫生大会拒绝涉台提案发表谈话
  • 巴基斯坦外长访华是否与印巴局势有关?外交部:此访体现巴方高度重视中巴关系
  • 这个死亡率第一的“老年病”,正悄悄逼近年轻人
  • 广东信宜一座在建桥梁暴雨中垮塌,镇政府:未造成人员伤亡
  • 内蒙古赤峰市城建集团董事长孙广通拟任旗县区党委书记
  • 盐城经济技术开发区党工委书记王旭东接受纪律审查和监察调查