Zum Fußzeileninhalt springen
.NET HILFE

C# Pair-Klasse (Funktionsweise für Entwickler)

A pair is a simple data structure that holds two related values. It provides a convenient way to bundle two distinct pieces of data together. Pairs are commonly used when a method needs to return two values or when working with key-value associations.

In C#, developers often resort to using tuples (Tuple<T1, T2>) for pairing values. However, tuples are immutable, and their elements are accessed via properties like Item1 and Item2, which can lead to less readable code when used extensively. This is where a custom Pair class comes in handy.

If you require a structure to hold two related objects and data hiding is not a priority, you can utilize the Pair class in your code. The Pair class does not encapsulate its object references. Instead, it exposes them directly to all calling codes as public class fields.

This design choice allows for straightforward access to the contained objects without the overhead of encapsulation. Also, at the end of the article, we will explore how IronPDF for PDF Generation from Iron Software Overview can be used to generate a PDF document.

Tuples

C# 7.0 introduced tuple syntax improvements, making tuples even easier to work with. Here's how you can declare and initialize tuples:

// Tuple declaration
var person = (name: "John", age: 30);

// Accessing tuple elements using named properties
Console.WriteLine($"Name: {person.name}, Age: {person.age}");

// Tuple deconstruction
var (name, age) = person;
Console.WriteLine($"Name: {name}, Age: {age}");
// Tuple declaration
var person = (name: "John", age: 30);

// Accessing tuple elements using named properties
Console.WriteLine($"Name: {person.name}, Age: {person.age}");

// Tuple deconstruction
var (name, age) = person;
Console.WriteLine($"Name: {name}, Age: {age}");
' Tuple declaration
Dim person = (name:= "John", age:= 30)

' Accessing tuple elements using named properties
Console.WriteLine($"Name: {person.name}, Age: {person.age}")

' Tuple deconstruction
'INSTANT VB TODO TASK: VB has no equivalent to C# deconstruction declarations:
var(name, age) = person
Console.WriteLine($"Name: {name}, Age: {age}")
$vbLabelText   $csharpLabel

Benefits of Tuples

Concise Syntax

Tuples allow you to express complex data structures using a concise syntax without the need for defining custom classes or structs.

Lightweight

Tuples are lightweight data structures, making them suitable for scenarios where you need temporary or intermediate storage of data.

Implicit Naming

With tuple syntax, you can implicitly name tuple elements, enhancing code readability and reducing the need for comments.

Returning Multiple Values from Methods

public (int Quotient, int Remainder) Divide(int dividend, int divisor)
{
    int quotient = dividend / divisor;
    int remainder = dividend % divisor;
    return (quotient, remainder);
}

var result = Divide(10, 3);
Console.WriteLine($"Quotient: {result.Quotient}, Remainder: {result.Remainder}");
public (int Quotient, int Remainder) Divide(int dividend, int divisor)
{
    int quotient = dividend / divisor;
    int remainder = dividend % divisor;
    return (quotient, remainder);
}

var result = Divide(10, 3);
Console.WriteLine($"Quotient: {result.Quotient}, Remainder: {result.Remainder}");
Public Function Divide(ByVal dividend As Integer, ByVal divisor As Integer) As (Quotient As Integer, Remainder As Integer)
	Dim quotient As Integer = dividend \ divisor
	Dim remainder As Integer = dividend Mod divisor
	Return (quotient, remainder)
End Function

Private result = Divide(10, 3)
Console.WriteLine($"Quotient: {result.Quotient}, Remainder: {result.Remainder}")
$vbLabelText   $csharpLabel

Simplifying Method Signatures

public (string Name, string Surname) GetNameAndSurname()
{
    // Retrieve name and surname from a data source
    return ("John", "Doe");
}

var (name, surname) = GetNameAndSurname();
Console.WriteLine($"Name: {name}, Surname: {surname}");
public (string Name, string Surname) GetNameAndSurname()
{
    // Retrieve name and surname from a data source
    return ("John", "Doe");
}

var (name, surname) = GetNameAndSurname();
Console.WriteLine($"Name: {name}, Surname: {surname}");
Public Function GetNameAndSurname() As (Name As String, Surname As String)
	' Retrieve name and surname from a data source
	Return ("John", "Doe")
