跳過到頁腳內容
.NET幫助

C#差異聯合(對開發者如何理解的工作)

區分的統一,也稱為標記的統一或總和類型,代表了一種強大的工具,可用來建模資料,這些資料可以有不同的形式,但具有定義明確且有限的可能情況。 雖然 C# 並不像某些其他語言 (例如 F# 或 Rust) 般有原生的判別式結合,但您可以使用語言中的幾種技術模擬判別式結合。 在本教程中,我們將深入介紹區分聯合、如何在 C# 中實作,以及其與 IronPDF 函式庫的實際使用案例。

什麼是判別聯合?

簡單來說,識別聯盟是一種類型,可以容納多種預定義的形式或值中的一種。 它提供了一種建立類型安全結構的方法,可以封裝不同的類型或值,同時在編譯時確保只處理有效的情況。

想像一下您想要表示操作結果的情況。 操作可能成功,返回一些資料,也可能失敗,返回錯誤訊息。 判別式合併將允許您在單一類型中表示這兩種可能的結果。

範例:模擬 C# 中的判別聯合;

以下是如何在 C# 中使用類結構模擬判別聯合的範例:

// Define an abstract base class representing the operation result.
public abstract class OperationResult<T>
{
    // Private constructor to ensure the class cannot be instantiated directly.
    private OperationResult() { }

    // Nested class representing a successful operation result.
    public sealed class Success : OperationResult<T>
    {
        public T Value { get; }

        public Success(T value) => Value = value;

        public override string ToString() => $"Success: {Value}";
    }

    // Nested class representing a failed operation result.
    public sealed class Failure : OperationResult<T>
    {
        public string Error { get; }

        public Failure(string error) => Error = error;

        public override string ToString() => $"Failure: {Error}";
    }

    // Factory method to create a successful operation result.
    public static OperationResult<T> CreateSuccess(T value) => new Success(value);

    // Factory method to create a failed operation result.
    public static OperationResult<T> CreateFailure(string error) => new Failure(error);
}
// Define an abstract base class representing the operation result.
public abstract class OperationResult<T>
{
    // Private constructor to ensure the class cannot be instantiated directly.
    private OperationResult() { }

    // Nested class representing a successful operation result.
    public sealed class Success : OperationResult<T>
    {
        public T Value { get; }

        public Success(T value) => Value = value;

        public override string ToString() => $"Success: {Value}";
    }

    // Nested class representing a failed operation result.
    public sealed class Failure : OperationResult<T>
    {
        public string Error { get; }

        public Failure(string error) => Error = error;

        public override string ToString() => $"Failure: {Error}";
    }

    // Factory method to create a successful operation result.
    public static OperationResult<T> CreateSuccess(T value) => new Success(value);

    // Factory method to create a failed operation result.
    public static OperationResult<T> CreateFailure(string error) => new Failure(error);
}
' Define an abstract base class representing the operation result.
Public MustInherit Class OperationResult(Of T)
	' Private constructor to ensure the class cannot be instantiated directly.
	Private Sub New()
	End Sub

	' Nested class representing a successful operation result.
	Public NotInheritable Class Success
		Inherits OperationResult(Of T)

		Public ReadOnly Property Value() As T

		Public Sub New(ByVal value As T)
			Me.Value = value
		End Sub

		Public Overrides Function ToString() As String
			Return $"Success: {Value}"
		End Function
	End Class

	' Nested class representing a failed operation result.
	Public NotInheritable Class Failure
		Inherits OperationResult(Of T)

		Public ReadOnly Property [Error]() As String

		Public Sub New(ByVal [error] As String)
			Me.Error = [error]
		End Sub

		Public Overrides Function ToString() As String
			Return $"Failure: {[Error]}"
		End Function
	End Class

	' Factory method to create a successful operation result.
	Public Shared Function CreateSuccess(ByVal value As T) As OperationResult(Of T)
		Return New Success(value)
	End Function

	' Factory method to create a failed operation result.
	Public Shared Function CreateFailure(ByVal [error] As String) As OperationResult(Of T)
		Return New Failure([error])
	End Function
End Class
$vbLabelText   $csharpLabel

在這個範例中,OperationResult<T> 是一個抽象類,代表我們的判別聯合類型。 它既可以是 T 類型值的 SuccessFailure 錯誤訊息。 私有構建器可確保只能透過預先定義的案例來建立此類型的實體。

使用模式匹配與區分聯合。

C# 提供了強大的模式匹配功能,能很好地與判別聯合。 讓我們擴充 OperationResult<T> 的範例,使用 switch 表達式來處理不同情況的方法。

