
C# 命名约定(开发者用法)
命名约定是一套规则和指南,开发人员遵循这些规则来一致地命名变量、方法、类和其他实体。 一致的命名不仅增强代码可读性,还帮助其他开发人员理解和维护你的代码。 下面,我们将逐步讲解 C# 的命名约定,重点关注实际使用和示例。 让我们直接深入讨论命名约定,并在文章后面讨论 IronPDF 库。
命名约定概述
类和接口
类名应遵循 Pascal 命名约定。 这意味着名称中的每个单词都以大写字母开头,没有下划线或空格。 接口名称也应遵循 Pascal 命名,但以前缀 I 开始。 例如:
public class Customer
{
public decimal Balance { get; set; }
}
public interface ICustomer
{
decimal GetBalance();
}Public Class Customer
Public Property Balance() As Decimal
End Class
Public Interface ICustomer
Function GetBalance() As Decimal
End Interface注意类名ICustomer都是遵循Pascal Case。 ICustomer类型是一个接口。
方法
方法名也使用 Pascal 命名。 每个方法名应该以大写字母开头,每个后续单词也应该以大写字母开头。 这里是一些方法定义的例子:
public decimal CalculateInterest(decimal principal, decimal rate)
{
return principal * rate;
}Public Function CalculateInterest(ByVal principal As Decimal, ByVal rate As Decimal) As Decimal
Return principal * rate
End Function对于入口点方法,static void Main(),约定是相同的—方法名使用 Pascal 命名。
属性
与方法名一样,属性名也使用 Pascal 命名。 属性应命名以明确描述其代表的内容:
public DateTime DateOpened { get; set; }
public decimal Reserves { get; set; }Public Property DateOpened() As DateTime
Public Property Reserves() As Decimal局部变量和方法参数
局部变量和方法参数应使用小驼峰命名。 这意味着第一个单词是小写的,后续单词以大写字母开头,没有空格或下划线。 这与 Pascal 命名的不同之处在于第一个字母不大写。
public void SelectCustomer(string customerName)
{
var selectedCustomer = FindCustomer(customerName);
}Public Sub SelectCustomer(ByVal customerName As String)
Dim selectedCustomer = FindCustomer(customerName)
End Sub在这个例子中,局部变量customerName也使用骆驼命名法。
方法参数
方法参数的名称应该是描述性的,并遵循小驼峰命名约定。 这提高了代码的可读性并帮助开发人员理解每个参数代表的含义。
public void AddCustomer(string customerName, DateTime dateOpened)
{
// Add customer logic
}Public Sub AddCustomer(ByVal customerName As String, ByVal dateOpened As DateTime)
' Add customer logic
End Sub静态成员和字段
类中的静态成员,如 静态字段、常量和方法,也遵循特定的命名约定。
静态字段
对于静态字段,命名约定是使用小驼峰命名但带有下划线前缀。 这将它们与其他字段区分开来。
private static int _totalCustomers;Private Shared _totalCustomers As Integer常量
常量通常使用全大写字母命名,单词之间用下划线分隔以提高可读性。 例如:
public const int MAX_CUSTOMERS = 100;Public Const MAX_CUSTOMERS As Integer = 100事件处理程序
事件处理程序方法名应描述它们处理的事件,通常使用On前缀后接事件名称。 事件处理程序方法的参数通常包括对象sender和事件参数。
private void OnCustomerAdded(object sender, EventArgs e)
{
// Event handling logic
}Private Sub OnCustomerAdded(ByVal sender As Object, ByVal e As EventArgs)
' Event handling logic
End Sub在这种情况下,参数名为e。 遵循这种命名约定使你的事件处理程序与行业标准保持一致。
命名私有字段和对象初始化
私有字段应遵循小驼峰命名,但带有下划线前缀。 这有助于将它们与局部变量和方法参数区分开来。
private string _customerName;Private _customerName As String使用对象初始化器时,你可以在创建类实例时直接为属性赋值:
var seattleCustomer = new Customer
{
Balance = 1000,
DateOpened = DateTime.Now
};Dim seattleCustomer = New Customer With {
.Balance = 1000,
.DateOpened = DateTime.Now
}在这个例子中,属性名称DateOpened使用Pascal Case,符合属性命名的惯例。
异常处理和方法
处理异常时,方法名称仍应遵循 Pascal 命名约定。 异常类名称应为 Pascal 命名,并以 Exception 后缀结尾。 例如:
public void ProcessTransaction()
{
try
{
// Transaction logic
}
catch (InvalidOperationException ex)
{
// Handle exception
}
}Public Sub ProcessTransaction()
Try
' Transaction logic
Catch ex As InvalidOperationException
' Handle exception
End Try
End Sub返回类型和方法定义
始终确保你的方法定义有有意义的名称和适当的返回类型。返回类型应从方法签名中看到。 以下是一个例子:
public decimal CalculateTotalBalance()
{
return _totalCustomers * balancePerCustomer;
}Public Function CalculateTotalBalance() As Decimal
Return _totalCustomers * balancePerCustomer
End Function在这个例子中,方法名称CalculateTotalBalance是描述性的,并遵循Pascal Case命名惯例。
C#常量命名惯例
在 C# 中,常量名称应为全大写字母,单词之间用下划线分隔。 这使常量从其他变量中脱颖而出。 以下是一个例子:
public const double PI = 3.14159;Public Const PI As Double = 3.14159这种约定适用于不同类型,确保常量名称在代码中一致且易于识别。
C#代码风格惯例:换行和大括号
C# 也有代码规范,用于换行和大括号。 在C#中,每个打开的**大括号}**应在新行上,与相应的语句对齐。 以下是一个例子:
public void AddCustomer(string customerName)
{
if (!string.IsNullOrEmpty(customerName))
{
_customerName = customerName;
}
}Public Sub AddCustomer(ByVal customerName As String)
If Not String.IsNullOrEmpty(customerName) Then
_customerName = customerName
End If
End Sub使用适当的格式使代码更易读和遵循。
避免匈牙利命名法
在现代 C# 开发中,不建议使用匈牙利命名法,即变量名带有数据类型前缀(例如字符串使用 strName 或整数使用 intCount 表示)。 相反,使用 有意义的名称 来描述变量的用途,而不是其数据类型:
public string CustomerName { get; set; }
public int OrderCount { get; set; }Public Property CustomerName() As String
Public Property OrderCount() As Integer这种方法使代码更清晰、更易于维护。
在命名约定中使用 IronPDF

