IRONSOFTWAREHOME
开发者更新

C# This(开发者如何使用)

Jacob Mellor,Team Iron 的首席技术官
Jacob Mellor
Updated: 2026年4月23日

在C#中有一个特别重要的关键字,即this关键字。 该关键字指的是使用它的当前类实例。 除其他外,它还可用于区分同名的类级变量和方法参数。 例如,如果您有一个实例变量和一个同名的方法参数,this可以成为救星!

该关键词的基础知识

在一个公共类中,例如 Employee,您可能有诸如 id 或 name 之类的公共实例变量。 如果您想在一个方法中为这些实例变量赋值,您可能会遇到一个常见问题:如果方法参数与实例变量同名怎么办?

这里有一个解决方案:使用C# 文档中的 this 关键字! 在下面公共类 Employee 中的方法示例中,this 关键字用于区分同名的实例变量和方法参数。

public class Employee
{
    private int id;
    private string name;
    
    public void Display(int id, string name)
    {
        // Use `this.id` to refer to the instance variable, 
        // and `id` for the method parameter.
        this.id = id;
        this.name = name;
    }
}

在这种情况下,this.id 指的是实例变量,而 id 是方法参数。

构造函数重载中的this关键字

通过利用this关键字,构造函数重载成为同一类中的强大技术。 当一个类(例如 Student 类)具有多个带有不同参数的构造函数时,this 关键字允许一个构造函数调用另一个,从而消除冗余代码的需要。

请看以下使用在参数化构造函数中的this的示例:

public class Student
{
    private string name;
    private int id;

    public Student() : this("Default", 0)
    {
        // Default constructor delegates to the parameterized constructor
        // with "Default" as the name and 0 as the id.
    }

    public Student(string name, int id)
    {
        // Assign the parameters to the instance variables
        this.name = name;
        this.id = id;
    }
}

在无参数构造函数中,this("Default", 0) 调用参数化构造函数,将 Default 设为名称,将 0 设为 ID。

探索扩展方法中的this

C# 中的扩展方法提供了一种在不修改原始类型的情况下为现有类型添加方法的方法。 这是this关键字发生魔法的地方。 在扩展方法的参数列表中,它指的是被扩展的类型。

下面是一个扩展方法的例子:

public static class StringExtensions
{
    // This extension method can be called on any string instance
    public static bool IsNullOrEmpty(this string str)
    {
        return string.IsNullOrEmpty(str);
    }
}

在这里,this string str告诉C#这是一个针对字符串类型的扩展方法。 现在,您可以在任何字符串对象上使用此方法,如if(myString.IsNullOrEmpty())。

索引器中的this

this关键字也可以用于定义索引器。 索引器可以像数组一样对类的实例进行索引。 这有助于您使用类似索引的符号访问对象中的数据。 在索引器中,this 后面跟着一个数组索引,通常是 int index。

下面是一个索引器的基本示例:

public class Test
{
    private int[] array = new int[100];

    // Define an indexer for the class
    public int this[int index]
    {
        get { return array[index]; }
        set { array[index] = value; }
    }
}

在这个 Test 类中,this 关键字定义了一个索引器,可用于获取或设置 array 实例字段中的值。

this和静态成员

关于this的一点需要注意,它无法用于引用静态成员或方法。 这是因为this指的是当前实例,而静态成员属于类本身,并不属于类的实例。

public class Program
{
    public static void Main(string[] args)
    {
        // Can't use `this` here, because 'Main' is a static method.
    }
}

所以请记住,this是用于实例的,而不是类级别或静态成员!

this关键字与属性

就像实例变量和方法参数一样,this关键字也可以用于属性。 在 C# 中,属性是一种成员,它为读取、写入或计算私有字段的值提供了灵活的机制。 属性可以作为公共数据成员使用,但它们实际上是一种特殊的方法,称为访问器。

让我们看看一个在属性中使用this的简单示例:

public class Employee
{
    private string name;

    public string Name
    {
        get { return this.name; }
        set { this.name = value; } // Use `this` to refer to the instance variable
    }
}

在上面的类中,this 关键字用于在 Name 属性的 get 和 set 访问器中引用私有字符串 name。

探索this和委托

在另一个地方this出现的是在委托中。 C# 中的委托类似于 C 或 C++ 中的函数指针。 这是一个引用类型的变量,用于保存方法的引用。 委托方法,就像扩展方法一样,可以使用this访问当前实例。

这是使用this的委托示例:

public delegate void DisplayDelegate();