End Function

'INSTANT VB TODO TASK: VB has no equivalent to C# deconstruction declarations:
var(name, surname) = GetNameAndSurname()
Console.WriteLine($"Name: {name}, Surname: {surname}")
$vbLabelText   $csharpLabel
var point = (x: 10, y: 20);
var color = (r: 255, g: 0, b: 0);
var person = (name: "Alice", age: 25);
var point = (x: 10, y: 20);
var color = (r: 255, g: 0, b: 0);
var person = (name: "Alice", age: 25);
Dim point = (x:= 10, y:= 20)
Dim color = (r:= 255, g:= 0, b:= 0)
Dim person = (name:= "Alice", age:= 25)
$vbLabelText   $csharpLabel

Limitations and Considerations

While C# 7.0 tuples provide significant benefits, there are some limitations and considerations to keep in mind:

  • Tuples are limited in terms of expressiveness compared to custom classes or structs.
  • Tuple elements are accessed using Item1, Item2, etc. when explicit names are not provided, which can reduce code readability.

Pair Custom Class

public class Pair<T1, T2>
{
    public T1 First { get; set; }
    public T2 Second { get; set; }

    // Constructor to initialize the pair
    public Pair(T1 first, T2 second)
    {
        First = first;
        Second = second;
    }
}
public class Pair<T1, T2>
{
    public T1 First { get; set; }
    public T2 Second { get; set; }

    // Constructor to initialize the pair
    public Pair(T1 first, T2 second)
    {
        First = first;
        Second = second;
    }
}
Public Class Pair(Of T1, T2)
	Public Property First() As T1
	Public Property Second() As T2

	' Constructor to initialize the pair
	Public Sub New(ByVal first As T1, ByVal second As T2)
		Me.First = first
		Me.Second = second
	End Sub
End Class
$vbLabelText   $csharpLabel

In this class, the types are defined at the time of usage, and the two properties are exposed as public properties.

Using the Pair Class

Now, let's explore some common use cases where the Pair class can be beneficial:

1. Storing Coordinates

// Creating a new instance of the Pair class to store coordinates
Pair<int, int> coordinates = new Pair<int, int>(10, 20);
Console.WriteLine($"X: {coordinates.First}, Y: {coordinates.Second}");
// Creating a new instance of the Pair class to store coordinates
Pair<int, int> coordinates = new Pair<int, int>(10, 20);
Console.WriteLine($"X: {coordinates.First}, Y: {coordinates.Second}");
' Creating a new instance of the Pair class to store coordinates
Dim coordinates As New Pair(Of Integer, Integer)(10, 20)
Console.WriteLine($"X: {coordinates.First}, Y: {coordinates.Second}")
$vbLabelText   $csharpLabel

2. Returning Multiple Values from a Method

// Method returning a Pair, representing both quotient and remainder
public Pair<int, int> Divide(int dividend, int divisor)
{
    int quotient = dividend / divisor;
    int remainder = dividend % divisor;
    return new Pair<int, int>(quotient, remainder);
}

// Usage
Pair<int, int> result = Divide(10, 3);
Console.WriteLine($"Quotient: {result.First}, Remainder: {result.Second}");
// Method returning a Pair, representing both quotient and remainder
public Pair<int, int> Divide(int dividend, int divisor)
{
    int quotient = dividend / divisor;
    int remainder = dividend % divisor;
    return new Pair<int, int>(quotient, remainder);
}

// Usage
Pair<int, int> result = Divide(10, 3);
Console.WriteLine($"Quotient: {result.First}, Remainder: {result.Second}");
' Method returning a Pair, representing both quotient and remainder
Public Function Divide(ByVal dividend As Integer, ByVal divisor As Integer) As Pair(Of Integer, Integer)
	Dim quotient As Integer = dividend \ divisor
	Dim remainder As Integer = dividend Mod divisor
	Return New Pair(Of Integer, Integer)(quotient, remainder)
End Function

' Usage
Private result As Pair(Of Integer, Integer) = Divide(10, 3)
Console.WriteLine($"Quotient: {result.First}, Remainder: {result.Second}")
$vbLabelText   $csharpLabel

3. Storing Key-Value Pairs

