.NETヘルプ C#インデックス付きforeach(開発者向けの仕組み) Curtis Chau 更新日:7月 22, 2025 Download IronPDF NuGet Download テキストの検索と置換 テキストと画像のスタンプ Start Free Trial Copy for LLMs Copy for LLMs Copy page as Markdown for LLMs Open in ChatGPT Ask ChatGPT about this page Open in Gemini Ask Gemini about this page Open in Grok Ask Grok about this page Open in Perplexity Ask Perplexity about this page Share Share on Facebook Share on X (Twitter) Share on LinkedIn Copy URL Email article In C#, the foreach statement is typically used to iterate over collections like arrays, lists, or other enumerable types. However, one limitation is that the foreach loop doesn't provide a built-in index variable to track the current iteration. Developers often need to access the current element's index. Below, we'll explore various ways to implement this functionality and the IronPDF library. The Basics of the foreach Loop The foreach loop is designed to simplify iterating through arrays, lists, dictionaries, and other types that implement IEnumerable. Here's a basic example of how to use a foreach statement to loop through an array of integers data type: int[] numbers = { 10, 20, 30, 40 }; foreach (int number in numbers) { Console.WriteLine(number); } int[] numbers = { 10, 20, 30, 40 }; foreach (int number in numbers) { Console.WriteLine(number); } Dim numbers() As Integer = { 10, 20, 30, 40 } For Each number As Integer In numbers Console.WriteLine(number) Next number $vbLabelText $csharpLabel In this example, the number represents the element of the collection during each iteration. The loop automatically iterates through all the elements in the array. However, there is no built-in way to access the current element's index. Handling the Index in a foreach Loop Although C# does not directly provide the index in a foreach loop, several techniques can solve this. Let's discuss these methods in detail. Method 1: Using a Separate Variable One of the simplest ways to get the current element's index is to use an external index variable. You'll need to increment it manually inside the loop: int[] numbers = { 10, 20, 30, 40 }; int numberIndex = 0; foreach (int number in numbers) { Console.WriteLine($"Index: {numberIndex}, Value: {number}"); numberIndex++; } int[] numbers = { 10, 20, 30, 40 }; int numberIndex = 0; foreach (int number in numbers) { Console.WriteLine($"Index: {numberIndex}, Value: {number}"); numberIndex++; } Dim numbers() As Integer = { 10, 20, 30, 40 } Dim numberIndex As Integer = 0 For Each number As Integer In numbers Console.WriteLine($"Index: {numberIndex}, Value: {number}") numberIndex += 1 Next number $vbLabelText $csharpLabel In this code, the index variable is initialized before the loop starts and then incremented inside the loop during each iteration. While this approach works, it requires manually maintaining the index, which isn't always ideal. Method 2: Using LINQ's Select Method LINQ's Select method can be used to project each element of a collection into a new form, including its index. 以下は例です: int[] numbers = { 10, 20, 30, 40 }; foreach (var item in numbers.Select((value, index) => new { value, index })) { Console.WriteLine($"Index: {item.index}, Value: {item.value}"); } int[] numbers = { 10, 20, 30, 40 }; foreach (var item in numbers.Select((value, index) => new { value, index })) { Console.WriteLine($"Index: {item.index}, Value: {item.value}"); } Dim numbers() As Integer = { 10, 20, 30, 40 } For Each item In numbers.Select(Function(value, index) New With { Key value, Key index }) Console.WriteLine($"Index: {item.index}, Value: {item.value}") Next item $vbLabelText $csharpLabel In this example, Select creates an anonymous object that contains both the current element's value and its index. The foreach loop can then iterate over these objects and access both the index and the value directly. Method 3: Using a Custom Iterator You can implement a custom iterator extension method using the yield return keyword to generate a method that yields both the current element and its index. This is a bit more advanced but offers a flexible solution. public static IEnumerable<(int index, T value)> WithIndex<T>(this IEnumerable<T> source) { int index = 0; foreach (T value in source) { yield return (index, value); index++; } } public static IEnumerable<(int index, T value)> WithIndex<T>(this IEnumerable<T> source) { int index = 0; foreach (T value in source) { yield return (index, value); index++; } } <System.Runtime.CompilerServices.Extension> _ Public Function WithIndex(Of T)(ByVal source As IEnumerable(Of T)) As IEnumerable(Of (index As Integer, value As T)) Dim index As Integer = 0 For Each value As T In source Yield (index, value) index += 1 Next value End Function $vbLabelText $csharpLabel Now, you can use this extension method with your collections: int[] numbers = { 10, 20, 30, 40 }; foreach (var (index, value) in numbers.WithIndex()) { Console.WriteLine($"Index: {index}, Value: {value}"); } int[] numbers = { 10, 20, 30, 40 }; foreach (var (index, value) in numbers.WithIndex()) { Console.WriteLine($"Index: {index}, Value: {value}"); } Dim numbers() As Integer = { 10, 20, 30, 40 } foreach var(index, value) In numbers.WithIndex() Console.WriteLine($"Index: {index}, Value: {value}") Next $vbLabelText $csharpLabel This approach creates a more elegant solution to the foreach with index problem by abstracting away the manual index management into a reusable method. Using a while Loop to Access Indexes If you're working with collections like arrays or lists, you can use a while loop in conjunction with an index variable to access both the index and the current element: int[] numbers = { 10, 20, 30, 40 }; int index = 0; while (index < numbers.Length) { Console.WriteLine($"Index: {index}, Value: {numbers[index]}"); index++; } int[] numbers = { 10, 20, 30, 40 }; int index = 0; while (index < numbers.Length) { Console.WriteLine($"Index: {index}, Value: {numbers[index]}"); index++; } Dim numbers() As Integer = { 10, 20, 30, 40 } Dim index As Integer = 0 Do While index < numbers.Length Console.WriteLine($"Index: {index}, Value: {numbers(index)}") index += 1 Loop $vbLabelText $csharpLabel This method allows you to access both the index and the current element directly by using the index variable as a subscript for the array or list. Custom Collections and Iterators in .NET If you're working with customized collections, you can implement your iterators to support indexed access. By implementing the IEnumerable interface and using the yield return statement, you can create iterators that return both the element and its index. Here's an example of creating a custom collection that implements IEnumerable: public class CustomCollection<T> : IEnumerable<T> { private T[] _items; public CustomCollection(T[] items) { _items = items; } public IEnumerator<T> GetEnumerator() { for (int i = 0; i < _items.Length; i++) { yield return _items[i]; } } IEnumerator IEnumerable.GetEnumerator() { return GetEnumerator(); } } public class CustomCollection<T> : IEnumerable<T> { private T[] _items; public CustomCollection(T[] items) { _items = items; } public IEnumerator<T> GetEnumerator() { for (int i = 0; i < _items.Length; i++) { yield return _items[i]; } } IEnumerator IEnumerable.GetEnumerator() { return GetEnumerator(); } } Public Class CustomCollection(Of T) Implements IEnumerable(Of T) Private _items() As T Public Sub New(ByVal items() As T) _items = items End Sub Public Iterator Function GetEnumerator() As IEnumerator(Of T) Implements IEnumerable(Of T).GetEnumerator For i As Integer = 0 To _items.Length - 1 Yield _items(i) Next i End Function Private Iterator Function IEnumerable_GetEnumerator() As IEnumerator Implements IEnumerable.GetEnumerator Return GetEnumerator() End Function End Class $vbLabelText $csharpLabel You can then use this custom collection in a foreach loop: var customCollection = new CustomCollection<int>(new int[] { 10, 20, 30, 40 }); foreach (int number in customCollection) { Console.WriteLine(number); } var customCollection = new CustomCollection<int>(new int[] { 10, 20, 30, 40 }); foreach (int number in customCollection) { Console.WriteLine(number); } Dim customCollection As New CustomCollection(Of Integer)(New Integer() { 10, 20, 30, 40 }) For Each number As Integer In customCollection Console.WriteLine(number) Next number $vbLabelText $csharpLabel By implementing the GetEnumerator method and using yield return, you create an iterator that allows the foreach loop to work with your custom collection just like any other collection in .NET. Using Dictionaries and Iterating with Key-Value Pairs When working with dictionaries, the foreach loop allows you to iterate over key-value pairs directly. This is a common use case for accessing both the key and the value during each iteration: Dictionary<int, string> dict = new Dictionary<int, string> { { 1, "Apple" }, { 2, "Banana" }, { 3, "Cherry" } }; foreach (var kvp in dict) { Console.WriteLine($"Key: {kvp.Key}, Value: {kvp.Value}"); } Dictionary<int, string> dict = new Dictionary<int, string> { { 1, "Apple" }, { 2, "Banana" }, { 3, "Cherry" } }; foreach (var kvp in dict) { Console.WriteLine($"Key: {kvp.Key}, Value: {kvp.Value}"); } Dim dict As New Dictionary(Of Integer, String) From { {1, "Apple"}, {2, "Banana"}, {3, "Cherry"} } For Each kvp In dict Console.WriteLine($"Key: {kvp.Key}, Value: {kvp.Value}") Next kvp $vbLabelText $csharpLabel In this example, kvp.Key gives you the current key and kvp.Value gives you the current value. Using IronPDF with C# foreach Loop and Index IronPDF is a PDF library to handle PDF generation from HTML and other PDF-related tasks in C#. It is compatible with the latest .NET Framework as well. When generating PDFs using IronPDF, you might need to iterate over a collection of data and dynamically insert content into your PDF file. Combining the foreach loop with index handling allows you to manage positioning, numbering, or custom logic based on the index of the current item in the collection. Here's a practical example of using IronPDF to create a PDF where each item in a collection is inserted into the document, along with its index. using IronPdf; class Program { static void Main(string[] args) { // Create a new PDF document renderer var pdf = new ChromePdfRenderer(); // Sample data array string[] items = { "First Item", "Second Item", "Third Item" }; // Initialize the HTML content with foreach loop and index string htmlContent = "<html><body>"; int index = 0; foreach (var item in items) { htmlContent += $"<h2>Item {index + 1}: {item}</h2>"; index++; } htmlContent += "</body></html>"; // Render the HTML to PDF var pdfDocument = pdf.RenderHtmlAsPdf(htmlContent); // Save the PDF document pdfDocument.SaveAs("output.pdf"); // Notify completion Console.WriteLine("PDF created successfully with indexed items."); } } using IronPdf; class Program { static void Main(string[] args) { // Create a new PDF document renderer var pdf = new ChromePdfRenderer(); // Sample data array string[] items = { "First Item", "Second Item", "Third Item" }; // Initialize the HTML content with foreach loop and index string htmlContent = "<html><body>"; int index = 0; foreach (var item in items) { htmlContent += $"<h2>Item {index + 1}: {item}</h2>"; index++; } htmlContent += "</body></html>"; // Render the HTML to PDF var pdfDocument = pdf.RenderHtmlAsPdf(htmlContent); // Save the PDF document pdfDocument.SaveAs("output.pdf"); // Notify completion Console.WriteLine("PDF created successfully with indexed items."); } } Imports IronPdf Friend Class Program Shared Sub Main(ByVal args() As String) ' Create a new PDF document renderer Dim pdf = New ChromePdfRenderer() ' Sample data array Dim items() As String = { "First Item", "Second Item", "Third Item" } ' Initialize the HTML content with foreach loop and index Dim htmlContent As String = "<html><body>" Dim index As Integer = 0 For Each item In items htmlContent &= $"<h2>Item {index + 1}: {item}</h2>" index += 1 Next item htmlContent &= "</body></html>" ' Render the HTML to PDF Dim pdfDocument = pdf.RenderHtmlAsPdf(htmlContent) ' Save the PDF document pdfDocument.SaveAs("output.pdf") ' Notify completion Console.WriteLine("PDF created successfully with indexed items.") End Sub End Class $vbLabelText $csharpLabel Here is the output PDF file: 結論 In C#, while the foreach loop is a convenient way to iterate over collections, it lacks native support for indexing. However, there are several ways to overcome this limitation. Whether you use a simple index variable, the Select method from LINQ, or custom iterators, you can gain access to the index of the current or next element during iteration. Understanding these techniques can help you make more efficient use of the foreach loop, especially when you need to know the index of each element. With IronPDF, you don't have to commit right away. We offer a free trial that lets you explore the software’s capabilities in depth. If you like what you see, licenses start at $799. よくある質問 C# の foreach ループで要素のインデックスを追跡するにはどうすればよいですか? C# の foreach ループでインデックスを追跡するには、別のインデックス変数を手動でインクリメントするか、LINQ の Select メソッドを使用して要素をそのインデックスと共に射影するか、要素とそのインデックスの両方を生成するカスタム イテレーターを作成することができます。 LINQ Select メソッドとは何で、インデックス作成にどのように役立ちますか? LINQ の Select メソッドは、コレクション内の各要素をインデックスを含む新しい形式に変換できます。この射影により、foreach ループ中に要素とそのインデックスの両方にアクセスできます。 C# でのインデックス作成用のカスタム イテレーターを作成するにはどうすればよいですか? C# でのカスタム イテレーターは、yield return キーワードを使用して作成できます。これにより、コレクションを反復処理し、現在の要素とそのインデックスの両方を生成するメソッドを構築できます。これにより、ループのインデックス作成が簡略化されます。 C# で索引付きコンテンツを作成する際に PDF ライブラリのサポートを受けることはできますか? はい、IronPDF のような PDF ライブラリは C# の foreach ループと共に使用してデータ コレクションを反復処理し、PDF に索引付きコンテンツを挿入するのに使用できます。この方法により、動的なコンテンツ配置と正確なインデックス作成が可能になります。 C# では、foreach ループを使用して辞書をどのように反復処理しますか? C# では、foreach ループは各キーと値のペアにアクセスして辞書を反復処理できます。これにより、開発者は反復処理中にキーと値の両方を直接操作できます。 C# 開発における PDF ライブラリの使用の利点は何ですか? PDF ライブラリを使用すると、開発者は HTML から PDF を生成し、さまざまな PDF 操作を C# で実行することができます。通常、無料トライアルによって機能を探索し、ライセンスを購入することができます。 C# での索引付け繰り返しには while ループをどのように使用できますか? while ループは、インデックス変数と共に使用して C# のコレクションを反復処理することができ、インデックスを下付き文字として利用しながら両方のインデックスと現在の要素にアクセスします。 Curtis Chau 今すぐエンジニアリングチームとチャット テクニカルライター Curtis Chauは、カールトン大学でコンピュータサイエンスの学士号を取得し、Node.js、TypeScript、JavaScript、およびReactに精通したフロントエンド開発を専門としています。直感的で美しいユーザーインターフェースを作成することに情熱を持ち、Curtisは現代のフレームワークを用いた開発や、構造の良い視覚的に魅力的なマニュアルの作成を楽しんでいます。開発以外にも、CurtisはIoT(Internet of Things)への強い関心を持ち、ハードウェアとソフトウェアの統合方法を模索しています。余暇には、ゲームをしたりDiscordボットを作成したりして、技術に対する愛情と創造性を組み合わせています。 関連する記事 更新日 9月 4, 2025 RandomNumberGenerator C# RandomNumberGenerator C#クラスを使用すると、PDF生成および編集プロジェクトを次のレベルに引き上げることができます 詳しく読む 更新日 9月 4, 2025 C# String Equals(開発者向けの仕組み) 強力なPDFライブラリであるIronPDFと組み合わせることで、switchパターンマッチングは、ドキュメント処理のためのよりスマートでクリーンなロジックを構築できます 詳しく読む 更新日 8月 5, 2025 C# Switch Pattern Matching(開発者向けの仕組み) 強力なPDFライブラリであるIronPDFと組み合わせることで、switchパターンマッチングは、ドキュメント処理のためのよりスマートでクリーンなロジックを構築できます 詳しく読む C#のインデクサー(開発者向けの仕組み)Socket io .NET(開発者向けの...
更新日 9月 4, 2025 RandomNumberGenerator C# RandomNumberGenerator C#クラスを使用すると、PDF生成および編集プロジェクトを次のレベルに引き上げることができます 詳しく読む
更新日 9月 4, 2025 C# String Equals(開発者向けの仕組み) 強力なPDFライブラリであるIronPDFと組み合わせることで、switchパターンマッチングは、ドキュメント処理のためのよりスマートでクリーンなロジックを構築できます 詳しく読む
更新日 8月 5, 2025 C# Switch Pattern Matching(開発者向けの仕組み) 強力なPDFライブラリであるIronPDFと組み合わせることで、switchパターンマッチングは、ドキュメント処理のためのよりスマートでクリーンなロジックを構築できます 詳しく読む