C# 拓展方法(涉及Linq)

拓展方法


C#中,扩展方法是一种特殊的静态方法,允许开发者向现有类型"添加"新的方法,而无需修改该类型的源代码或创建新的派生类型。这种机制提供了一种更为灵活的方式来扩展已有的类或结构的功能。

C# 中的扩展方法是一种特殊的静态方法,它允许你为现有类型添加新的方法,而不需要修改原始类型。扩展方法通常用于为那些你无法修改的类型添加功能,比如 .NET Framework 中的类型。


扩展方法的定义必须在一个静态类中,并且方法的第一个参数必须使用 this 关键字,这个
参数指定了扩展方法所作用的类型


定义一个扩展方法

要定义一个扩展方法,需要遵循以下规则:

扩展方法必须在静态类中定义。

扩展方法的第一个参数要使用 this 关键字,并且这个参数指定了方法将扩展哪个类型。

第一个参数之后的参数是扩展方法所需要的其他参数。

下面是一个简单的扩展方法的例子,这个方法为 int 类型添加了一个名为 Square 的方法,用于计算整数的平方:

csharp 复制代码
public static class IntegerExtensions
{
    // 这是一个扩展方法,它扩展了 int 类型
    public static int Square(this int num)
    {
        return num * num;
    }
}

使用扩展方法

一旦定义了扩展方法,就可以像使用实例方法一样来使用它。

例如

csharp 复制代码
using System;

namespace ExtensionMethods
{
    class Program
    {
        static void Main(string[] args)
        {
            int number = 5;
            int square = number.Square(); // 调用扩展方法
            Console.WriteLine($"The square of {number} is {square}");
        }
    }

    public static class IntegerExtensions
    {
        public static int Square(this int num)
        {
            return num * num;
        }
    }
}

在这个例子中,我们不需要修改 int 类型的定义,就可以直接调用 Square 方法。


再举个例子

csharp 复制代码
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace forCode {
    class Program {
        static void Main(string[] args) {

            double a = 2;
            int b = a.Square();
            Console.WriteLine($"b = {b}");
            Console.ReadKey();
        }
    }
    public static class aa {
        public static int Square(this double num) {
            return Convert.ToInt32(Math.Pow(num, 2.0));
        }
    }

}

上述例子是double的拓展,一个double变量平方后转为int


终极例子

csharp 复制代码
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace forCode {
    class Program {
        static void Main(string[] args) {

            MyNumber m = new MyNumber();
            m.a = 1;
            m.AddSome(3);
            Console.WriteLine($"m.a = {m.a}");
            Console.ReadKey();
        }
    }

    public class MyNumber {
        public int a;
    } 

    public static class MyExTention {
        public static int Square(this double num) {
            return Convert.ToInt32(Math.Pow(num, 2.0));
        }
        public static void AddSome(this MyNumber myNumber, int num) {
            myNumber.a += num;
        }
    }

}

为自定义的类添加扩展方法

注意事项

扩展方法不能访问它们所扩展的类型中的私有成员。

如果扩展方法与现有方法冲突,那么扩展方法将不会被调用。

扩展方法是一种语法糖,编译器会将它们转换为静态方法的调用。

通过使用扩展方法,开发者可以更加方便地对现有类型进行功能增强,同时保持代码的整洁和可维护性。

与Linq

Where就是一种扩展方法

相关推荐
hez20101 天前
Runtime Async - 步入高性能异步时代
c#·.net·.net core·clr
mudtools1 天前
.NET驾驭Word之力:玩转文本与格式
c#·.net
唐青枫2 天前
C#.NET 数据库开发提速秘籍:SqlSugar 实战详解
c#·.net
mudtools2 天前
.NET驾驭Word之力:理解Word对象模型核心 (Application, Document, Range)
c#·.net
侃侃_天下2 天前
最终的信号类
开发语言·c++·算法
echoarts3 天前
Rayon Rust中的数据并行库入门教程
开发语言·其他·算法·rust
Aomnitrix3 天前
知识管理新范式——cpolar+Wiki.js打造企业级分布式知识库
开发语言·javascript·分布式
大飞pkz3 天前
【设计模式】C#反射实现抽象工厂模式
设计模式·c#·抽象工厂模式·c#反射·c#反射实现抽象工厂模式
每天回答3个问题3 天前
UE5C++编译遇到MSB3073
开发语言·c++·ue5
伍哥的传说3 天前
Vite Plugin PWA – 零配置构建现代渐进式Web应用
开发语言·前端·javascript·web app·pwa·service worker·workbox