Posts

Showing posts from January, 2024

C# Predicate Delegate

Image
Predicate   is the delegate like   Func   and   Action   delegates. It represents a method containing a set of criteria and checks whether the passed parameter meets those criteria. A predicate delegate methods must take one input parameter and return a boolean - true or false. The  Predicate  delegate is defined in the  System  namespace, as shown below: Predicate signature:  public   delegate   bool   Predicate < in  T>(T obj); Same as other delegate types,  Predicate  can also be used with any method, anonymous method, or lambda expression. Example: Predicate delegate static bool IsUpperCase( string str) { return str.Equals(str.ToUpper()); } static void Main( string [] args) { Predicate < string > isUpper = IsUpperCase; bool result = isUpper( "hello world!!" ); Console .WriteLine(result); } Try it Output: false An anonymous method can also be assigned to a Predicate d...

C# Action Delegate

Image
Action   is a delegate type defined in the   System   namespace. An Action type delegate is the same as   Func delegate   except that the Action delegate doesn't return a value. In other words, an Action delegate can be used with a method that has a void return type.  For example, the following delegate prints an int value. Example: C# Delegate public delegate void Print( int val); static void ConsolePrint( int i) { Console .WriteLine(i); } static void Main( string [] args) { Print prnt = ConsolePrint; prnt(10); } Output: 10 You can use an Action delegate instead of defining the above Print delegate, for example: Example: Action delegate static void ConsolePrint( int i) { Console .WriteLine(i); } static void Main( string [] args) { Action < int > printActionDel = ConsolePrint; printActionDel(10); } Try it You can initialize an Action delegate using the new keyword or by directly assigning a method: ...

C# Func Delegate

Image
C# includes built-in generic delegate types   Func   and   Action , so that you don't need to define custom delegates manually in most cases. Func  is a generic delegate included in the  System  namespace. It has zero or more  input  parameters and one  out  parameter. The last parameter is considered as an out parameter. The  Func  delegate that takes one input parameter and one out parameter is defined in the  System  namespace, as shown below: Signature: Func namespace System { public delegate TResult Func< in T, out TResult>(T arg); } The last parameter in the angle brackets  <>  is considered the return type, and the remaining parameters are considered input parameter types, as shown in the following figure. Func delegate A Func delegate with two input parameters and one out parameters will be represented as shown below. Func delegate The following  Func  delegate take...

C# Delegates

Image
The delegate is a reference type data type that   defines the method signature . You can define variables of delegate, just like other data type, that can refer to any method with the same signature as the delegate. There are three steps involved while working with delegates: Declare a delegate Create an instance and reference a method Invoke a delegate A delegate can be declared using the  delegate  keyword followed by a function signature, as shown below. Delegate Syntax [access modifier] delegate [return type] [delegate name]([parameters]) The following declares a delegate named  MyDelegate . Example: Declare a Delegate public delegate void MyDelegate( string msg ); Above, we have declared a delegate  MyDelegate  with a  void  return type and a string parameter. A delegate can be declared outside of the class or inside the class. Practically, it should be declared out of the class. After declaring a delegate, we need to set the target ...