Answers for "c# delegate vs action"

C#
0

delegate c# mvc

using System;
namespace MVC
{
    public class Book
    {
        // declare a delegate for the bookpricechanged event
        public delegate void BookPriceChangedHandler(objectsender,
        BookPriceChangedEventArgs e);
        
        // declare the bookpricechanged event using the bookpricechangeddelegate
        public event BookPriceChangedHandlerBookPriceChanged;
        
        // instance variable for book price
        object _bookPrice;
        
        // property for book price
        public object BookPrice
        {
            set
            {
                // set the instance variable
                _bookPrice=value;
                
                // the price changed so fire the event!
                OnBookPriceChanged();
            }
        }
        
        // method to fire price canged event delegate with proper name
        // this is the method our observers should be implenting!
        protected void OnBookPriceChanged()
        {
            BookPriceChanged(this, new BookPriceChangedEventArgs(_bookPrice));
        }
    }
}
Posted by: Guest on August-19-2020
0

c# func vs action

//A Action is a container to set a method for invoking that has no returns 
//where as a Func is a container to set a method for invoking that has returns
//so
//Any Generals or Templates in a Action will always be signiture input params
//Where as to say a function will alway have a output return indicated by the
//last General or Template type and any others will be inputs params.
//Addtionally async invokers (.BeginInvoke) with actions can be fire and forget 
//where as a Funcs must be awaited for the result using the EndInvoke
//example using lambda's (Short handed anonymous methods):
//Actions
Action BasicAction = ()=>{/*does something*/};
Action</*in*/ Type> SingleParamiterizedAction = (x)=>{/*does something*/};
Action</*in*/ Type1, /*in*/ Type2> DoubleParamiterizedAction = (x, y)=>{/*does something*/};
//....
Func</*out*/ TResult> BasicFunc = ()=>{/*does something*/ return TheTResult; };
Func</*in*/ Type,/*out*/ TypeResult> SingleParamiterizedFunc = (x)=>{/*does something*/ return TheTypeResult; };
Func</*in*/ Type1, /*in*/ Type2, /*out*/ TypeResult> DoubleParamiterizedFunc = (x, y)=>{/*does something*/ return TheTypeResult; }
//Usage example
BasicAction.Invoke(); //result of void can not be used
TResult TheResult = BasicFunc(); // result of the methods output can be used.
Posted by: Guest on October-03-2020

C# Answers by Framework

Browse Popular Code Answers by Language