在将IronPDF集成到你的 C# 项目时,通过遵循命名约定保持代码的整洁和可读性是至关重要的。 IronPDF 允许你在 C# 应用中从HTML 内容生成 PDF。 在这样做时,需要遵循类、方法和变量的命名约定,以保持一致性。 下面是一个简单实现命名约定的示例,以提高代码可读性,同时使用 IronPDF 遵循这些命名约定:
using IronPdf;
public class PdfReportGenerator
{
private readonly string _htmlContent;
private readonly string _filePath;
public PdfReportGenerator(string htmlContent, string filePath)
{
_htmlContent = htmlContent;
_filePath = filePath;
}
public void GenerateReport()
{
var pdfRenderer = new ChromePdfRenderer();
PdfDocument pdfDocument = pdfRenderer.RenderHtmlAsPdf(_htmlContent);
pdfDocument.SaveAs(_filePath);
}
}
public static class Program
{
public static void Main()
{
var htmlContent = "<h1>Monthly Report</h1><p>Generated using IronPDF.</p>";
var filePath = @"C:\Reports\MonthlyReport.pdf";
PdfReportGenerator reportGenerator = new PdfReportGenerator(htmlContent, filePath);
reportGenerator.GenerateReport();
}
}Imports IronPdf
Public Class PdfReportGenerator
Private ReadOnly _htmlContent As String
Private ReadOnly _filePath As String
Public Sub New(ByVal htmlContent As String, ByVal filePath As String)
_htmlContent = htmlContent
_filePath = filePath
End Sub
Public Sub GenerateReport()
Dim pdfRenderer = New ChromePdfRenderer()
Dim pdfDocument As PdfDocument = pdfRenderer.RenderHtmlAsPdf(_htmlContent)
pdfDocument.SaveAs(_filePath)
End Sub
End Class
Public Module Program
Public Sub Main()
Dim htmlContent = "<h1>Monthly Report</h1><p>Generated using IronPDF.</p>"
Dim filePath = "C:\Reports\MonthlyReport.pdf"
Dim reportGenerator As New PdfReportGenerator(htmlContent, filePath)
reportGenerator.GenerateReport()
End Sub
End Module通过坚持这些命名约定,你的代码在使用 IronPDF 生成报告时保持专业、组织和易读。
结论

通过遵循这些 C# 命名约定,确保你的代码整洁、可读且易于维护。 无论是类名使用 Pascal 命名,局部变量使用小驼峰命名,还是私有字段使用下划线前缀,这些约定有助于建立一致的代码库。
借助 IronPDF,你可以立即开始探索其所有功能,并使用免费试用。 此试用让你亲自体验它如何完美集成到你的工作流程中。 当您准备好迈出下一步时,许可证起价仅为$999。

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