// Method to handle the result using pattern matching.
public string HandleResult(OperationResult<int> result) =>
    result switch
    {
        OperationResult<int>.Success success => $"Operation succeeded with value: {success.Value}",
        OperationResult<int>.Failure failure => $"Operation failed with error: {failure.Error}",
        _ => throw new InvalidOperationException("Unexpected result type")
    };
// Method to handle the result using pattern matching.
public string HandleResult(OperationResult<int> result) =>
    result switch
    {
        OperationResult<int>.Success success => $"Operation succeeded with value: {success.Value}",
        OperationResult<int>.Failure failure => $"Operation failed with error: {failure.Error}",
        _ => throw new InvalidOperationException("Unexpected result type")
    };
' Method to handle the result using pattern matching.
'INSTANT VB TODO TASK: The following 'switch expression' was not converted by Instant VB:
'public string HandleResult(OperationResult<int> result) => result switch
'	{
'		OperationResult<int>.Success success => $"Operation succeeded with value: {success.Value}",
'		OperationResult<int>.Failure failure => $"Operation failed with error: {failure.Error}",
'		_ => throw new InvalidOperationException("Unexpected result type")
'	};
$vbLabelText   $csharpLabel

這裡的 switch 表達式會處理 OperationResult<int>SuccessFailure 兩種情況。 這可確保在編譯時涵蓋所有可能的情況,提供類型安全並降低執行時出錯的風險。

判別式 Unions 的擴充方法

您可以使用擴充方法來擴充判別聯合的功能。 例如,讓我們為 OperationResult<T> 建立一個擴充方法,以判斷結果是否成功:

// Static class to hold extension methods for OperationResult<T>.
public static class OperationResultExtensions
{
    // Extension method to check if the operation result indicates success. 
    public static bool IsSuccess<T>(this OperationResult<T> result) =>
        result is OperationResult<T>.Success;
}
// Static class to hold extension methods for OperationResult<T>.
public static class OperationResultExtensions
{
    // Extension method to check if the operation result indicates success. 
    public static bool IsSuccess<T>(this OperationResult<T> result) =>
        result is OperationResult<T>.Success;
}
' Static class to hold extension methods for OperationResult<T>.
Public Module OperationResultExtensions
	' Extension method to check if the operation result indicates success. 
	<System.Runtime.CompilerServices.Extension> _
	Public Function IsSuccess(Of T)(ByVal result As OperationResult(Of T)) As Boolean
		Return TypeOf result Is OperationResult(Of T).Success
	End Function
End Module
$vbLabelText   $csharpLabel

此靜態方法會檢查結果是否為 Success 的實例。

原生支援 C# 中的判別聯合;。

C# 並不像某些其他語言一樣,原生支援識別合一,但社群中一直在討論增加此功能。 本機區分的聯合將使定義和使用聯合類型變得更容易,而不需要依賴類的層次結構。

編譯器錯誤與類型安全

判別式結合的主要優點之一是其提供的類型安全性。由於在編譯時已知道所有可能的情況,因此編譯器可以強制處理所有情況。 這可減少執行時的錯誤,並使程式碼不易出錯。

例如,如果您忘記處理 switch 語句中的特定 case,編譯器會產生錯誤,提示您處理遺漏的 case。 這在處理具有多種可能情況的複雜資料結構時尤其有用。

在 C# 中使用 IronPDF 與 Discriminated Unions;。

C# Discriminated Union (How It Works For Developers):圖 1 - IronPdf

IronPDF 是一個 C# PDF 函式庫,可協助開發人員 從 HTML 創建 PDF 檔案,並允許他們毫不費力地修改 PDF 檔案。 在 C# 中處理 PDF 時,您可以將 IronPDF 與判別式聯結整合,以便在產生或處理 PDF 檔案時處理不同的情況。 例如,您可能有一個成功產生 PDF 或遇到錯誤的程序。 判別式合一可讓您清楚地建立此流程的模型。 讓我們建立一個簡單的範例,使用 IronPDF 產生 PDF,並將結果以判別聯合的方式傳回。

// Using directives for necessary namespaces.
using IronPdf;
using System;

// Define an abstract base class representing the PDF generation result.
public abstract class PdfResult
{
    // Private constructor to ensure the class cannot be instantiated directly.
    private PdfResult() { }

    // Nested class representing a successful PDF generation result.
    public sealed class Success : PdfResult
    {
        public PdfDocument Pdf { get; }

        public Success(PdfDocument pdf) => Pdf = pdf;

        public override string ToString() => "PDF generation succeeded";
    }

    // Nested class representing a failed PDF generation result.
    public sealed class Failure : PdfResult
    {
        public string ErrorMessage { get; }

        public Failure(string errorMessage) => ErrorMessage = errorMessage;

        public override string ToString() => $"PDF generation failed: {ErrorMessage}";
    }

    // Factory method to create a successful PDF result.
    public static PdfResult CreateSuccess(PdfDocument pdf) => new Success(pdf);

