【Azure Developer】C# / .NET 静态函数中this关键字的作用

简介: 【Azure Developer】C# / .NET 静态函数中this关键字的作用

问题描述

在查看.NET代码的时候,发现一个静态方法,第一个参数使用 this 关键字,它在这里是什么作用呢?

public static XElement AquireElement(this XContainer container,
        string name, bool addFirst = false)
    {
      ...

问题解答

通过查看微软的官方博文介绍(https://asp-blogs.azurewebsites.net/scottgu/new-orcas-language-feature-extension-methods), 这是C#的功能:扩展方法。

扩展方法允许开发人员向现有 CLR 类型的公共协定添加新方法,而无需对其进行子类化或重新编译原始类型。  

扩展方法有助于将当今动态语言中流行的“鸭子类型”支持的灵活性与强类型语言的性能和编译时验证相结合。

利用新的扩展方法功能向各个类型添加方法,为开发人员提供了许多有用的可扩展性场景。  然而,扩展方法真正强大的原因在于它们不仅能够应用于单个类型,而且还能够应用于 .NET中的任何父基类或接口。

这使得开发人员能够构建各种丰富的、可组合的框架扩展,这些扩展可以在 .NET 中使用。

例如:

public static int Foo(this MyClass arg)

就可以用

MyClass myClass = new MyClass();

int i = myClass.Foo();

代替传统的写法

MyClass myClass = new MyClass();

int i = Foo(myClass);

如果MyClass类是同一个项目里面的类,那么此方法优势不明显。 但是如果 MyClass类是由其它第三方开发,然后只是在自己的项目中引用。

那么,扩展方法(this)就非常强大。

 

本文中遇见的就是对System.Xml.Linq基类中的XContainer对象进行了扩展。添加了获取一个指定元素的扩展方法AquireElement,如元素不存在,则创建。

public static class XmlLinqExtensions
    {
        public static XElement AquireElement(this XContainer container,
            string name, bool addFirst = false)
        {
            var element = container.Element(name);
            if (null != element)
            {
                return element;
            }
            element = new XElement(name);
            if (addFirst)
            {
                container.AddFirst(element);
            }
            else
            {
                container.Add(element);
            }
            return element;
        }
    }

使用时

//XDocument document;


var configElement = document.AquireElement("configuration", addFirst: true);

var configSections = configElement.AquireElement("configSections", addFirst: true);

 

PS: 原生类中并没有包含AquireElement方法。

 

参考资料

Extension Methods :https://asp-blogs.azurewebsites.net/scottgu/new-orcas-language-feature-extension-methods

Use of "this" keyword in formal parameters for static methods in C# : https://stackoverflow.com/questions/846766/use-of-this-keyword-in-formal-parameters-for-static-methods-in-c-sharp

 

目录
打赏
0
0
0
0
194
分享
相关文章
Azure 云服务与 C# 集成浅谈
本文介绍了 Azure 云服务与 C# 的集成方法,涵盖基础概念、资源创建、SDK 使用、常见问题解决及单元测试等内容,通过代码示例详细说明了如何在 C# 中调用 Azure 服务,帮助开发者提高开发效率和代码质量。
70 8
【Azure Function】C#独立工作模式下参数类型 ServiceBusReceivedMessage 无法正常工作
Cannot convert input parameter 'message' to type 'Azure.Messaging.ServiceBus.ServiceBusReceivedMessage' from type 'System.String'.
114 73
【Azure Developer】.NET Aspire 项目本地调试遇 Grpc.Core.RpcException 异常( Error starting gRPC call ... )
Error starting gRPC call. HttpRequestException: The SSL connection could not be established, see inner exception. AuthenticationException: The remote certificate is invalid because of errors in the certificate chain: UntrustedRoot
46 12
一个包含了 50+ C#/.NET编程技巧实战练习教程
一个包含了 50+ C#/.NET编程技巧实战练习教程
131 18
精选10款C#/.NET开发必备类库(含使用教程),工作效率提升利器!
精选10款C#/.NET开发必备类库(含使用教程),工作效率提升利器!
88 12
C#/.NET/.NET Core拾遗补漏合集(24年12月更新)
C#/.NET/.NET Core拾遗补漏合集(24年12月更新)
C#/.NET/.NET Core技术前沿周刊 | 第 15 期(2024年11.25-11.30)
C#/.NET/.NET Core技术前沿周刊 | 第 15 期(2024年11.25-11.30)
C#/.NET/.NET Core技术前沿周刊 | 第 16 期(2024年12.01-12.08)
C#/.NET/.NET Core技术前沿周刊 | 第 16 期(2024年12.01-12.08)
C#进阶-ASP.NET WebForms调用ASMX的WebService接口
通过本文的介绍,希望您能深入理解并掌握ASP.NET WebForms中调用ASMX WebService接口的方法和技巧,并在实际项目中灵活运用这些技术,提高开发效率和应用性能。
87 5

热门文章

最新文章