c# delegate func code example
Example 1: C# func
Func<string> YourFuncProperty
= new Func(()=>{ return "or return what you want";});
Func<string> YourFuncProperty
= ()=>{return "or return what you want";};
Func<int
,
string> YourParamitarizedFuncProperty =
(x)=>
{return $"you entered a {x} or return what you want";};
string YouReturn = YourFuncProperty.Invoke();
string YouReturn = YourParamitarizedFuncProperty.Invoke(5);
YourFuncProperty.BeginInvoke();
YourParamitarizedFuncProperty.BeginInvoke(5);
string YouReturn = YourFuncProperty.EndInvoke();
string YouReturn = YourParamitarizedFuncProperty.EndInvoke(5);
Func<string> YourActionDefinedProperty = YourDefinedMethod;
string YourDefinedMethod()
{
return "or return what you want";
}
public sealed class DataContainer
{
static func<string> SealedFuncStringOverride;
DataContainer(datetime Date)
{
this.Date = Date;
}
public datetime Date {get; private set;}
public int Amount {get; private set;}
public string Info {get; private set;}
public string FirstName {get; private set;}
public override string ToString()
{
if(SealedFuncStringOverride!=null)
return SealedFuncStringOverride.BeginInvoke();
return base.ToString;
}
}
Example 2: delegate function c#
public delegate void Del(string message);
public static void DelegateMethod(string message)
{
Console.WriteLine(message);
}
Del hadler = DelegateMethod;
hadler("Hello World");
Example 3: c# func
using System;
using System.Collections.Generic;
using static System.Console;
namespace CSFlow.Delegates.Others
{
public class ActionFunc
{
public static void Run ()
{
Action<string> display = new Action<string>(DisplayMessage);
display("Calculate Discount :");
Func<double, double> discount = new Func<double, double>(Discount);
display(discount(12.5).ToString());
List<Customer> custList = new List<Customer>();
custList.Add(new Customer { Id = 1, FirstName = "Joydip", LastName = "Kanjilal", State = "Telengana", City = "Hyderabad", Address = "Begumpet", Country = "India" });
custList.Add(new Customer { Id = 2, FirstName = "Steve", LastName = "Jones", State = "OA", City = "New York", Address = "Lake Avenue", Country = "US" });
custList.Add(new Customer { Id = 3, FirstName = "Sefat", LastName = "Anam", State = "OSA", City = "New York", Address = "Manhatten", Country = "US" });
Predicate<Customer> FindAddress = customer => customer.Address == "Manhatten";
Customer searchData = custList.Find(FindAddress);
display($"{searchData?.FirstName} {searchData?.LastName} From - {searchData?.City} ");
ReadKey();
}
static void DisplayMessage (string message)
{
WriteLine(message);
}
static double Discount (double money)
{
return money * .5;
}
class Customer
{
public int Id { get; set; }
public string FirstName { get; set; }
public string LastName { get; set; }
public string Address { get; set; }
public string City { get; set; }
public string State { get; set; }
public string Country { get; set; }
}
}
}
Example 4: c# delegate func
Func<string, string> convert = delegate(string s)
{ return s.ToUpper();};
string name = "Dakota";
Console.WriteLine(convert(name));