// Storing a key-value pair
Pair<string, int> keyValue = new Pair<string, int>("Age", 30);
Console.WriteLine($"Key: {keyValue.First}, Value: {keyValue.Second}");
// Storing a key-value pair
Pair<string, int> keyValue = new Pair<string, int>("Age", 30);
Console.WriteLine($"Key: {keyValue.First}, Value: {keyValue.Second}");
' Storing a key-value pair
Dim keyValue As New Pair(Of String, Integer)("Age", 30)
Console.WriteLine($"Key: {keyValue.First}, Value: {keyValue.Second}")
$vbLabelText   $csharpLabel

Key-Value Pairs

Key-value pairs provide a simple and efficient way to associate data. In C#, the primary tool for working with key-value pairs is the Dictionary<TKey, TValue> class, a versatile and powerful collection type.

Understanding Key-Value Pairs

A key-value pair is a data structure that associates a unique key with a value. This association allows for efficient retrieval and manipulation of data based on its unique identifier. In C#, key-value pairs are commonly used for tasks such as caching, configuration management, and data storage.

Dictionary<TKey, TValue> in C#

The Dictionary<TKey, TValue> class in C# is a generic collection that stores key-value pairs. It provides fast lookups based on the keys and is widely used for managing associative data.

Creating and Populating a Dictionary

Dictionary<string, int> ages = new Dictionary<string, int>
{
    { "Alice", 30 },
    { "Bob", 35 },
    { "Charlie", 25 }
};
Dictionary<string, int> ages = new Dictionary<string, int>
{
    { "Alice", 30 },
    { "Bob", 35 },
    { "Charlie", 25 }
};
Dim ages As New Dictionary(Of String, Integer) From {
	{"Alice", 30},
	{"Bob", 35},
	{"Charlie", 25}
}
$vbLabelText   $csharpLabel

Accessing Values by Key

// Directly access a value by its key
Console.WriteLine($"Alice's age: {ages["Alice"]}");
// Directly access a value by its key
Console.WriteLine($"Alice's age: {ages["Alice"]}");
' Directly access a value by its key
Console.WriteLine($"Alice's age: {ages("Alice")}")
$vbLabelText   $csharpLabel

Iterating Over Key-Value Pairs

// Iterate over all key-value pairs in the dictionary
foreach (var pair in ages)
{
    Console.WriteLine($"Name: {pair.Key}, Age: {pair.Value}");
}
// Iterate over all key-value pairs in the dictionary
foreach (var pair in ages)
{
    Console.WriteLine($"Name: {pair.Key}, Age: {pair.Value}");
}
' Iterate over all key-value pairs in the dictionary
For Each pair In ages
	Console.WriteLine($"Name: {pair.Key}, Age: {pair.Value}")
Next pair
$vbLabelText   $csharpLabel

Advanced Scenarios

Handling Missing Keys

if (ages.TryGetValue("David", out int age))
{
    Console.WriteLine($"David's age: {age}");
}
else
{
    Console.WriteLine("David's age is not available.");
}
if (ages.TryGetValue("David", out int age))
{
    Console.WriteLine($"David's age: {age}");
}
else
{
    Console.WriteLine("David's age is not available.");
}
Dim age As Integer
If ages.TryGetValue("David", age) Then
	Console.WriteLine($"David's age: {age}")
Else
	Console.WriteLine("David's age is not available.")
End If
$vbLabelText   $csharpLabel

Removing Entries

// Remove an entry given its key
ages.Remove("Charlie");
// Remove an entry given its key
ages.Remove("Charlie");
' Remove an entry given its key
ages.Remove("Charlie")
$vbLabelText   $csharpLabel

Dictionary Initialization

// Initialize a dictionary with color codes
var colors = new Dictionary<string, string>
{
    { "red", "#FF0000" },
    { "green", "#00FF00" },
    { "blue", "#0000FF" }
};
// Initialize a dictionary with color codes
var colors = new Dictionary<string, string>
{
    { "red", "#FF0000" },
    { "green", "#00FF00" },
    { "blue", "#0000FF" }
};
' Initialize a dictionary with color codes
Dim colors = New Dictionary(Of String, String) From {
	{"red", "#FF0000"},
	{"green", "#00FF00"},
	{"blue", "#0000FF"}
}
$vbLabelText   $csharpLabel

Beyond Dictionary: Alternatives and Considerations

While Dictionary<TKey, TValue> is a powerful tool, alternative approaches, and considerations depend on the specific requirements of your application:

  • ConcurrentDictionary<TKey, TValue>: If your application requires thread-safe access to the dictionary from multiple threads, consider using ConcurrentDictionary<TKey, TValue>.
  • ImmutableDictionary<TKey, TValue>: For scenarios where immutability is desired, ImmutableDictionary<TKey, TValue> from the System.Collections.Immutable namespace provides immutable key-value collections.
  • Custom Key-Value Pair Classes: In situations where you need additional functionality or specific behavior, consider creating custom key-value pair classes tailored to your requirements.

IronPDF Library

IronPDF by Iron Software Products is an excellent library for generating PDF documents. Its ease of use and efficiency are second to none.

IronPDF excels in HTML to PDF conversion, ensuring precise preservation of original layouts and styles. It's perfect for creating PDFs from web-based content such as reports, invoices, and documentation. With support for HTML files, URLs, and raw HTML strings, IronPDF easily produces high-quality PDF documents.

using IronPdf;

class Program
{
    static void Main(string[] args)
    {
        var renderer = new ChromePdfRenderer();

        // 1. Convert HTML String to PDF
        var htmlContent = "<h1>Hello, IronPDF!</h1><p>This is a PDF from an HTML string.</p>";
        var pdfFromHtmlString = renderer.RenderHtmlAsPdf(htmlContent);
        pdfFromHtmlString.SaveAs("HTMLStringToPDF.pdf");

        // 2. Convert HTML File to PDF
        var htmlFilePath = "path_to_your_html_file.html"; // Specify the path to your HTML file
        var pdfFromHtmlFile = renderer.RenderHtmlFileAsPdf(htmlFilePath);
        pdfFromHtmlFile.SaveAs("HTMLFileToPDF.pdf");

        // 3. Convert URL to PDF
        var url = "http://ironpdf.com"; // Specify the URL
        var pdfFromUrl = renderer.RenderUrlAsPdf(url);
        pdfFromUrl.SaveAs("URLToPDF.pdf");
    }
}
using IronPdf;

class Program
{
    static void Main(string[] args)
    {
        var renderer = new ChromePdfRenderer();

        // 1. Convert HTML String to PDF
        var htmlContent = "<h1>Hello, IronPDF!</h1><p>This is a PDF from an HTML string.</p>";
        var pdfFromHtmlString = renderer.RenderHtmlAsPdf(htmlContent);
        pdfFromHtmlString.SaveAs("HTMLStringToPDF.pdf");

        // 2. Convert HTML File to PDF
        var htmlFilePath = "path_to_your_html_file.html"; // Specify the path to your HTML file
        var pdfFromHtmlFile = renderer.RenderHtmlFileAsPdf(htmlFilePath);
        pdfFromHtmlFile.SaveAs("HTMLFileToPDF.pdf");

        // 3. Convert URL to PDF
        var url = "http://ironpdf.com"; // Specify the URL
        var pdfFromUrl = renderer.RenderUrlAsPdf(url);
        pdfFromUrl.SaveAs("URLToPDF.pdf");
    }
}
Imports IronPdf

Friend Class Program
	Shared Sub Main(ByVal args() As String)
		Dim renderer = New ChromePdfRenderer()

		' 1. Convert HTML String to PDF
		Dim htmlContent = "<h1>Hello, IronPDF!</h1><p>This is a PDF from an HTML string.</p>"
		Dim pdfFromHtmlString = renderer.RenderHtmlAsPdf(htmlContent)
		pdfFromHtmlString.SaveAs("HTMLStringToPDF.pdf")

		' 2. Convert HTML File to PDF
		Dim htmlFilePath = "path_to_your_html_file.html" ' Specify the path to your HTML file
		Dim pdfFromHtmlFile = renderer.RenderHtmlFileAsPdf(htmlFilePath)
		pdfFromHtmlFile.SaveAs("HTMLFileToPDF.pdf")

		' 3. Convert URL to PDF
		Dim url = "http://ironpdf.com" ' Specify the URL
		Dim pdfFromUrl = renderer.RenderUrlAsPdf(url)
		pdfFromUrl.SaveAs("URLToPDF.pdf")
	End Sub
