首頁  >  文章  >  後端開發  >  C# 謂詞

C# 謂詞

王林
王林原創
2024-09-03 15:27:25935瀏覽

內建泛型類型委託是 C# 中的謂詞委託,在命名空間系統下定義。包含特定一組條件的命名空間和方法可以與謂詞委託一起使用,以確定傳遞的參數是否滿足給定的條件,並且此條件僅採用一個輸入,傳回值true 或false 以及謂詞委託與其他委託Func delegate和Action delegate 相同。

文法:

public delegate bool Predicate <in P>(P obj);

其中物件類型由 P 表示,obj 是比較方法內定義的條件並由謂詞委託表示的物件。

C# 中謂詞委託的工作

  • 傳回 true 或 false 的函數是謂詞,對謂詞的引用是謂詞委託。
  • 謂詞委託的功能是隨著.NET 2.0的發布而引入的。框架。
  • 可以定義謂詞函數,並且可以透過謂詞委託將其作為參數傳遞給任何其他函數。
  • Func 的一種特殊情況是謂詞委託,它只接受一個參數作為輸入,並傳回一個布林值,該值要麼為 true,要麼為 false。
  • 任何方法都可以寫在謂詞委託中,甚至是 lambda 表達式或匿名方法。
  • 當泛型類型與 lambda 表達式一起使用時,謂詞委託將其作為參數。

C# 謂詞範例

下面給出了提到的範例:

範例#1

C# 程序,示範在程式中使用謂詞委託來檢查作為參數傳遞的給定字串是否為大寫字母。

代碼:

using System;
//a namespace called program is defined
namespace program
{
//a class called check is defined
public class check
{
//a Boolean method is defined to check if the given string is written in capital letters or not. If written in capital letters, true is returned else False is returned.
static bool IsUC(string stri)
{
return stri.Equals(stri.ToUpper());
}
//main method is called
static void Main(string[] args)
{
//a predicate delegate is defined with object type as string and IsUC is an object which compares the criteria that is defined within a method and is represented by predicate delegate.
Predicate<string> isU = IsUC;
//The result of the predicate delegate is stored in a variable called res
bool res = isU("welcome to c#");
//the result is displayed
Console.WriteLine(res);
}
}
}

輸出:

C# 謂詞

說明:

  • 在上面的程式中,定義了一個名為program的命名空間。然後定義一個名為check的類別。然後定義一個布林方法來檢查給定的字串是否是大寫字母。如果給定字串是大寫字母,則傳回 true,否則傳回 False。然後呼叫main方法。
  • 然後定義一個謂詞委託,對象類型為字串,IsUC 是一個對象,它比較方法中定義的條件並由謂詞委託表示。然後謂詞委託的結果就儲存在名為 res 的變數中。然後顯示結果。

範例#2

C# 程序,示範在程式中使用謂詞委託來檢查給定字串的長度是否小於指定值。

代碼:

using System;
//a class called program is defined
class program
{
// a predicate delegate is defined with object type as string
public delegate bool my_del(string stri);
// a method is defined inside a predicate delegate by passing the object as parameter to check if the length of the given string is less than a specified value. If less than the given specified value, true is returned else false is returned
public static bool fun(string stri)
{
if (stri.Length < 5)
{
return true;
}
else
{
return false;
}
}
//Main method is called
static public void Main()
{
// a predicate delegate is defined with object type as string and fun is an object which compares the criteria that is defined within a method and is represented by predicate delegate.
my_del obj = fun;
//The string to be passed as a parameter to predicate delegate is written here
Console.WriteLine(obj("Shobha"));
}
}

輸出:

C# 謂詞

說明:

  • 在上面的程式中,定義了一個名為program的類別。然後將物件類型定義為字串的謂詞委託。然後在謂詞委託中定義一個方法,透過將物件作為參數傳遞來檢查給定字串的長度是否小於指定值。如果字串的長度小於給定的指定值,則傳回 true,否則傳回 false。
  • 然後呼叫Main方法。然後定義謂詞委託,對象類型為字串,fun 是對象,它比較方法內定義的條件並由謂詞委託表示。然後最後寫入要作為參數傳遞給謂詞委託的字串。

優點

以下是 C# Predicate 的優點:

  • 當我們必須過濾掉值清單時,謂詞委託非常有用。
  • 謂詞委託可以內聯以實現一次性搜尋功能。
  • 當我們必須在通用集合中搜尋項目時,可以使用謂詞委託。
  • 透過使用謂詞委託,可以縮短程式碼長度,並且傳回 true 或 false。
  • 匿名方法、lambda 表達式可以指派給謂詞委託。
  • 謂詞委託提供執行階段的邏輯,它可以是簡單的邏輯,也可以是複雜的邏輯。

以上是C# 謂詞的詳細內容。更多資訊請關注PHP中文網其他相關文章!

陳述:
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn