func in c# examples
Example 1: How to make a function in C#
public void SayHello(string name)
{
Console.WriteLine("Hello")
}
Console.WriteLine("What is your name?")
string name = Console.ReadLine() ;
SayHello(Adam)
Example 2: 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;
}
}