End Class
$vbLabelText   $csharpLabel

IronPDF can be installed from the NuGet package manager:

Install-Package IronPdf

Or from Visual Studio like this:

C# Pair Class (How It Works For Developers): Figure 1 - Installing IronPDF with the NuGet package manager

To generate a document with a tuple example, we can use the following code:

using IronPdf;

namespace IronPatterns
{
    class Program
    {
        static void Main()
        {
            Console.WriteLine("-----------Iron Software-------------");
            var renderer = new ChromePdfRenderer(); // var pattern
            var content = "<h1>Iron Software is Awesome</h1> Made with IronPDF!";
            content += "<h2>Demo C# Pair with Tuples</h2>";

            var result = Divide(10, 3);
            Console.WriteLine($"Quotient: {result.Item1}, Remainder: {result.Item2}");
            content += $"<p>When we divide 10 by 3:</p>";
            content += $"<p>Quotient: {result.Item1}, Remainder: {result.Item2}</p>";

            var pdf = renderer.RenderHtmlAsPdf(content);
            pdf.SaveAs("output.pdf"); // Saves PDF
        }

        // Method to demonstrate division using tuples
        public static (int Quotient, int Remainder) Divide(int dividend, int divisor)
        {
            int quotient = dividend / divisor;
            int remainder = dividend % divisor;
            return (quotient, remainder);
        }
    }
}
using IronPdf;

namespace IronPatterns
{
    class Program
    {
        static void Main()
        {
            Console.WriteLine("-----------Iron Software-------------");
            var renderer = new ChromePdfRenderer(); // var pattern
            var content = "<h1>Iron Software is Awesome</h1> Made with IronPDF!";
            content += "<h2>Demo C# Pair with Tuples</h2>";

            var result = Divide(10, 3);
            Console.WriteLine($"Quotient: {result.Item1}, Remainder: {result.Item2}");
            content += $"<p>When we divide 10 by 3:</p>";
            content += $"<p>Quotient: {result.Item1}, Remainder: {result.Item2}</p>";

            var pdf = renderer.RenderHtmlAsPdf(content);
            pdf.SaveAs("output.pdf"); // Saves PDF
        }

        // Method to demonstrate division using tuples
        public static (int Quotient, int Remainder) Divide(int dividend, int divisor)
        {
            int quotient = dividend / divisor;
            int remainder = dividend % divisor;
            return (quotient, remainder);
        }
    }
}
Imports IronPdf

Namespace IronPatterns
	Friend Class Program
		Shared Sub Main()
			Console.WriteLine("-----------Iron Software-------------")
			Dim renderer = New ChromePdfRenderer() ' var pattern
			Dim content = "<h1>Iron Software is Awesome</h1> Made with IronPDF!"
			content &= "<h2>Demo C# Pair with Tuples</h2>"

			Dim result = Divide(10, 3)
			Console.WriteLine($"Quotient: {result.Item1}, Remainder: {result.Item2}")
			content &= $"<p>When we divide 10 by 3:</p>"
			content &= $"<p>Quotient: {result.Item1}, Remainder: {result.Item2}</p>"

			Dim pdf = renderer.RenderHtmlAsPdf(content)
			pdf.SaveAs("output.pdf") ' Saves PDF
		End Sub

		' Method to demonstrate division using tuples
		Public Shared Function Divide(ByVal dividend As Integer, ByVal divisor As Integer) As (Quotient As Integer, Remainder As Integer)
			Dim quotient As Integer = dividend \ divisor
			Dim remainder As Integer = dividend Mod divisor
			Return (quotient, remainder)
		End Function
	End Class
End Namespace
$vbLabelText   $csharpLabel

Output

C# Pair Class (How It Works For Developers): Figure 2

Trial License for IronPDF

Get your IronPDF Trial License and place the license in the appsettings.json.

{
    "IronPDF.LicenseKey": "<Your Key>"
}

Conclusion

In this article, we've explored the concept of pairs and the importance of having a Pair class in C#. We've provided a simple implementation of the Pair custom class along with various use cases demonstrating its versatility and utility in everyday programming tasks.

Whether you're working with coordinates, returning multiple values from a method, or storing key-value associations, the Pair class can be a valuable addition to your programming skill set.

