C# 尋找(開發者使用方法)
歡迎來到 C# 方便的 Find 函式教學。 您剛剛發現了一個可以簡化編碼流程的強大功能。 因此,無論您是經驗豐富的程式設計師,還是剛開始工作,本教學都會帶您了解所有的要素,讓您輕鬆上手。
尋找的基礎知識
Find 的核心是一個函式,可讓您找出集合、陣列或列表中滿足指定謂語的第一個元素。 您會問什麼是謂詞? 在程式設計中,謂語是一種函數,用來測試針對集合中元素所定義的特定條件。
現在,讓我們深入一個公共類別的範例。
public class BikePart
{
public string Id { get; set; } // Property to identify the bike part
// Override the Equals method to specify how to compare two BikePart objects
public override bool Equals(object obj)
{
if (obj == null || !(obj is BikePart))
return false;
return this.Id == ((BikePart)obj).Id;
}
// Override GetHashCode for hashing BikePart objects
public override int GetHashCode()
{
return this.Id.GetHashCode();
}
// Override ToString to return a custom string representation of the object
public override string ToString()
{
return "BikePart ID: " + this.Id;
}
}public class BikePart
{
public string Id { get; set; } // Property to identify the bike part
// Override the Equals method to specify how to compare two BikePart objects
public override bool Equals(object obj)
{
if (obj == null || !(obj is BikePart))
return false;
return this.Id == ((BikePart)obj).Id;
}
// Override GetHashCode for hashing BikePart objects
public override int GetHashCode()
{
return this.Id.GetHashCode();
}
// Override ToString to return a custom string representation of the object
public override string ToString()
{
return "BikePart ID: " + this.Id;
}
}在這段程式碼中,BikePart 是我們的公共類別,它包含一個公共字串 ID 來識別每個自行車零件。 我們覆寫了 ToString 方法,以便很好地列印自行車零件的 ID,我們也覆寫了 Equals 和 GetHashCode 方法,以便進行比較。
使用謂語查找。
現在我們有了 BikePart 類,我們可以建立一個自行車零件清單,並使用 Find 根據 ID 來定位特定零件。 讓我們考慮以下範例:
using System;
using System.Collections.Generic;
public static void Main()
{
// Create a list of BikePart objects
List<BikePart> bikeParts = new List<BikePart>
{
new BikePart { Id = "Chain Ring ID" },
new BikePart { Id = "Crank Arm ID" },
new BikePart { Id = "Regular Seat ID" },
new BikePart { Id = "Banana Seat ID" },
};
// Define a predicate to find a BikePart with a specific ID
Predicate<BikePart> findChainRingPredicate = (BikePart bp) => { return bp.Id == "Chain Ring ID"; };
BikePart chainRingPart = bikeParts.Find(findChainRingPredicate);
// Print the found BikePart's ID to the console
Console.WriteLine(chainRingPart.ToString());
}using System;
using System.Collections.Generic;
public static void Main()
{
// Create a list of BikePart objects
List<BikePart> bikeParts = new List<BikePart>
{
new BikePart { Id = "Chain Ring ID" },
new BikePart { Id = "Crank Arm ID" },
new BikePart { Id = "Regular Seat ID" },
new BikePart { Id = "Banana Seat ID" },
};
// Define a predicate to find a BikePart with a specific ID
Predicate<BikePart> findChainRingPredicate = (BikePart bp) => { return bp.Id == "Chain Ring ID"; };
BikePart chainRingPart = bikeParts.Find(findChainRingPredicate);
// Print the found BikePart's ID to the console
Console.WriteLine(chainRingPart.ToString());
}在此程式碼中,我們實體化四個具有唯一 ID 的 BikePart 物件。 接下來,我們建立一個謂語 findChainRingPredicate 來檢查自行車零件是否具有 ID"Chain Ring ID"。 最後,我們使用定義的謂語在自行車零件清單上呼叫 Find 並將找到的零件 ID 列印到控制台。
瞭解謂語參數
您可能會對我們的 Find 方法中的 Predicate match 參數感到疑惑。 這是您定義 Find 方法返回元素的條件。 在我們的案例中,我們希望 Find 方法能傳回符合"Chain Ring ID"的第一個元素。
如果沒有元素滿足您在謂語中定義的條件,Find 方法將返回預設值。 舉例來說,如果您正在處理一個整數陣列,而您的謂詞沒有找到符合的值,Find 方法將會回傳 '0',也就是 C# 中整數的預設值。
線性搜尋原理
必須注意的是,Find 函式會在整個陣列、列表或集合中進行線性搜尋。 這表示它會從第一個元素開始,依序檢查接下來的每個元素,直到找到滿足謂語的元素首次出現為止。
在某些情況下,您可能想要找出滿足謂語的最後一個元素,而不是第一個元素。 為此,C# 提供了 FindLast 函式。
FindIndex 和 FindLastIndex
正如 Find 可幫助您找出符合您指定謂語的元素的第一次出現,C# 也提供 FindIndex 和 FindLastIndex 方法,可分別提供符合您條件的第一個和最後一個元素的索引。
讓我們試舉一個例子:
using System;
using System.Collections.Generic;
public static void Main()
{
// Create a list of BikePart objects with an additional duplicate entry
List<BikePart> bikeParts = new List<BikePart>
{
new BikePart { Id = "Chain Ring ID" },
new BikePart { Id = "Crank Arm ID" },
new BikePart { Id = "Regular Seat ID" },
new BikePart { Id = "Banana Seat ID" },
new BikePart { Id = "Chain Ring ID" }, // Added a second chain ring
};
// Define a predicate to find a BikePart with a specific ID
Predicate<BikePart> findChainRingPredicate = (BikePart bp) => { return bp.Id == "Chain Ring ID"; };
// Find the index of the first and last occurrence of the specified BikePart
int firstChainRingIndex = bikeParts.FindIndex(findChainRingPredicate);
int lastChainRingIndex = bikeParts.FindLastIndex(findChainRingPredicate);
// Print the indices to the console
Console.WriteLine($"First Chain Ring ID found at index: {firstChainRingIndex}");
Console.WriteLine($"Last Chain Ring ID found at index: {lastChainRingIndex}");
}using System;
using System.Collections.Generic;
public static void Main()
{
// Create a list of BikePart objects with an additional duplicate entry
List<BikePart> bikeParts = new List<BikePart>
{
new BikePart { Id = "Chain Ring ID" },
new BikePart { Id = "Crank Arm ID" },
new BikePart { Id = "Regular Seat ID" },
new BikePart { Id = "Banana Seat ID" },
new BikePart { Id = "Chain Ring ID" }, // Added a second chain ring
};
// Define a predicate to find a BikePart with a specific ID
Predicate<BikePart> findChainRingPredicate = (BikePart bp) => { return bp.Id == "Chain Ring ID"; };
// Find the index of the first and last occurrence of the specified BikePart
int firstChainRingIndex = bikeParts.FindIndex(findChainRingPredicate);
int lastChainRingIndex = bikeParts.FindLastIndex(findChainRingPredicate);
// Print the indices to the console
Console.WriteLine($"First Chain Ring ID found at index: {firstChainRingIndex}");
Console.WriteLine($"Last Chain Ring ID found at index: {lastChainRingIndex}");
}FindAll的力量
FindAll 方法,顧名思義,擷取集合中所有滿足謂語的元素。 當您需要根據特定條件篩選元素時,就會用到它。 FindAll 方法會返回一個包含所有匹配元素的新 List。
以下是一個程式碼範例:
using System;
using System.Collections.Generic;
public static void Main()
{
// Create a list of BikePart objects with an additional duplicate entry
List<BikePart> bikeParts = new List<BikePart>
{
new BikePart { Id = "Chain Ring ID" },
new BikePart { Id = "Crank Arm ID" },
new BikePart { Id = "Regular Seat ID" },
new BikePart { Id = "Banana Seat ID" },
new BikePart { Id = "Chain Ring ID" }, // Added a second chain ring
};
// Define a predicate to find all BikeParts with a specific ID
Predicate<BikePart> findChainRingPredicate = (BikePart bp) => { return bp.Id == "Chain Ring ID"; };
// Use FindAll to get all matching BikePart objects
List<BikePart> chainRings = bikeParts.FindAll(findChainRingPredicate);
// Print the count and details of each found BikePart
Console.WriteLine($"Found {chainRings.Count} Chain Rings:");
foreach (BikePart chainRing in chainRings)
{
Console.WriteLine(chainRing.ToString());
}
}using System;
using System.Collections.Generic;
public static void Main()
{
// Create a list of BikePart objects with an additional duplicate entry
List<BikePart> bikeParts = new List<BikePart>
{
new BikePart { Id = "Chain Ring ID" },
new BikePart { Id = "Crank Arm ID" },
new BikePart { Id = "Regular Seat ID" },
new BikePart { Id = "Banana Seat ID" },
new BikePart { Id = "Chain Ring ID" }, // Added a second chain ring
};
// Define a predicate to find all BikeParts with a specific ID
Predicate<BikePart> findChainRingPredicate = (BikePart bp) => { return bp.Id == "Chain Ring ID"; };
// Use FindAll to get all matching BikePart objects
List<BikePart> chainRings = bikeParts.FindAll(findChainRingPredicate);
// Print the count and details of each found BikePart
Console.WriteLine($"Found {chainRings.Count} Chain Rings:");
foreach (BikePart chainRing in chainRings)
{
Console.WriteLine(chainRing.ToString());
}
}將 IronPDF 融入畫面。
我們的 C# Find 知識可以利用的一個關鍵領域是使用 IronPDF 進行 PDF 內容處理,IronPDF 是用於 PDF 處理的強大 C# 函式庫。
假設我們正在處理一份 PDF 文件,其中包含各種自行車零件的資訊。 通常,我們需要在這些內容中找出特定的部分。 這就是 IronPdf 與 C# Find 方法結合提供強大解決方案的地方。
首先,我們會使用 IronPDF 來 從我們的 PDF 中萃取文字,然後我們可以使用 Find 或 FindAll 方法來在萃取的文字中找出特定的部分。
using IronPdf;
using System;
using System.Collections.Generic;
using System.Linq;
public class Program
{
public static void Main()
{
// Load and extract text from a PDF document
PdfDocument pdf = PdfDocument.FromFile(@"C:\Users\Administrator\Desktop\bike.pdf");
string pdfText = pdf.ExtractAllText();
// Split the extracted text into lines
List<string> pdfLines = pdfText.Split('\n').ToList();
// Define a predicate to find lines that contain a specific text
Predicate<string> findChainRingPredicate = (string line) => { return line.Contains("Chain Ring ID"); };
// Use FindAll to get all lines containing the specified text
List<string> chainRingLines = pdfLines.FindAll(findChainRingPredicate);
// Print the count and content of each found line
Console.WriteLine($"Found {chainRingLines.Count} lines mentioning 'Chain Ring ID':");
foreach (string line in chainRingLines)
{
Console.WriteLine(line);
}
}
}using IronPdf;
using System;
using System.Collections.Generic;
using System.Linq;
public class Program
{
public static void Main()
{
// Load and extract text from a PDF document
PdfDocument pdf = PdfDocument.FromFile(@"C:\Users\Administrator\Desktop\bike.pdf");
string pdfText = pdf.ExtractAllText();
// Split the extracted text into lines
List<string> pdfLines = pdfText.Split('\n').ToList();
// Define a predicate to find lines that contain a specific text
Predicate<string> findChainRingPredicate = (string line) => { return line.Contains("Chain Ring ID"); };
// Use FindAll to get all lines containing the specified text
List<string> chainRingLines = pdfLines.FindAll(findChainRingPredicate);
// Print the count and content of each found line
Console.WriteLine($"Found {chainRingLines.Count} lines mentioning 'Chain Ring ID':");
foreach (string line in chainRingLines)
{
Console.WriteLine(line);
}
}
}在這段程式碼中,我們載入 PDF,擷取文字,將其分割成行,然後使用 FindAll 來找出所有提到"Chain Ring ID"的行。

