.NET ヘルプ

C# 配列の長さ(開発者向けの動作方法)

更新済み 3月 6, 2024
共有:

配列は、C#における基本的なデータ構造であり、開発者が要素のコレクションを格納および操作することを可能にします。 配列を操作する際の重要な側面の一つは、配列の長さを理解することです。これは、配列の要素へのアクセス、操作、および繰り返し処理に直接影響します。 配列には多くの種類があり、一次元配列、ジャグ配列、または多次元配列など、複数の次元を持つことができます。

この包括的なガイドでは、以下の概念について詳しく掘り下げていきます C# 配列の長さプロパティその意味、測定方法、及びベストプラクティスをカバーします。 C# 配列と C# PDF ライブラリを使用して、PDF 配列を作成および検索することもできます。 IronPDF.

配列の長さとは何ですか?

C#では、配列の長さは保持できる要素の数を表します。 いくつかの動的データ構造とは異なり、配列のサイズは初期化時に固定されます。 (3次元の整数配列のように). 配列の長さは重要なパラメーターであり、さまざまな操作に影響を与え、適切なメモリ割り当てを保証します。

配列の長さの決定

2.1. Lengthプロパティの使用

C#配列の要素の長さを取得する最も簡単な方法は、Lengthプロパティを使用することです。 このプロパティはすべての配列インスタンスに固有であり、Length プロパティは要素の総数を返します。

int [] numbers = { 1, 2, 3, 4, 5 };
int arrayLength = numbers.Length; // arrayLength will be 5
int [] numbers = { 1, 2, 3, 4, 5 };
int arrayLength = numbers.Length; // arrayLength will be 5
Dim numbers() As Integer = { 1, 2, 3, 4, 5 }
Dim arrayLength As Integer = numbers.Length ' arrayLength will be 5
VB   C#

ループ反復

次のコードを使うよりも非効率的ですが、ループを使って配列を繰り返し処理することも、その長さを知る方法となります。

int [] numbers = { 1, 2, 3, 4, 5 };
int arrayLength = 0;
foreach (var item in numbers)
{
    arrayLength++;
}
// arrayLength will be 5
int [] numbers = { 1, 2, 3, 4, 5 };
int arrayLength = 0;
foreach (var item in numbers)
{
    arrayLength++;
}
// arrayLength will be 5
Dim numbers() As Integer = { 1, 2, 3, 4, 5 }
Dim arrayLength As Integer = 0
For Each item In numbers
	arrayLength += 1
Next item
' arrayLength will be 5
VB   C#

Lengthプロパティを使用することは、特に大規模な配列の場合、効率のために推奨されることに注意してください。

配列の長さと配列のランク

配列の長さと配列のランクの違いを理解することは重要です。 長さは、上記の例に示されているように、一次元配列の要素の総数を指します。 一方、ランクは多次元配列の次元数を表します。

int [] dimension = new int [5]; //One-dimensional int array, Length: 5, Rank: 1
string [,] dimensionTwo = new string [3, 4]; // Two-dimensional string array, Length: 3 * 4 = 12, Rank: 2
int [] dimension = new int [5]; //One-dimensional int array, Length: 5, Rank: 1
string [,] dimensionTwo = new string [3, 4]; // Two-dimensional string array, Length: 3 * 4 = 12, Rank: 2
Dim dimension(4) As Integer 'One-dimensional int array, Length: 5, Rank: 1
Dim dimensionTwo(2, 3) As String ' Two-dimensional string array, Length: 3 * 4 = 12, Rank: 2
VB   C#

これらの概念を区別することは、多次元配列と一次元配列を使用した適切な配列の初期化、操作、制御、アクセスのために不可欠です。

ベストプラクティスと考慮事項

4.1. 配列の長さとインデックス配列

配列の要素にアクセスする際は、常にインデックスが配列の長さの範囲内であることを確認してください。 有効な値の範囲外のインデックスにアクセスしようとすると、IndexOutOfRangeException が発生します。

int [] numbers = { 1, 2, 3, 4, 5 };
// Incorrect usage leading to IndexOutOfRangeException
int value = numbers [10]; // Avoid accessing elements beyond the array length
int [] numbers = { 1, 2, 3, 4, 5 };
// Incorrect usage leading to IndexOutOfRangeException
int value = numbers [10]; // Avoid accessing elements beyond the array length
Dim numbers() As Integer = { 1, 2, 3, 4, 5 }
' Incorrect usage leading to IndexOutOfRangeException
Dim value As Integer = numbers (10) ' Avoid accessing elements beyond the array length
VB   C#

4.2. ダイナミックリサイズ

配列の長さは初期化後に固定されることを忘れないでください。 動的なリサイズが必要な場合は、List のような他のデータ構造を使用することを検討してください動的に拡大または縮小できる。

List<int> dynamicList = new List<int>();
dynamicList.Add(1);
dynamicList.Add(2);
// No fixed length; the list can dynamically grow
List<int> dynamicList = new List<int>();
dynamicList.Add(1);
dynamicList.Add(2);
// No fixed length; the list can dynamically grow
Dim dynamicList As New List(Of Integer)()
dynamicList.Add(1)
dynamicList.Add(2)
' No fixed length; the list can dynamically grow
VB   C#

5. IronPDFの紹介

C# 配列の長さ (開発者向けの仕組み): 図1 - IronPDF ウェブページ

IronPDF は.NETアプリケーション内でPDFドキュメントを作成、操作、レンダリングすることができる強力なC#ライブラリです。 ウェブアプリケーション、デスクトップアプリケーション、またはその他の.NETプロジェクトに取り組んでいるかどうかにかかわらず、IronPDFはPDFの作成、編集、処理のプロセスを簡素化し、PDFファイルを生成、編集、処理するための強力な機能セットを提供します。