    // Factory method to create a failed PDF result.
    public static PdfResult CreateFailure(string errorMessage) => new Failure(errorMessage);
}

// Class to generate PDFs using IronPDF.
public class PdfGenerator
{
    // Method to generate a PDF from HTML content and return the result as a PdfResult.
    public PdfResult GeneratePdf(string htmlContent)
    {
        try
        {
            // Create a new ChromePdfRenderer instance.
            var renderer = new ChromePdfRenderer();

            // Attempt to render the HTML content as a PDF.
            var pdf = renderer.RenderHtmlAsPdf(htmlContent);

            // Return a success result with the generated PDF.
            return PdfResult.CreateSuccess(pdf);
        }
        catch (Exception ex)
        {
            // Return a failure result with the error message if an exception occurs.
            return PdfResult.CreateFailure(ex.Message);
        }
    }
}
// Using directives for necessary namespaces.
using IronPdf;
using System;

// Define an abstract base class representing the PDF generation result.
public abstract class PdfResult
{
    // Private constructor to ensure the class cannot be instantiated directly.
    private PdfResult() { }

    // Nested class representing a successful PDF generation result.
    public sealed class Success : PdfResult
    {
        public PdfDocument Pdf { get; }

        public Success(PdfDocument pdf) => Pdf = pdf;

        public override string ToString() => "PDF generation succeeded";
    }

    // Nested class representing a failed PDF generation result.
    public sealed class Failure : PdfResult
    {
        public string ErrorMessage { get; }

        public Failure(string errorMessage) => ErrorMessage = errorMessage;

        public override string ToString() => $"PDF generation failed: {ErrorMessage}";
    }

    // Factory method to create a successful PDF result.
    public static PdfResult CreateSuccess(PdfDocument pdf) => new Success(pdf);

    // Factory method to create a failed PDF result.
    public static PdfResult CreateFailure(string errorMessage) => new Failure(errorMessage);
}

// Class to generate PDFs using IronPDF.
public class PdfGenerator
{
    // Method to generate a PDF from HTML content and return the result as a PdfResult.
    public PdfResult GeneratePdf(string htmlContent)
    {
        try
        {
            // Create a new ChromePdfRenderer instance.
            var renderer = new ChromePdfRenderer();

            // Attempt to render the HTML content as a PDF.
            var pdf = renderer.RenderHtmlAsPdf(htmlContent);

            // Return a success result with the generated PDF.
            return PdfResult.CreateSuccess(pdf);
        }
        catch (Exception ex)
        {
            // Return a failure result with the error message if an exception occurs.
            return PdfResult.CreateFailure(ex.Message);
        }
    }
}
' Using directives for necessary namespaces.
Imports IronPdf
Imports System

' Define an abstract base class representing the PDF generation result.
Public MustInherit Class PdfResult
	' Private constructor to ensure the class cannot be instantiated directly.
	Private Sub New()
	End Sub

	' Nested class representing a successful PDF generation result.
	Public NotInheritable Class Success
		Inherits PdfResult

		Public ReadOnly Property Pdf() As PdfDocument

		Public Sub New(ByVal pdf As PdfDocument)
			Me.Pdf = pdf
		End Sub

		Public Overrides Function ToString() As String
			Return "PDF generation succeeded"
		End Function
	End Class

	' Nested class representing a failed PDF generation result.
	Public NotInheritable Class Failure
		Inherits PdfResult

		Public ReadOnly Property ErrorMessage() As String

		Public Sub New(ByVal errorMessage As String)
			Me.ErrorMessage = errorMessage
		End Sub

		Public Overrides Function ToString() As String
			Return $"PDF generation failed: {ErrorMessage}"
		End Function
	End Class

	' Factory method to create a successful PDF result.
	Public Shared Function CreateSuccess(ByVal pdf As PdfDocument) As PdfResult
		Return New Success(pdf)
	End Function

	' Factory method to create a failed PDF result.
	Public Shared Function CreateFailure(ByVal errorMessage As String) As PdfResult
		Return New Failure(errorMessage)
	End Function
End Class

' Class to generate PDFs using IronPDF.
Public Class PdfGenerator
	' Method to generate a PDF from HTML content and return the result as a PdfResult.
	Public Function GeneratePdf(ByVal htmlContent As String) As PdfResult
		Try
			' Create a new ChromePdfRenderer instance.
			Dim renderer = New ChromePdfRenderer()

			' Attempt to render the HTML content as a PDF.
			Dim pdf = renderer.RenderHtmlAsPdf(htmlContent)

			' Return a success result with the generated PDF.
			Return PdfResult.CreateSuccess(pdf)
		Catch ex As Exception
			' Return a failure result with the error message if an exception occurs.
			Return PdfResult.CreateFailure(ex.Message)
		End Try
	End Function
