Posts

Anonymous Methods in C#

Image
  Why do we need Anonymous Methods in C#? In our   Delegates in C#   article, we discussed how to bind a delegate with a method. To bind a delegate with a method, first, we need to create an instance of the delegate and when we create the instance of the delegate, we need to pass the method name as a parameter to the delegate constructor, and it is the function the delegate will point to and when we invoke the delegate, this function which is pointed by the delegate is going to be executed. It is also possible that the delegates might be pointed to multiple functions and in that case, when we invoke the delegate instance, then all the functions which are pointed by the delegate are going to be executed. Example to Understand Delegate in C#: Before understanding the anonymous method, let us first see how we can use a delegate to execute the method. Why because to understand the Anonymous method, it is important to understand delegate because anonymous methods are used dele...

C# Abstraction

C# OOPs

Object-oriented programming is a way of developing software applications using real-world terminologies to create entities (classes) that interact with one another using objects.  

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...