IronPDFの注目すべき機能はその HTMLからPDFへ レイアウトとスタイルをそのまま保持する機能。 それは、ウェブコンテンツからのPDF生成を可能にし、レポート、請求書、ドキュメントに最適です。 HTMLファイル、URL、HTML文字列は簡単に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");
    }
}
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
VB   C#

IronPDFを使用すると、開発者はPDF機能をアプリケーションにシームレスに統合でき、動的でインタラクティブなPDFドキュメントを作成することができます。 このソフトウェアは、HTMLからPDFの生成、既存のPDFへのテキストと画像の追加、PDFからのデータ抽出など、さまざまなタスクをサポートします。

5.1. IronPDFのインストール

NuGet パッケージ マネージャ コンソールを使用して IronPDF をインストールするには:

Install-Package IronPdf

このコマンドは、IronPDFライブラリとその依存関係を.NETプロジェクトにダウンロードしてインストールします。 インストール後、必要な名前空間をインポートすることでアプリケーションでIronPDFを使用し始めることができます。

5.2. IronPDF:C#配列を使用してPDF配列の長さを見つける

using IronPdf;
using System;
using System.Collections.Generic;
using System.Linq;
class Program
{
    public static void Main()
    {
        // PDF files to open
        string [] pdfFiles = { "GeneratedPDF_1.pdf", "GeneratedPDF_2.pdf", "GeneratedPDF_3.pdf" };
        PdfDocument [] pdfArray = new PdfDocument [3];
        // Loop to open each PDF and extract information
        foreach (string pdfFile in pdfFiles)
        {
            // Load PDF document
            var pdfDocument = PdfDocument.FromFile(pdfFile);
            pdfArray.Append(pdfDocument);
        }
        int arrayLength = pdfArray.Length;
        Console.WriteLine("PDF array Length: "+arrayLength);
    }
}
using IronPdf;
using System;
using System.Collections.Generic;
using System.Linq;
class Program
{
    public static void Main()
    {
        // PDF files to open
        string [] pdfFiles = { "GeneratedPDF_1.pdf", "GeneratedPDF_2.pdf", "GeneratedPDF_3.pdf" };
        PdfDocument [] pdfArray = new PdfDocument [3];
        // Loop to open each PDF and extract information
        foreach (string pdfFile in pdfFiles)
        {
            // Load PDF document
            var pdfDocument = PdfDocument.FromFile(pdfFile);
            pdfArray.Append(pdfDocument);
        }
        int arrayLength = pdfArray.Length;
        Console.WriteLine("PDF array Length: "+arrayLength);
    }
}
Imports IronPdf
Imports System
Imports System.Collections.Generic
Imports System.Linq
Friend Class Program
	Public Shared Sub Main()
		' PDF files to open
		Dim pdfFiles() As String = { "GeneratedPDF_1.pdf", "GeneratedPDF_2.pdf", "GeneratedPDF_3.pdf" }
		Dim pdfArray(2) As PdfDocument
		' Loop to open each PDF and extract information
		For Each pdfFile As String In pdfFiles
			' Load PDF document
			Dim pdfDocument = PdfDocument.FromFile(pdfFile)
			pdfArray.Append(pdfDocument)
		Next pdfFile
		Dim arrayLength As Integer = pdfArray.Length
		Console.WriteLine("PDF array Length: " & arrayLength)
	End Sub
End Class
VB   C#

このC#コードは、IronPDFライブラリを利用して既存のPDFファイルを開き、処理します。 これはPDFファイル名の配列を定義します (pdfFiles
(pdfFiles)) そして空の配列を作成します (pdfArray) PdfDocumentオブジェクトを保存するために。 ループを通して、IronPDFのPdfDocument.FromFileメソッドを使用して各PDFファイルを開き、各ファイルに対してPdfDocumentオブジェクトを作成します。その後、pdfArrayはこれらのオブジェクトで満たされます。 最後に、コードは結果のpdfArrayの長さをコンソールに出力し、処理および保存されたPDFの数に関する情報を提供します。

C# 配列の長さ(開発者向けの仕組み):図2 - 前のコード例からのコンソール出力

結論

この記事では、C#配列の長さに関連する重要な概念について包括的な概要を提供し、配列操作におけるその重要性を強調しました。 配列の長さを決定する方法、長さとランクの違い、およびベストプラクティスが検討されました。

ガイドではさらに IronPDF強力なC#ライブラリであるIronPDFを使用して、既存のPDFファイルを開き、PdfDocumentオブジェクトを作成し、それらを配列に保存するという実践的な使用方法を示しました。 この簡潔でありながら有益なガイドは、配列操作を習得し、アプリケーションで効率的なPDF関連のタスクのためにIronPDFを活用しようとするC#開発者にとって貴重なリソースです。

可能性をさらに探求し、IronPDFの完全な潜在能力を引き出すために、開発者は以下を利用することができます。 無料試用ライセンス IronPDFによって提供されています。 IronPDFを使用してPDFの生成や編集について詳しく知るには:訪問 [以下の内容を日本語に翻訳します:

ここに

ご希望のイディオムや技術用語が追加されることによって、より適切な翻訳が提供できる場合もありますので、詳細なコンテキストを教えていただけると幸いです。](/docs/),およびPDFファイルの読み取りについてのチュートリアルは、こちらをご覧ください リンク.

< 以前
HashSet C# (開発者向けの動作方法)
次へ >
C#で配列を初期化する方法(開発者向け)

準備はできましたか? バージョン: 2024.9 新発売

無料のNuGetダウンロード 総ダウンロード数: 10,659,073 View Licenses >