在生產環境中測試,無水印。
在任何需要的地方都能運行。
獲得 30 天的全功能產品。
在幾分鐘內上手運行。
試用產品期間完全訪問我們的支援工程團隊
這init 關鍵字C# 9.0 引入了一種新方式來定義類屬性,以創建不可變對象。 在早期版本的 C# 中,屬性通常用於獲取和設定存取子,以讀取和寫取對象欄位。 然而,使用 init,您可以在物件初始化期間將屬性設為可寫,之後則維持唯讀。
本教程將探討使用 C# init 關鍵字的實際範例和情境。IronPDF 庫. 您還將了解傳統屬性設置器之間的關鍵差異(設定)和新的僅初始化設置器。
讓我們從一個基本範例開始:
public class Person
{
public string FirstName { get; init; }
public string LastName { get; init; }
}
var person = new Person
{
FirstName = "Iron",d
LastName = "Dev"
};
// person.FirstName = "Jane"; // This will give a compile-time error.
public class Person
{
public string FirstName { get; init; }
public string LastName { get; init; }
}
var person = new Person
{
FirstName = "Iron",d
LastName = "Dev"
};
// person.FirstName = "Jane"; // This will give a compile-time error.
在此範例中,FirstName 和 LastName 被標記為只可初始化的屬性。 這意味著它們只能在物件初始化值設置器中被指派。 物件建立之後,企圖更改其數值會導致編譯時錯誤。
使用 init 關鍵字的主要原因是在初始化後使物件屬性不可變。 傳統上,您可以將屬性標記為唯讀以實現不可變性。 然而,你通常需要一個接受所有必要值來設置欄位的構造函數,這可能會導致構造函數樣板代碼。 使用初始化器 (init),您可以利用物件初始化器達成相同的目標,而無需撰寫冗長的建構子。
public class Person
{
public string FirstName { get; init; }
public string LastName { get; init; }
// Without using constructor boilerplate for property initialization
}
var person = new Person
{
FirstName = "John",
LastName = "Doe"
};
public class Person
{
public string FirstName { get; init; }
public string LastName { get; init; }
// Without using constructor boilerplate for property initialization
}
var person = new Person
{
FirstName = "John",
LastName = "Doe"
};
使用初始化可以輕鬆地與物件初始化器配合運作。 與其依賴建構函式設定值,您可以在創建物件時直接定義所需的屬性。
public class Point
{
public int X { get; init; }
public int Y { get; init; }
}
var point = new Point { X = 10, Y = 20 };
// point.X = 30; // This will throw a compile-time error
public class Point
{
public int X { get; init; }
public int Y { get; init; }
}
var point = new Point { X = 10, Y = 20 };
// point.X = 30; // This will throw a compile-time error
這會創建一個簡單且不可變的 Point 類型對象。 請注意,X 和 Y 的值在初始化時設置,後續無法修改。
雖然 init 的主要用途是透過物件初始化器來進行物件初始化,但如果需要,你仍然可以使用建構函式。 這在物件創建期間強制執行特定屬性值時特別有用。
public class Person
{
public string FirstName { get; init; }
public string LastName { get; init; }
public Person(string firstName, string lastName)
{
FirstName = firstName;
LastName = lastName;
}
}
public class Person
{
public string FirstName { get; init; }
public string LastName { get; init; }
public Person(string firstName, string lastName)
{
FirstName = firstName;
LastName = lastName;
}
}
您可以同時使用構造函數和初始化屬性。 這種方法在對象構建後仍然保持不變的同時,具有更大的靈活性。
以前,開發者使用私有設置存取子來限制在類別外部對屬性的修改。
public class Person
{
public string FirstName { get; private set; }
public string LastName { get; private set; }
public Person(string firstName, string lastName)
{
FirstName = firstName;
LastName = lastName;
}
}
public class Person
{
public string FirstName { get; private set; }
public string LastName { get; private set; }
public Person(string firstName, string lastName)
{
FirstName = firstName;
LastName = lastName;
}
}
雖然此方法有效,但需要構造函式樣板程式碼來初始化屬性。 此外,這也允許類本身在後期修改屬性,這對於不可變物件來說並不總是理想的。 init 關鍵字解決了這個問題,因為它僅允許在物件創建時初始化,並在之後阻止任何修改。
在物件創建期間,init 關鍵字可以初始化欄位或屬性,而之後它們保持不變。 雖然唯讀欄位提供不可變性,但初始存取子為屬性提供了類似的功能。 以下是兩種處理不變性的方式:使用唯讀欄位和初始化屬性。
在此示例中,我們使用了唯讀欄位來存放 firstName 和 lastName,這些欄位是在物件建構時設置的。 這些欄位只能在建構函式中被賦值一次,之後不可修改:
public class Person
{
private readonly string firstName;
private readonly string lastName;
public string FirstName => firstName;
public string LastName => lastName;
public Person(string firstName, string lastName)
{
this.firstName = firstName;
this.lastName = lastName;
}
}
public class Person
{
private readonly string firstName;
private readonly string lastName;
public string FirstName => firstName;
public string LastName => lastName;
public Person(string firstName, string lastName)
{
this.firstName = firstName;
this.lastName = lastName;
}
}
或者,我們可以使用 init 存取器來創建只能在物件創建時進行初始化但之後無法更改的唯讀屬性。 這消除了對唯讀欄位的需求,並提供了一種更現代的語法:
public class Person
{
public string FirstName { get; init; }
public string LastName { get; init; }
}
public class Person
{
public string FirstName { get; init; }
public string LastName { get; init; }
}
IronPDF是一個為 C# 開發人員設計的強大的 PDF 生成和操作庫。 它簡化了與...的工作將 HTML 轉換為 PDF 文件、CSS、圖片和其他內容轉換成 PDF 文件。 IronPDF 具備像是完美像素渲染、跨平台支持和輕鬆整合到 .NET 專案中的功能,非常適合需要快速生成高品質 PDF 的開發者。 您可以將其用於 .NET Core、Framework 和 Standard,並且它支持包括 Windows、Linux 和 macOS 在內的多種平台。
要在 C# 專案中建立不可變物件,並生成 PDF,可以將 init 關鍵字與 IronPDF 結合使用。 初始化關鍵字確保物件在初始化後的完整性,同時IronPDF處理數據並根據該不可變模型生成PDF。
確保在您的項目中正確引用 IronPDF。 您可以透過 NuGet 安裝:
Install-Package IronPdf
Install-Package IronPdf
以下是程式碼示例:
using IronPdf;
public class Person
{
public int Id { get; init; }
public string FirstName { get; init; }
public string LastName { get; init; }
}
public class PDFGenerator
{
public static void CreatePersonPDF(Person person)
{
var htmlContent = $@"
<html>
<body>
<h1>Person Information</h1>
<p>ID: {person.Id}</p>
<p>First Name: {person.FirstName}</p>
<p>Last Name: {person.LastName}</p>
</body>
</html>";
var renderer = new ChromePdfRenderer();
var pdf = renderer.RenderHtmlAsPdf(htmlContent);
pdf.SaveAs($"Person_{person.Id}.pdf");
}
}
class Program
{
static void Main(string[] args)
{
var person = new Person
{
Id = 1,
FirstName = "Iron",
LastName = "Dev"
};
PDFGenerator.CreatePersonPDF(person);
}
}
using IronPdf;
public class Person
{
public int Id { get; init; }
public string FirstName { get; init; }
public string LastName { get; init; }
}
public class PDFGenerator
{
public static void CreatePersonPDF(Person person)
{
var htmlContent = $@"
<html>
<body>
<h1>Person Information</h1>
<p>ID: {person.Id}</p>
<p>First Name: {person.FirstName}</p>
<p>Last Name: {person.LastName}</p>
</body>
</html>";
var renderer = new ChromePdfRenderer();
var pdf = renderer.RenderHtmlAsPdf(htmlContent);
pdf.SaveAs($"Person_{person.Id}.pdf");
}
}
class Program
{
static void Main(string[] args)
{
var person = new Person
{
Id = 1,
FirstName = "Iron",
LastName = "Dev"
};
PDFGenerator.CreatePersonPDF(person);
}
}
總結來說,C# 的 init 關鍵字讓你可以在物件初始化時,創建不變物件並提供靈活性。 這是一種更乾淨且更安全的替代方案,用於私有設置訪問器,減少對構造函式樣板代碼的需求。 將 init 關鍵字與唯讀欄位、結構體及驗證邏輯結合使用,有助於構建強大且安全的資料結構,能夠在不犧牲可讀性或靈活性的情況下保持不變性。 IronPDF 提供一個免費試用,許可證價格自 $749 起。 這可以讓您使用其全部功能,包括編輯、壓縮和保護PDF文件。