RajasekharwhY / Build-your-muscle

Learning concepts and sample practice code - C#, MVC, SQL etc..
2 stars 0 forks source link

Explain about Func and Action Delegate pointers #26

Closed RajasekharwhY closed 5 years ago

RajasekharwhY commented 5 years ago

Func, Action, Delegate type, Anonymous functions, Lambda expressions

Func is a delegate (Pointer to a method) which takes zero or more input parameters and returns a TResult (value or reference type). Func < TResult> : Function delegate which takes no inputs but returns TResult type. Func < T, TResult> (T arg) : Function delegate which takes one Input org T and Returns TResult type. Func <T1, T2, TReust> (T1 arg1, T2 arg2) : Function delegate which takes T1 and T2 input arguments and Returns TResult Type.

Lambda expressions add syntactical sugar when creating delegate types (In this case func, action delegates). Lambda expression is a anonymous function that can contains expressions and statements as mentioned above these are used to create delegates types or expression tree types.

The lambda expressions use => lambda operator , which reads as "Goes To". The left side of lambda specify the input parameters if any, right side of lambda expression specifies the expression or statement block.

Action is a delegate type (pointer to a method) which takes zero or more parameters but it did not return any value.

Below link has very good example of explanation with examples : https://docs.microsoft.com/en-us/dotnet/api/system.func-1?view=netframework-4.7.2

Good resource on c# cook book: some chapters are available online https://docs.microsoft.com/en-us/previous-versions/visualstudio/visual-studio-2008/ff518995(v=orm.10)

https://mycomputersciencebooks.files.wordpress.com/2017/08/c-6-0-cookbook-4th-edition.pdf