C# 扩展方法

WBOY
发布: 2024-09-03 15:32:56
原创
599 人浏览过

根据扩展的字面意思,附加方法称为C#扩展方法,使用它可以在不做任何更改或继承或重构原始结构、类或接口的情况下添加附加方法,我们可以添加这样的扩展方法我们创建的自定义类、.NET 框架的类或来自第三方或接口的类,这些扩展方法可以在整个程序流程中通过包含定义它们的命名空间来访问,并且它是静态方法静态类中定义的特殊类型。

C# 扩展方法的语法

定义命名空间、类和扩展方法。

语法:

namespace namespace_name
{
public static class class_name
{
public static bool extension_method_name(parameters_list)
{
//Blocks of code
}
}
}
登录后复制

其中namespace_name是定义扩展方法的命名空间的名称。

Class_name 是定义扩展方法的静态类的名称。

Extension_method_name 是扩展方法的名称。

参数列表是参数列表,第一个参数是方法要操作的运算符的类型,该运算符的前缀为 this 关键字。

C# 扩展方法的工作原理

  • 扩展方法是额外创建的自定义方法,不属于原始类的一部分。
  • 定义一个命名空间,在其中定义静态类,然后在静态类中定义扩展方法。通过使用定义扩展方法的命名空间,可以在整个应用程序中使用该方法。
  • 扩展方法是静态类中定义的静态方法的一种特殊情况,其第一个参数是要操作的运算符的类型,前缀为 this 关键字。
  • 扩展方法可以包含在.NET框架类、自定义类、结构或接口、第三方类中。

实现 C# 扩展方法的示例

以下是 C# 扩展方法的示例

示例#1

C# 程序,演示程序中比较两个整数的扩展方法:

 代码:

using System;
using System.Text;
//a namespace called check is defined
namespace check
{
// a static class called extensionclassmethod is defined
public static class extensionmethodclass
{
//extension method to compare two integers is defined
public static bool extensionmethodname(this intstr, intval)
{
return str>val;
}
}
//a class called check1 is defined
class check1
{
//main method is called
static void Main(string[] args)
{
intstri = 565;
//extension method defined in another static class is called here
bool z = stri.myExtensionMethod(200);
Console.WriteLine("The result of the comparison is: {0}", z);
Console.ReadLine();
}
}
}
登录后复制

输出:

C# 扩展方法

说明:在上面的程序中,定义了一个名为 check 的命名空间。然后定义一个称为扩展方法类的静态类,其中定义了比较两个整数的扩展方法。然后定义另一个名为 check1 的类,即使它是在不同的类中定义但位于同一命名空间下,也可以在其中添加扩展方法。扩展方法返回两个整数的比较结果。快照的输出如上面的快照所示。

示例#2

C# 程序演示程序中的 Extension 方法以找出字符串的长度:

 代码:

using System;
using System.Text;
//a namespace called check is defined
namespace check
{
// a static class called extensionclassmethod is defined
public static class extensionmethodclass
{
//extension method to find out the length of a string is defined
public static intextensionmethodname(this string str)
{
return str.Length;
}
}
//a class called check1 is defined
class check1
{
//main method is called
static void Main(string[] args)
{
string stri = "ShobhaShivakumar";
//extension method defined in another static class is called here
int z = stri.extensionmethodname();
Console.WriteLine("The length of the string obtained by using extension method is: {0}", z);
Console.ReadLine();
}
}
}
登录后复制

输出:

C# 扩展方法

说明:在上面的程序中,定义了一个名为 check 的命名空间。然后定义一个称为扩展方法类的静态类,其中定义了计算作为参数传递给它的字符串长度的扩展方法。然后定义另一个名为 check1 的类,即使它是在不同的类中定义但位于同一命名空间下,也可以在其中添加扩展方法。扩展方法返回作为参数传递给它的字符串的长度作为结果。快照的输出如上面的快照所示。

示例 #3

代码:

using System;
using System.Text;
//a namespace called check is defined
namespace check
{
// a static class called extensionclassmethod is defined
public static class extensionmethodclass
{
//extension method to add two numbers is defined
public static intextensionmethodname(this intstr, intval)
{
return str+val;
}
}
//a class called check1 is defined
class check1
{
//main method is called
static void Main(string[] args)
{
intstri = 100;
//extension method defined in another static class is called here
int z = stri.extensionmethodname(200);
Console.WriteLine("The result of addition of two numbers obtained by using extension method is: {0}", z);
Console.ReadLine();
}
}
}
登录后复制

输出:

C# 扩展方法

说明:在上面的程序中,定义了一个名为 check 的命名空间。然后定义一个称为扩展方法类的静态类,其中定义了将作为参数传递给它的两个数字相加的扩展方法。然后定义另一个名为 check1 的类,即使它是在不同的类中定义但位于同一命名空间下,也可以在其中添加扩展方法。扩展方法返回两个数字相加后的结果。

结论

在本教程中,我们通过定义、语法以及编程示例及其输出来了解 C# 扩展方法的概念。

推荐文章

这是 C# 扩展方法的指南。在这里,我们讨论 C# 扩展方法简介及其工作原理以及示例和代码实现。您还可以浏览我们其他推荐的文章以了解更多信息 –

  1. C# 中的随机数生成器
  2. Java 中的静态构造函数
  3. C# 中的 TextWriter
  4. C# 中的静态构造函数

以上是C# 扩展方法的详细内容。更多信息请关注PHP中文网其他相关文章!

相关标签:
来源:php
本站声明
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
关于我们 免责声明 Sitemap
PHP中文网:公益在线PHP培训,帮助PHP学习者快速成长!