這是 Find 方法如何與 IronPDF 一起用於實際情境的基本範例。 它展示了 C# 的實用性和多功能性,以及其強大的函式庫,這些函式庫可讓您的程式設計工作更輕鬆、更有效率。
結論
在本教程中,我們深入探討了 C# Find 方法及其親屬 FindIndex, FindLastIndex, 和 FindAll. 我們探討了這些工具的用途,探索了一些程式碼範例,並發掘了在哪些情況下它們最有效。
我們也使用 IronPDF 函式庫涉足 PDF 操作的世界。 同樣地,我們也看到了 Find 方法知識在 PDF 文件中提取和搜索內容的實際應用。
IronPDF 提供免費的 IronPDF試用版,提供絕佳的機會讓您探索其功能,並判斷它如何能讓您的 C# 專案獲益。 如果您在試用後決定繼續使用 IronPDF,許可證的起價為$799。
常見問題解答
C# Find 函數對開發人員來說是如何運作的?
C# 的 Find 函數允許開發人員在集合、陣列或清單中尋找滿足謂詞所定義特定條件的第一個元素。此功能有助於簡化編碼過程。
什麼是謂詞?它在 C# 中如何使用?
在 C# 中,謂詞是一種委託,它代表一個帶有特定條件的方法。它用於諸如Find類的方法中,以測試集合中的每個元素,並傳回符合條件的元素。
我可以在 C# 中使用自訂類別的 Find 方法嗎?
是的,您可以透過定義謂詞來符合類別元素的搜尋條件,從而將 Find 方法與自訂類別一起使用,例如尋找具有特定屬性值的物件。
如果在 Find 方法中沒有元素符合條件,會發生什麼情況?
如果 Find 方法中沒有元素符合謂詞指定的條件,則傳回預設值,例如參考類型傳回null ,值類型傳回0 。
C# 中的 Find 和 FindAll 有什麼差別?
Find 方法傳回第一個與謂詞相符的元素,而 FindAll 則傳回滿足謂詞條件的所有元素的清單。
FindIndex 和 FindLastIndex 方法有何不同?
FindIndex 傳回與謂詞相符的第一個元素的索引,而 FindLastIndex 傳回與條件相符的最後一個元素的索引。
如何將 PDF 庫與 C# Find 整合以實現文字搜尋?
透過使用 PDF 庫,您可以從 PDF 中提取文本,並利用查找方法在文本中搜尋特定內容,從而有效地進行文件處理。
是否可以使用 Find 功能依屬性搜尋元素?
是的,您可以根據元素屬性定義謂詞來搜尋特定條件,例如尋找具有特定 ID 或屬性的物件。
Find 方法在處理大型資料集時效率如何?
Find 方法執行線性搜索,依序檢查每個元素。雖然這種方法簡單直接,但由於其時間複雜度為 O(n),因此對於非常大的集合來說可能並非最高效的方法。
PDF庫能為C#開發人員帶來哪些好處?
PDF 庫提供強大的 PDF 處理功能,使開發人員能夠使用 C# 輕鬆提取、操作和搜尋 PDF 內容,從而提高與文件相關的任務的效率。







