.NETヘルプ C#印刷リスト: クイックチュートリアル Jacob Mellor 更新日:7月 28, 2025 IronPrint をダウンロード NuGet ダウンロード 無料トライアル LLM向けのコピー LLM向けのコピー LLM 用の Markdown としてページをコピーする ChatGPTで開く このページについてChatGPTに質問する ジェミニで開く このページについてGeminiに問い合わせる ジェミニで開く このページについてGeminiに問い合わせる 困惑の中で開く このページについてPerplexityに問い合わせる 共有する Facebook で共有 Xでシェア(Twitter) LinkedIn で共有 URLをコピー 記事をメールで送る C# では、リストの印刷は一般的なタスクであり、さまざまな方法で実行できるため、柔軟性とカスタマイズ性が提供されます。 リストは C# の基本的なデータ構造であり、その内容を印刷できることは、デバッグ、ログ記録、またはユーザーへの情報の表示に不可欠です。 この記事では、 C# でリストを印刷するさまざまな方法について説明します。 C# におけるリストの基本 C#では、listはサイズを拡大または縮小できる動的配列です。これはSystem.Collections.Generic名前空間の一部で、アイテムのコレクションを扱う際の柔軟性と効率を提供します。 次のコードは単純な数値リストを作成します。 using System; using System.Collections.Generic; class Program { static void Main() { List<int> numbers = new List<int> { 1, 2, 3, 4, 5 }; } } using System; using System.Collections.Generic; class Program { static void Main() { List<int> numbers = new List<int> { 1, 2, 3, 4, 5 }; } } IRON VB CONVERTER ERROR developers@ironsoftware.com $vbLabelText $csharpLabel ループを使用してリストを印刷する 1. foreachループの使用 リストの要素を出力する伝統的かつ簡単な方法は、 foreachループを使うことです。簡単な例を以下に示します。 using System; using System.Collections.Generic; class Program { public static void Main() { List<int> numbers = new List<int> { 1, 2, 3, 4, 5 }; Console.WriteLine("Printing list using foreach loop:"); foreach (var number in numbers) { Console.WriteLine(number); } } } using System; using System.Collections.Generic; class Program { public static void Main() { List<int> numbers = new List<int> { 1, 2, 3, 4, 5 }; Console.WriteLine("Printing list using foreach loop:"); foreach (var number in numbers) { Console.WriteLine(number); } } } Imports System Imports System.Collections.Generic Friend Class Program Public Shared Sub Main() Dim numbers As New List(Of Integer) From {1, 2, 3, 4, 5} Console.WriteLine("Printing list using foreach loop:") For Each number In numbers Console.WriteLine(number) Next number End Sub End Class $vbLabelText $csharpLabel この方法は簡潔で読みやすいため、ほとんどのシナリオに適しています。 2. forループの使用 インデックスをより細かく制御する必要がある場合や、条件に応じて要素を出力したい場合は、 forループを使用できます。以下は文字列リストの例です。 using System; using System.Collections.Generic; class Program { public static void Main() { // Create list of strongly typed objects List<string> colors = new List<string> { "Red", "Green", "Blue", "Yellow" }; Console.WriteLine("Printing list using for loop:"); for (int index = 0; index < colors.Count; index++) { Console.WriteLine($"Color at index {index}: {colors[index]}"); } } } using System; using System.Collections.Generic; class Program { public static void Main() { // Create list of strongly typed objects List<string> colors = new List<string> { "Red", "Green", "Blue", "Yellow" }; Console.WriteLine("Printing list using for loop:"); for (int index = 0; index < colors.Count; index++) { Console.WriteLine($"Color at index {index}: {colors[index]}"); } } } Imports System Imports System.Collections.Generic Friend Class Program Public Shared Sub Main() ' Create list of strongly typed objects Dim colors As New List(Of String) From {"Red", "Green", "Blue", "Yellow"} Console.WriteLine("Printing list using for loop:") For index As Integer = 0 To colors.Count - 1 Console.WriteLine($"Color at index {index}: {colors(index)}") Next index End Sub End Class $vbLabelText $csharpLabel このアプローチは、インデックスにアクセスする必要がある場合や、印刷中に特定のロジックを適用する場合に役立ちます。 リスト要素を逆順に印刷する リストを逆の順序で印刷するには、 Reverseメソッドを使用します。 このメソッドは、リスト内の要素の順序を逆にして、反復処理して印刷できるようにします。 1. List.Reverseメソッドの使用 たとえば、 Reverseメソッドを使用して各要素を印刷することで、リストを逆の順序で印刷できます。 using System; using System.Collections.Generic; class Program { public static void Main() { List<int> numbers = new List<int> { 1, 2, 3, 4, 5 }; numbers.Reverse(); foreach (var number in numbers) { Console.WriteLine(number); } } } using System; using System.Collections.Generic; class Program { public static void Main() { List<int> numbers = new List<int> { 1, 2, 3, 4, 5 }; numbers.Reverse(); foreach (var number in numbers) { Console.WriteLine(number); } } } Imports System Imports System.Collections.Generic Friend Class Program Public Shared Sub Main() Dim numbers As New List(Of Integer) From {1, 2, 3, 4, 5} numbers.Reverse() For Each number In numbers Console.WriteLine(number) Next number End Sub End Class $vbLabelText $csharpLabel 2. LINQ OrderByDescending の使用 また、キーを指定したOrderByDescendingメソッドを使用して、LINQ のジェネリック クラスから指定した要素のコレクションを並べ替えることもできます。 using System; using System.Collections.Generic; using System.Linq; class Program { public static void Main() { List<int> numbers = new List<int> { 1, 2, 3, 4, 5 }; var reversedNumbers = numbers.OrderByDescending(n => n); foreach (var number in reversedNumbers) { Console.WriteLine(number); } } } using System; using System.Collections.Generic; using System.Linq; class Program { public static void Main() { List<int> numbers = new List<int> { 1, 2, 3, 4, 5 }; var reversedNumbers = numbers.OrderByDescending(n => n); foreach (var number in reversedNumbers) { Console.WriteLine(number); } } } Imports System Imports System.Collections.Generic Imports System.Linq Friend Class Program Public Shared Sub Main() Dim numbers As New List(Of Integer) From {1, 2, 3, 4, 5} Dim reversedNumbers = numbers.OrderByDescending(Function(n) n) For Each number In reversedNumbers Console.WriteLine(number) Next number End Sub End Class $vbLabelText $csharpLabel 要素の数を数えて出力する リスト内の要素の数を数えることは一般的な操作です。 この目的にはCountプロパティを使用できます。 カウントがわかれば、簡単に印刷できます。 1. List.Countプロパティの使用 using System; using System.Collections.Generic; class Program { public static void Main() { List<string> names = new List<string> { "Alice", "Bob", "Charlie" }; int count = names.Count; Console.WriteLine($"Number of elements: {count}"); } } using System; using System.Collections.Generic; class Program { public static void Main() { List<string> names = new List<string> { "Alice", "Bob", "Charlie" }; int count = names.Count; Console.WriteLine($"Number of elements: {count}"); } } Imports System Imports System.Collections.Generic Friend Class Program Public Shared Sub Main() Dim names As New List(Of String) From {"Alice", "Bob", "Charlie"} Dim count As Integer = names.Count Console.WriteLine($"Number of elements: {count}") End Sub End Class $vbLabelText $csharpLabel 2. LINQ Countメソッドの使用 LINQ が優先される場合は、カウントにも使用できます。 using System; using System.Collections.Generic; using System.Linq; class Program { public static void Main() { List<string> names = new List<string> { "Alice", "Bob", "Charlie" }; int count = names.Count(); Console.WriteLine($"Number of elements: {count}"); } } using System; using System.Collections.Generic; using System.Linq; class Program { public static void Main() { List<string> names = new List<string> { "Alice", "Bob", "Charlie" }; int count = names.Count(); Console.WriteLine($"Number of elements: {count}"); } } Imports System Imports System.Collections.Generic Imports System.Linq Friend Class Program Public Shared Sub Main() Dim names As New List(Of String) From {"Alice", "Bob", "Charlie"} Dim count As Integer = names.Count() Console.WriteLine($"Number of elements: {count}") End Sub End Class $vbLabelText $csharpLabel 指定したインデックスのリスト要素を印刷する 指定されたインデックスの要素を印刷するには、そのインデックスのリストにアクセスする必要があります。 これにより、さまざまな場所にある要素にアクセスできるようになり、潜在的なインデックス範囲外のシナリオを処理できるようになります。 using System; using System.Collections.Generic; class Program { public static void Main() { List<double> prices = new List<double> { 19.99, 29.99, 39.99 }; int indexToPrint = 1; if (indexToPrint >= 0 && indexToPrint < prices.Count) { Console.WriteLine($"Element at index {indexToPrint}: {prices[indexToPrint]}"); } else { Console.WriteLine("Index out of range."); } } } using System; using System.Collections.Generic; class Program { public static void Main() { List<double> prices = new List<double> { 19.99, 29.99, 39.99 }; int indexToPrint = 1; if (indexToPrint >= 0 && indexToPrint < prices.Count) { Console.WriteLine($"Element at index {indexToPrint}: {prices[indexToPrint]}"); } else { Console.WriteLine("Index out of range."); } } } Imports System Imports System.Collections.Generic Friend Class Program Public Shared Sub Main() Dim prices As New List(Of Double) From {19.99, 29.99, 39.99} Dim indexToPrint As Integer = 1 If indexToPrint >= 0 AndAlso indexToPrint < prices.Count Then Console.WriteLine($"Element at index {indexToPrint}: {prices(indexToPrint)}") Else Console.WriteLine("Index out of range.") End If End Sub End Class $vbLabelText $csharpLabel これらの例は、さまざまなシナリオでリスト要素を印刷するための基礎を提供します。 listクラスには、印刷に使用できる他の便利なメソッドも用意されています。 いくつかの便利なメソッドは次のとおりです: Remove: Remove()メソッドは、C# リスト内の最初の出現を削除します。 RemoveAll: RemoveAll()メソッドは、指定された述語に基づいて要素を削除するために使用されます。 RemoveRange: RemoveRange()メソッドは、指定されたインデックスとカウント パラメータに基づいて要素の範囲を削除します。 Add: Add()メソッドは、リストの末尾に 1 つの要素のみを追加できます。 AddRange: AddRange()メソッドは、指定されたコレクションの要素を末尾に追加できます。 Clear: Clear()メソッドはリストからすべての要素を削除します。 Insert: Insert()メソッドは、指定されたインデックスでリストに要素を挿入します。 ForEach: ForEach()メソッドは、各要素に対して特定のアクションを実行するために使用されます。たとえば、各リストの値を効率的に印刷します。 ToArray: ToArray()メソッドはリストを新しい配列にコピーします。 これで、要件に最適なアプローチを選択し、C# コードの読みやすさと効率性を向上させることができます。 String.Join メソッド String.Joinメソッドは、指定された区切り文字を使用してリストの要素を 1 つの文字列に連結する簡潔な方法を提供します。 これは、リストのフォーマットされた文字列表現を作成するのに役立ちます。 using System; using System.Collections.Generic; class Program { public static void Main() { List<int> numbers = new List<int> { 1, 2, 3, 4, 5 }; string result = string.Join(", ", numbers); Console.WriteLine(result); } } using System; using System.Collections.Generic; class Program { public static void Main() { List<int> numbers = new List<int> { 1, 2, 3, 4, 5 }; string result = string.Join(", ", numbers); Console.WriteLine(result); } } Imports System Imports System.Collections.Generic Friend Class Program Public Shared Sub Main() Dim numbers As New List(Of Integer) From {1, 2, 3, 4, 5} Dim result As String = String.Join(", ", numbers) Console.WriteLine(result) End Sub End Class $vbLabelText $csharpLabel ここでは、リストnumbersの要素がコンマとスペースで区切られた文字列に結合され、フォーマットされた出力が生成されます。 リスト要素を印刷する前に sort メソッドを使用することもできます。 高度なシナリオでの LINQ の使用 印刷前に要素をフィルター処理、変換、または書式設定する必要があるより複雑なシナリオでは、統合言語クエリ ( LINQ ) が役立ちます。 using System; using System.Collections.Generic; using System.Linq; class Program { public static void Main() { List<int> numbers = new List<int> { 1, 2, 3, 4, 5 }; List<int> evenNumbers = numbers.Where(n => n % 2 == 0).ToList(); Console.WriteLine("Even numbers: " + string.Join(", ", evenNumbers)); } } using System; using System.Collections.Generic; using System.Linq; class Program { public static void Main() { List<int> numbers = new List<int> { 1, 2, 3, 4, 5 }; List<int> evenNumbers = numbers.Where(n => n % 2 == 0).ToList(); Console.WriteLine("Even numbers: " + string.Join(", ", evenNumbers)); } } Imports System Imports System.Collections.Generic Imports System.Linq Friend Class Program Public Shared Sub Main() Dim numbers As New List(Of Integer) From {1, 2, 3, 4, 5} Dim evenNumbers As List(Of Integer) = numbers.Where(Function(n) n Mod 2 = 0).ToList() Console.WriteLine("Even numbers: " & String.Join(", ", evenNumbers)) End Sub End Class $vbLabelText $csharpLabel この例では、LINQを使用して元のリストから偶数を除外しています。Where Where()メソッドをラムダ式で適用し、 ToList()メソッドを使用して結果をリストに戻しています。 ! C# リストの印刷 (開発者向け) 図 1 - コンソール出力: 偶数: 2、4 カスタムオブジェクトとToString()メソッド カスタム オブジェクトのリストがある場合は、意味のある表現のためにオブジェクト クラスのToStringメソッドをオーバーライドすることをお勧めします。 次の例はその方法を示しています。 using System; using System.Collections.Generic; class Person { public string Name { get; set; } public int Age { get; set; } public override string ToString() { return $"{Name}, {Age} years old"; } } class Program { public static void Main() { List<Person> people = new List<Person> { new Person { Name = "Alice", Age = 30 }, new Person { Name = "Bob", Age = 25 } }; foreach (Person person in people) { Console.WriteLine(person); } } } using System; using System.Collections.Generic; class Person { public string Name { get; set; } public int Age { get; set; } public override string ToString() { return $"{Name}, {Age} years old"; } } class Program { public static void Main() { List<Person> people = new List<Person> { new Person { Name = "Alice", Age = 30 }, new Person { Name = "Bob", Age = 25 } }; foreach (Person person in people) { Console.WriteLine(person); } } } Imports System Imports System.Collections.Generic Friend Class Person Public Property Name() As String Public Property Age() As Integer Public Overrides Function ToString() As String Return $"{Name}, {Age} years old" End Function End Class Friend Class Program Public Shared Sub Main() Dim people As New List(Of Person) From { New Person With { .Name = "Alice", .Age = 30 }, New Person With { .Name = "Bob", .Age = 25 } } For Each person As Person In people Console.WriteLine(person) Next person End Sub End Class $vbLabelText $csharpLabel PersonクラスのToString()メソッドをオーバーライドすることで、クラスのインスタンスが文字列としてどのように表現されるかを制御します。 これにより、印刷したときにリストが読みやすくなります。 ! C# リストの印刷 (開発者向け) 図 2 - コンソール出力 IronPrint の紹介 - C# 印刷ライブラリ IronPrint は、精度、使いやすさ、速度を重視した、強力で展開可能な印刷ライブラリとして際立っています。 クロスプラットフォームのサポートとさまざまなドキュメント形式との互換性により、アプリケーションで効率的な印刷ソリューションを求める .NET 開発者にとって貴重なツールとなります。 ! C# でリストを印刷する (開発者向け) : 図 3 - IronPrint for .NET: C# 印刷ライブラリ 主要機能 C# アプリケーションで物理ドキュメントを印刷する場合に IronPrint が優れている主な機能は次のとおりです。 1. クロスプラットフォームの互換性 .NET バージョンのサポート: .NET 8、7、6、5、および Core 3.1+ オペレーティング システム: Windows (7+、Server 2016+)、macOS (10+)、iOS (11+)、Android API 21+ (v5 "Lollipop") プロジェクトの種類: モバイル (Xamarin、MAUI、Avalonia)、デスクトップ (WPF、MAUI、Windows Avalonia)、コンソール (アプリとライブラリ) 2. サポートされている形式 PDF、PNG、HTML、TIFF、GIF、JPEG、IMAGE、BITMAP など、さまざまなドキュメント形式を処理します。 ドキュメントの要件に応じて印刷設定をカスタマイズします。 3. 簡単なインストール NuGet パッケージ マネージャー コンソールを使用してIronPrintライブラリをインストールします。 Install-Package IronPrint あるいは、Visual Studio を使用してプロジェクトにインストールすることもできます。 ソリューション エクスプローラーでプロジェクトを右クリックし、[ソリューションの NuGet パッケージ マネージャーの管理] をクリックします。 NuGet の参照タブで"ironprint"を検索し、検索結果から IronPrint パッケージの最新バージョンを選択して、[インストール] ボタンをクリックしてプロジェクトに追加します。 IronPrint による印刷: コード例 1. 文書を印刷する IronPrint は、シンプルなPrint方法を使用してドキュメントを静かに印刷します。 物理プリンタが使用できない場合は、OS で指定されたデフォルトのプリンタを使用して印刷します。 using IronPrint; class Program { static void Main() { // Print the document Printer.Print("newDoc.pdf"); } } using IronPrint; class Program { static void Main() { // Print the document Printer.Print("newDoc.pdf"); } } Imports IronPrint Friend Class Program Shared Sub Main() ' Print the document Printer.Print("newDoc.pdf") End Sub End Class $vbLabelText $csharpLabel 2. ダイアログ付きで印刷 また、印刷中の制御を向上させるために、 printダイアログを表示する専用の方法も提供します。 ShowPrintDialogAsyncメソッドを使用して非同期印刷することもできます。 using IronPrint; class Program { static void Main() { // Show print dialog Printer.ShowPrintDialog("newDoc.pdf"); } } using IronPrint; class Program { static void Main() { // Show print dialog Printer.ShowPrintDialog("newDoc.pdf"); } } Imports IronPrint Friend Class Program Shared Sub Main() ' Show print dialog Printer.ShowPrintDialog("newDoc.pdf") End Sub End Class $vbLabelText $csharpLabel 3. 印刷設定をカスタマイズする IronPrint は、ドキュメントの印刷を細かく制御できるさまざまなprint設定を提供します。 using IronPrint; class Program { static void Main() { // Configure print settings PrintSettings printSettings = new PrintSettings() { Dpi = 150, NumberOfCopies = 2, PaperOrientation = PaperOrientation.Portrait }; // Print the document Printer.Print("newDoc.pdf", printSettings); } } using IronPrint; class Program { static void Main() { // Configure print settings PrintSettings printSettings = new PrintSettings() { Dpi = 150, NumberOfCopies = 2, PaperOrientation = PaperOrientation.Portrait }; // Print the document Printer.Print("newDoc.pdf", printSettings); } } Imports IronPrint Friend Class Program Shared Sub Main() ' Configure print settings Dim printSettings As New PrintSettings() With { .Dpi = 150, .NumberOfCopies = 2, .PaperOrientation = PaperOrientation.Portrait } ' Print the document Printer.Print("newDoc.pdf", printSettings) End Sub End Class $vbLabelText $csharpLabel 使用されるクラスとメソッドをより深く理解するには、 API リファレンスページをご覧ください。 結論 C# でリストを印刷するには、データの複雑さと目的の出力に基づいて適切な方法を選択する必要があります。 単純なループ、 String.Join() 、LINQ を使用する場合でも、カスタム オブジェクトのToString()メソッドをカスタマイズする場合でも、これらのアプローチを理解することで、C# アプリケーションでリストの内容を効果的に表示できるようになります。 これらのテクニックを試してみて、特定のユースケースに最適なものを選択してください。 正確性、使いやすさ、スピードを求めるなら、 IronPrint が最適な印刷ライブラリです。 Web アプリを構築する場合でも、MAUI、Avalonia を操作する場合でも、.NET 関連のあらゆる作業を行う場合でも、IronPrint が役立ちます。 IronPrint の詳細については、このドキュメント ページをご覧ください。 IronPrint は有料ライブラリですが、無料トライアルもご利用いただけます。 ここからライブラリをダウンロードして試してみてください。 よくある質問 C# でリストを印刷するにはどうすればいいですか? C# でリストを印刷するために IronPrint を使用することができます。 foreach または for ループを使用してリスト要素を反復処理し、フォーマットされた出力を IronPrint に渡して印刷します。 C# で動的配列を使用する利点は何ですか? C# の動的配列、またはリストは、サイズが増減できるため柔軟性を提供します。それらは System.Collections.Generic 名前空間の一部であり、コレクションの効率的な処理と操作を可能にします。 C# でリストを逆順に印刷するにはどうすればいいですか? リストを逆順に印刷するには、Reverse メソッドまたは LINQ の OrderByDescending を使用し、逆順にしたリストを IronPrint で印刷することができます。 印刷用にリストを文字列としてフォーマットするにはどうすればいいですか? String.Join メソッドを使用して、リスト要素を指定されたセパレータで連結し、単一のフォーマットされた文字列にすることができます。このフォーマットされた文字列を IronPrint を使用して印刷できます。 カスタムオブジェクトの印刷における ToString メソッドの役割は何ですか? ToString メソッドをオーバーライドすることで、カスタムオブジェクトのインスタンスが文字列としてどのように表現されるかを定義でき、IronPrint で印刷されたときの読みやすさが向上します。 C# で特定の要素をフィルタリングして印刷するにはどうすればいいですか? LINQ メソッドの Where を使用して、リストから特定の要素をフィルタリングして選択できます。フィルタリングされた結果を IronPrint を使用して印刷できます。 .NET 向けの印刷ライブラリをインストールするにはどうすればいいですか? NuGet Package Manager Console を使用してコマンド Install-Package IronPrint を入力するか、Visual Studio の NuGet Package Manager を通じて IronPrint をインストールできます。 .NET 開発者向けに IronPrint が提供する機能は何ですか? IronPrint はクロスプラットフォーム互換性、さまざまなドキュメント形式のサポート、サイレント印刷、印刷ダイアログ、およびカスタマイズ可能な印刷設定を提供し、.NET 開発者にとって強力な選択肢です。 Jacob Mellor 今すぐエンジニアリングチームとチャット 最高技術責任者(CTO) Jacob Mellorは、Iron Softwareの最高技術責任者であり、C# PDF技術の開拓者としてその先進的な役割を担っています。Iron Softwareのコアコードベースのオリジナルデベロッパーである彼は、創業時から製品のアーキテクチャを形作り、CEOのCameron Rimingtonと協力してNASA、Tesla、全世界の政府機関を含む50人以上の会社に成長させました。Jacobは、1998年から2001年にかけてマンチェスター大学で土木工学の第一級優等学士号(BEng)を取得しました。1999年にロンドンで最初のソフトウェアビジネスを立ち上げ、2005年には最初の.NETコンポーネントを作成し、Microsoftエコシステムにおける複雑な問題の解決を専門にしました。彼の旗艦製品であるIronPDFとIronSuite .NETライブラリは、全世界で3000万以上のNuGetインストールを達成しており、彼の基本コードが世界中で使用されている開発者ツールを支えています。商業的な経験を25年間積み、コードを書くことを41年間続けるJacobは、企業向けのC#、Java、およびPython PDF技術の革新を推進し続け、次世代の技術リーダーを指導しています。 関連する記事 更新日 7月 28, 2025 C# 印刷ラインを効果的に使用する方法 この記事では、C#でのライン印刷に関連するさまざまなメソッドと技術を探ります。 詳しく読む 更新日 6月 22, 2025 C#の印刷変数: コードを簡素化する この包括的な記事では、異なるデータ型、フォーマットオプション、および高度な技術をカバーし、C#での変数印刷のさまざまな側面を探ります。 詳しく読む 更新日 7月 28, 2025 C#印刷関数マスター:開発者ガイド C#印刷の核心にあるのはConsole.WriteLineメソッドです。フォーマット済みの出力情報をコンソールに表示するための方法です 詳しく読む C#印刷関数マスター:開発者ガイドC# 印刷コンソール: ステッ...
更新日 6月 22, 2025 C#の印刷変数: コードを簡素化する この包括的な記事では、異なるデータ型、フォーマットオプション、および高度な技術をカバーし、C#での変数印刷のさまざまな側面を探ります。 詳しく読む
更新日 7月 28, 2025 C#印刷関数マスター:開発者ガイド C#印刷の核心にあるのはConsole.WriteLineメソッドです。フォーマット済みの出力情報をコンソールに表示するための方法です 詳しく読む