Test in production without watermarks.
Works wherever you need it to.
Get 30 days of fully functional product.
Have it up and running in minutes.
Full access to our support engineering team during your product trial
C# has become a popular and adaptable option for developers among the many available programming languages. The concept of collections is at the core of C#'s extensive library and frameworks, which are one of the language's main advantages. In C#, a collection is essential for effectively storing and organizing data. They give developers a wide range of effective tools to solve challenging programming problems. We'll delve further into collections in this post, covering their features, types, and optimal usage strategies.
Collections in C# are containers that let programmers work with and store sets of object classes. These objects are flexible and adaptable to many programming environments, and they might be of the same or distinct kinds. Most collection classes implement components of the System namespace in C#, meaning importing namespaces such as System.Collections and System.Collections.Generic, which offers various collection classes that are both generic and non-generic. Collections also allow for dynamic memory allocation, adding, searching, and sorting items within the collection classes.
ArrayList, Hashtable, and Queue are a few of the non-generic collection classes available in C# that were included in the first iterations of the language. These collections offer an alternative to explicitly defining the types of things you want to keep and work with. However, developers frequently choose generic collections because of their superior performance and type safety.
Later iterations of C# included generic collections to overcome the drawbacks of non-generic collections. They provide type safety during compilation and let developers deal with tightly typed data. The generic collection classes List
A dynamic array that facilitates quick and easy element insertion and removal is the List
// Creating a list with integers and adding/removing elements
List<int> numbers = new List<int> { 1, 2, 3, 4, 5 };
numbers.Add(6); // Adds element '6' to the end
numbers.Remove(3); // Removes the first occurrence of the element '3'
// Creating a list with integers and adding/removing elements
List<int> numbers = new List<int> { 1, 2, 3, 4, 5 };
numbers.Add(6); // Adds element '6' to the end
numbers.Remove(3); // Removes the first occurrence of the element '3'
' Creating a list with integers and adding/removing elements
Dim numbers As New List(Of Integer) From {1, 2, 3, 4, 5}
numbers.Add(6) ' Adds element '6' to the end
numbers.Remove(3) ' Removes the first occurrence of the element '3'
With quick lookup speeds, a collection of key-value pairs is represented by the Dictionary<TKey, TValue> class. It is frequently employed in situations where having rapid access to data via a unique key value is crucial. This key is used to access elements within the dictionary.
// Creating a dictionary mapping names to ages
Dictionary<string, int> ageMap = new Dictionary<string, int>();
ageMap.Add("John", 25); // The string "John" is the key that can access the value 25
ageMap["Jane"] = 30; // Setting the key "Jane" to hold the value 30
// Creating a dictionary mapping names to ages
Dictionary<string, int> ageMap = new Dictionary<string, int>();
ageMap.Add("John", 25); // The string "John" is the key that can access the value 25
ageMap["Jane"] = 30; // Setting the key "Jane" to hold the value 30
' Creating a dictionary mapping names to ages
Dim ageMap As New Dictionary(Of String, Integer)()
ageMap.Add("John", 25) ' The string "John" is the key that can access the value 25
ageMap("Jane") = 30 ' Setting the key "Jane" to hold the value 30
The first-in, first-out collection (FIFO), and last-in, first-out (LIFO) paradigms are implemented, respectively, by the generic Queue
// Creating and manipulating a queue
Queue<string> tasks = new Queue<string>();
tasks.Enqueue("Task 1"); // Adding to the queue
tasks.Enqueue("Task 2");
// Creating and manipulating a stack
Stack<double> numbers = new Stack<double>();
numbers.Push(3.14); // Adding to the stack
numbers.Push(2.71);
// Creating and manipulating a queue
Queue<string> tasks = new Queue<string>();
tasks.Enqueue("Task 1"); // Adding to the queue
tasks.Enqueue("Task 2");
// Creating and manipulating a stack
Stack<double> numbers = new Stack<double>();
numbers.Push(3.14); // Adding to the stack
numbers.Push(2.71);
' Creating and manipulating a queue
Dim tasks As New Queue(Of String)()
tasks.Enqueue("Task 1") ' Adding to the queue
tasks.Enqueue("Task 2")
' Creating and manipulating a stack
Dim numbers As New Stack(Of Double)()
numbers.Push(3.14) ' Adding to the stack
numbers.Push(2.71)
Unique items arranged in an unordered collection are represented by the HashSet
// Creating hashsets and performing a union operation
HashSet<int> setA = new HashSet<int> { 1, 2, 3, 4 };
HashSet<int> setB = new HashSet<int> { 3, 4, 5, 6 };
HashSet<int> unionSet = new HashSet<int>(setA);
unionSet.UnionWith(setB); // Combining setA and setB
// Creating hashsets and performing a union operation
HashSet<int> setA = new HashSet<int> { 1, 2, 3, 4 };
HashSet<int> setB = new HashSet<int> { 3, 4, 5, 6 };
HashSet<int> unionSet = new HashSet<int>(setA);
unionSet.UnionWith(setB); // Combining setA and setB
' Creating hashsets and performing a union operation
Dim setA As New HashSet(Of Integer) From {1, 2, 3, 4}
Dim setB As New HashSet(Of Integer) From {3, 4, 5, 6}
Dim unionSet As New HashSet(Of Integer)(setA)
unionSet.UnionWith(setB) ' Combining setA and setB
A C# library called IronPDF makes it easy to create, edit, and display PDF documents in .NET applications. It offers many licensing choices, cross-platform compatibility, high-quality rendering, and HTML to PDF conversion. IronPDF's user-friendly API makes handling PDFs easier, making it a valuable tool for C# developers.
The standout feature of IronPDF is its HTML to PDF conversion capability, which maintains all layouts and styles. It generates PDFs from web content, making it perfect for reports, invoices, and documentation. HTML files, URLs, and HTML strings can be converted to PDFs effortlessly.
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
Key features of IronPDF include:
To know more about the IronPDF documentation, refer to the IronPDF Documentation.
Install the IronPDF library first using the Package Manager Console or NuGet Package Manager with:
Install-Package IronPdf
Using the NuGet Package Manager to search for the package "IronPDF" is an additional option. We may choose and download the necessary package from this list out of all the NuGet packages associated with IronPDF.
Understanding the role that collections play in data structures and organization is crucial before we dive into the interface with IronPDF. Developers may store, retrieve, and modify groupings of things in an organized manner by using collections. With so many different types available, such as List
Imagine that you have to create a report with a list of sales transactions in it. The data may be effectively organized using a List
// Define the Transaction class
public class Transaction
{
public string ProductName { get; set; }
public decimal Amount { get; set; }
public DateTime Date { get; set; }
}
// Create a list of transactions
List<Transaction> transactions = new List<Transaction>
{
new Transaction { ProductName = "Product A", Amount = 100.50m, Date = DateTime.Now.AddDays(-2) },
new Transaction { ProductName = "Product B", Amount = 75.20m, Date = DateTime.Now.AddDays(-1) },
// Add more transactions as needed
};
// Define the Transaction class
public class Transaction
{
public string ProductName { get; set; }
public decimal Amount { get; set; }
public DateTime Date { get; set; }
}
// Create a list of transactions
List<Transaction> transactions = new List<Transaction>
{
new Transaction { ProductName = "Product A", Amount = 100.50m, Date = DateTime.Now.AddDays(-2) },
new Transaction { ProductName = "Product B", Amount = 75.20m, Date = DateTime.Now.AddDays(-1) },
// Add more transactions as needed
};
' Define the Transaction class
Public Class Transaction
Public Property ProductName() As String
Public Property Amount() As Decimal
Public Property [Date]() As DateTime
End Class
' Create a list of transactions
Private transactions As New List(Of Transaction) From {
New Transaction With {
.ProductName = "Product A",
.Amount = 100.50D,
.Date = DateTime.Now.AddDays(-2)
},
New Transaction With {
.ProductName = "Product B",
.Amount = 75.20D,
.Date = DateTime.Now.AddDays(-1)
}
}
In the PDF, we'll make a straightforward table that lists the product name, transaction amount, and date for each.
using IronPdf;
// Create a PDF document renderer
var pdfDocument = new HtmlToPdf();
// HTML content with a table populated by data from the 'transactions' list
string htmlContent = "<table><tr><th>Product Name</th><th>Amount</th><th>Date</th></tr>";
foreach (var transaction in transactions)
{
htmlContent += $"<tr><td>{transaction.ProductName}</td><td>{transaction.Amount}</td><td>{transaction.Date.ToShortDateString()}</td></tr>";
}
htmlContent += "</table>";
// Convert HTML to PDF
PdfDocument pdf = pdfDocument.RenderHtmlAsPdf(htmlContent);
// Specify the file path to save the PDF
string pdfFilePath = "transactions_report.pdf";
pdf.SaveAs(pdfFilePath);
using IronPdf;
// Create a PDF document renderer
var pdfDocument = new HtmlToPdf();
// HTML content with a table populated by data from the 'transactions' list
string htmlContent = "<table><tr><th>Product Name</th><th>Amount</th><th>Date</th></tr>";
foreach (var transaction in transactions)
{
htmlContent += $"<tr><td>{transaction.ProductName}</td><td>{transaction.Amount}</td><td>{transaction.Date.ToShortDateString()}</td></tr>";
}
htmlContent += "</table>";
// Convert HTML to PDF
PdfDocument pdf = pdfDocument.RenderHtmlAsPdf(htmlContent);
// Specify the file path to save the PDF
string pdfFilePath = "transactions_report.pdf";
pdf.SaveAs(pdfFilePath);
Imports IronPdf
' Create a PDF document renderer
Private pdfDocument = New HtmlToPdf()
' HTML content with a table populated by data from the 'transactions' list
Private htmlContent As String = "<table><tr><th>Product Name</th><th>Amount</th><th>Date</th></tr>"
For Each transaction In transactions
htmlContent &= $"<tr><td>{transaction.ProductName}</td><td>{transaction.Amount}</td><td>{transaction.Date.ToShortDateString()}</td></tr>"
Next transaction
htmlContent &= "</table>"
' Convert HTML to PDF
Dim pdf As PdfDocument = pdfDocument.RenderHtmlAsPdf(htmlContent)
' Specify the file path to save the PDF
Dim pdfFilePath As String = "transactions_report.pdf"
pdf.SaveAs(pdfFilePath)
Developers have the option to save the PDF document to disk or show it to users once it has been produced. IronPDF offers several output choices, including browser streaming, file saving, and cloud storage integration.
The above screen shows the output generated from the above code. To learn more about the code, refer to Using HTML to Create a PDF Example.
A plethora of opportunities for dynamic document production are made possible by combining collections with IronPDF. Developers may effectively manage and organize data by utilizing collections, and IronPDF makes it easy to create visually beautiful PDF documents. The combined power of IronPDF and collections offers a reliable and adaptable solution for dynamic content production in C# applications, regardless of the kind of document you're producing—invoices, reports, or anything else.
IronPDF's $749 Lite edition includes a year of software support, upgrade options, and a permanent license. Users also get the opportunity to evaluate the product in real-world circumstances during the watermarked trial period. To learn more about IronPDF's cost, licensing, and free trial, kindly visit the IronPDF Licensing Information. For more information on Iron Software, go to the Iron Software Website.
Collections in C# are containers that allow developers to store and manipulate groups of object classes. They offer dynamic memory allocation, adding, searching, and sorting functionalities.
C# offers both non-generic and generic collections. Non-generic collections like ArrayList and Hashtable are less type-safe, while generic collections like List
Generic collections are preferred because they provide type safety during compilation, better performance, and the ability to handle tightly typed data.
To create a list in C#, you can use the List
IronPDF is a C# library that simplifies the creation, editing, and display of PDF documents in .NET applications. It supports HTML to PDF conversion, high-quality rendering, and is cross-platform compatible.
You can use IronPDF's ChromePdfRenderer class to convert HTML strings, files, or URLs to PDFs. For example, using 'renderer.RenderHtmlAsPdf(htmlContent)' converts an HTML string to a PDF.
Key features of IronPDF include HTML to PDF conversion, PDF generation and manipulation, superior rendering, cross-platform compatibility, and performance optimization.
Collections help organize data efficiently for document creation. For example, a List
IronPDF can be installed using the NuGet Package Manager with the command 'Install-Package IronPdf'. You can also search for 'IronPDF' in the NuGet Package Manager to install it.
IronPDF offers a Lite license, which includes a year of software support and upgrade options. There is also a watermarked trial period for evaluation. More information is available on the IronPDF licensing page.