End Class
$vbLabelText   $csharpLabel

PdfResult 類代表具有兩種情況的判別聯合:成功失敗Success 的情況包含一個 PdfDocument ,而 Failure 的情況則包含一個錯誤訊息。 GeneratePdf 方法接收 HTML 字串,嘗試使用 IronPDF 生成 PDF,並將結果以 PdfResult 的形式返回。 如果 PDF 生成成功,它會返回 Success 個案與生成的 PDF。 如果發生異常,它會返回 Failure 的情況,並附上錯誤訊息。

結論

C# Discriminated Union (How It Works For Developers):圖 2 - 授權

C# 中的判別聯合提供了一種功能強大且靈活的方式,可為具有多種可能情況的資料建模。 雖然 C# 不支援判別式結合,但您可以使用類別層次、模式匹配及其他技術模擬它們。 翻譯後的程式碼會更安全、不易出錯且更容易維護。

IronPdf 提供 免費試用,讓您無需任何前期費用即可感受軟體的優點。 您可以探索所有功能,看看它們如何符合您的需求。 在您試用之後,可從 $799 開始取得授權。

常見問題解答

如何在 C# 中建立判別聯合?

您可以透過定義具有巢狀子類別的抽象類別,在 C# 中建立判別式聯合。每個子類別代表一種可能的情況,例如成功或錯誤狀態,您可以使用模式匹配來處理這些情況。

IronPDF 函式庫在處理區分聯盟時扮演什麼角色?

IronPDF 函式庫可與判別式單元一起使用,以管理 PDF 生成結果。透過將這些結果建模為判別式單元,您可以確保類型安全,並處理成功的 PDF 建立與發生的任何錯誤。

模式匹配如何增強 C# 中的判別聯合?

模式匹配可讓開發人員優雅地處理每種可能的情況,從而增強 C# 中的判別聯合。透過模式匹配,您可以安全地管理不同的結果,確保在編譯時涵蓋所有情況。

為什麼判別式單元對 C# 中的 PDF 產生有益?

判別聯合對 C# 中的 PDF 生成有利,因為它們提供了一種結構化的方式來處理成功和錯誤的情況。此方法可確保在編譯時處理潛在的問題,減少 PDF 建立過程中的執行時錯誤。

在 C# 中是否可以擴充判別聯合以獲得額外的功能?

是的,判別式單元可以使用擴充方法擴充額外的功能。這可讓您在不改變基本結構的情況下,新增自訂行為,例如檢查 PDF 產生的成功狀態。

在沒有本機支援的情況下,有沒有辦法在 C# 中模擬判別聯合?

是的,即使 C# 沒有原生支援判別式合併,但可以使用類別層級來模擬它們。一個抽象的基本類別可以搭配巢狀的類別來表示不同的可能結果,例如成功或失敗的情況。

C# 開發人員如何有效處理 PDF 產生過程中的錯誤?

C# 開發人員可透過使用判別聯合來建立可能結果的模型,有效處理 PDF 產生過程中的錯誤。此方法可確保在編譯時處理錯誤,改善程式碼的可靠性與可維護性。

在 C# 專案中使用 IronPDF 與判別式結合有哪些優點?

在 C# 專案中使用 IronPDF 與判別式結合,可在 PDF 生成過程中提供強大的錯誤處理優勢。這種組合可以清楚區分成功的操作和錯誤,增強程式碼的安全性和可靠性。

C# 中的判別聯合如何有助於類型安全?

Discriminated unions 可確保在編譯期間處理所有潛在的情況,有助於 C# 的類型安全。這可降低執行時出錯的可能性,並使程式碼更具可預期性且更容易維護。

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

Jacob Mellor 是 Iron Software 的首席技術官,作為 C# PDF 技術的先鋒工程師。作為 Iron Software 核心代碼的原作者,他自開始以來塑造了公司產品架構,與 CEO Cameron Rimington 一起將其轉變為一家擁有超過 50 名員工的公司,為 NASA、特斯拉 和 全世界政府機構服務。

Jacob 持有曼徹斯特大學土木工程一級榮譽学士工程學位(BEng) (1998-2001)。他於 1999 年在倫敦開設了他的第一家軟件公司,並於 2005 年製作了他的首個 .NET 組件,專注於解決 Microsoft 生態系統內的複雜問題。

他的旗艦產品 IronPDF & Iron Suite .NET 庫在全球 NuGet 被安裝超過 3000 萬次,其基礎代碼繼續為世界各地的開發工具提供動力。擁有 25 年的商業經驗和 41 年的編碼專業知識,Jacob 仍專注於推動企業級 C#、Java 及 Python PDF 技術的創新,同時指導新一代技術領袖。