In addition to this, the IronPDF library functionality is a great combination skill set to have for developers to generate PDF documents on the fly as required in applications.

Häufig gestellte Fragen

Was ist eine Pair-Klasse in C#?

Eine Pair-Klasse in C# ist eine einfache Datenstruktur, die entwickelt wurde, um zwei verwandte Werte zu halten. Sie ermöglicht einen einfachen Zugriff auf ihre Eigenschaften als öffentliche Felder und ist eine bequeme Alternative zu Tupeln, wenn Kapselung keine Priorität hat.

Wie unterscheidet sich die Pair-Klasse von einem Tuple in C#?

Die Pair-Klasse unterscheidet sich von einem Tuple dadurch, dass sie ihre Objektverweise direkt über öffentliche Felder offenlegt, was die Lesbarkeit und Flexibilität erhöht. Tupel hingegen sind unveränderlich und greifen über Eigenschaften wie Item1 und Item2 auf ihre Elemente zu.

Was sind die Vorteile der Verwendung der Pair-Klasse gegenüber Tupeln?

Die Vorteile der Verwendung der Pair-Klasse im Vergleich zu Tupeln sind eine verbesserte Code-Lesbarkeit durch die Verwendung beschreibender Eigenschaftsnamen anstelle von Item1 und Item2 sowie die Möglichkeit, die Werte zu ändern, da Paare veränderlich sind.

Kann ich die Pair-Klasse verwenden, um Schlüssel-Wert-Paare zu speichern?

Ja, die Pair-Klasse ist besonders nützlich, um Schlüssel-Wert-Paare in einer lesbareren Weise im Vergleich zu Tupeln zu speichern, da sie durch öffentliche Felder direkten Zugriff auf Werte bietet.

Was sind einige häufige Szenarien für die Verwendung der Pair-Klasse in C#?

Häufige Szenarien für die Verwendung der Pair-Klasse umfassen das Speichern von Koordinaten, das Zurückgeben mehrerer Werte aus einer Methode und das Verwalten von Schlüssel-Wert-Paar-Associationen in einem lesbaren Format.

Warum würde ein Entwickler die IronPDF-Bibliothek verwenden?

Ein Entwickler könnte die IronPDF-Bibliothek nutzen, um PDFs aus HTML-Inhalten zu erstellen. Sie stellt sicher, dass das ursprüngliche Layout und der Stil erhalten bleiben, was die Erstellung professioneller Dokumente wie Berichte und Rechnungen vereinfacht.

Wie kann ich ein PDF aus einer HTML-Datei in C# erstellen?

Sie können ein PDF aus einer HTML-Datei in C# mit der IronPDF-Bibliothek erstellen. Sie bietet Methoden wie RenderHtmlAsPdf zum Umwandeln von HTML-Strings und -Dateien in hochwertige PDF-Dokumente.

Was ist der Vorteil der Verwendung einer Bibliothek zur PDF-Erstellung?

Die Verwendung einer Bibliothek wie IronPDF zur PDF-Erstellung bietet vereinfachte Prozesse zur Erstellung hochwertiger PDF-Dokumente und stellt sicher, dass Layouts und Stile aus verschiedenen Inhaltsquellen genau beibehalten werden.

Welche Rollen spielen die Pair-Klasse und die IronPDF-Bibliothek im Werkzeugkasten eines Entwicklers?

Die Pair-Klasse und die IronPDF-Bibliothek erweitern den Werkzeugkasten eines Entwicklers, indem sie eine effiziente Datenstrukturverwaltung mit Paaren und zuverlässige Dokumentenerstellungsmöglichkeiten mit IronPDF bieten, was sie wertvoll für den Umgang mit komplexen Daten und Dokumenten-Workflows macht.

Curtis Chau
Technischer Autor

Curtis Chau hat einen Bachelor-Abschluss in Informatik von der Carleton University und ist spezialisiert auf Frontend-Entwicklung mit Expertise in Node.js, TypeScript, JavaScript und React. Leidenschaftlich widmet er sich der Erstellung intuitiver und ästhetisch ansprechender Benutzerschnittstellen und arbeitet gerne mit modernen Frameworks sowie der Erstellung gut strukturierter, optisch ansprechender ...

Weiterlesen