public class Student
{
    public int Id { get; set; }
    public string Name { get; set; }

    public void Display()
    {
        // `this.DisplayDetails` refers to the method instance of the current object.
        DisplayDelegate displayDelegate = new DisplayDelegate(this.DisplayDetails);
        displayDelegate();
    }

    private void DisplayDetails()
    {
        Console.WriteLine("ID: " + Id + ", Name: " + Name);
    }
}

在学生类中,this.DisplayDetails 创建了一个新的委托实例,它引用当前对象的 DisplayDetails 方法。

与IronPDF一起实现this关键字

让我们深入探讨一个可能使用this关键字与IronPDF结合的示例,这是一款用于通过HTML编辑和创建PDF文件的强大.NET库。

考虑一个名为PDFHandler的类,该类使用IronPDF库对PDF文件执行各种操作:

using IronPdf;

public class PDFHandler
{
    private string path;

    public PDFHandler(string path)
    {
        this.path = path;
    }

    public void GeneratePDF(string content)
    {
        // Creating a renderer to convert HTML content to PDF
        var Renderer = new IronPdf.ChromePdfRenderer();
        var PDF = Renderer.RenderHtmlAsPdf(content);
        
        // Save the generated PDF to the path specified by the current instance
        PDF.SaveAs(this.path);
    }
}

在这个 PDFHandler 类中,this 关键字用于引用当前实例的 path 字段。 此字段用于将生成的 PDF 保存到指定路径。

当我们创建一个新的 PDFHandler 实例并调用 GeneratePDF 方法时,this 关键字允许我们使用在对象创建期间指定的 path:

class Program
{
    static void Main(string[] args)
    {
        // Initialize PDFHandler with a specified file path
        PDFHandler pdfHandler = new PDFHandler("C:\\ThisKeyword.pdf");
        pdfHandler.GeneratePDF("Hello World!");
    }
}

在这里,this使代码更加可读和易于理解,特别是在处理像IronPDF这样的库时。

C# This (How It Works For Developers) Figure 1

结论

到现在为止,您应该对C#中的this关键字有了很好的了解,包括其广泛应用,从简单的实例变量到复杂的上下文,如构造函数、扩展方法、属性、委托、匿名方法,甚至使用像IronPDF这样的流行库时。

请记住,IronPDF 提供 免费的 IronPDF 试用版,因此您可以对今天所学的一切进行测试。 如果您决定继续使用,许可证起价仅为 $999。 IronPDF 可以成为您 C# 开发工具包中值得一用的工具,简化在应用程序中处理 PDF 文件的任务。

Jacob Mellor,Team Iron 的首席技术官
首席技术官

Jacob Mellor 是 Iron Software 的首席技术官,也是一位开创 C# PDF 技术的有远见的工程师。作为 Iron Software 核心代码库的原始开发者,他从公司成立之初就开始塑造公司的产品架构,与首席执行官 Cameron Rimington 一起将公司转变为一家拥有 50 多名员工的公司,为 NASA、特斯拉和全球政府机构提供服务。

相关文章

Key in blue circle

立即获取免费的 30 天试用版密钥。

Your trial license will be sent to your email address

无任何限制。100% 解锁。无需信用卡。

OR
bullet_checked无需信用卡或创建账户无任何限制。100% 解锁。无需信用卡。
  • Logo Aetna
  • Logo NASA
  • Logo GE
  • Logo Porsche
  • Logo USDA
  • Logo Qatar
Join Millions of Engineers who’ve tried Iron Suite
预约您的免费现场演示
Booking Badge

深受全球数百万工程师信赖

Iron Software 的客户徽标
获取您的无义务咨询
填写下面的表格或通过sales@ironsoftware.com
您的资料将始终保密。
深受全球数百万工程师信赖
Iron Software 的客户徽标
立即获取您的免费30 天试用密钥。
无需信用卡或创建账户
C# 用于 PDF 的 NuGet 库
通过 NuGet 安装

版本: 2026.9

PM > Install-Package IronPdf
nuget.org/packages/IronPdf/
  1. 在解决方案资源管理器中,右键点击引用,管理 NuGet 包
  2. 选择浏览并搜索 “IronPDF”
  3. 选择包并安装
C# PDF DLL
下载 DLL

版本: 2026.9

或在此处下载 Windows 安装程序。

  1. 下载并解压 IronPDF 到您的解决方案目录中的 ~/Libs 之类的位置
  2. 在 Visual Studio 解决方案资源管理器中,右键点击引用。选择浏览,“IronPDF.dll”

$999 起