M
Markus Freitag
Guest
Hello,
I want to create a function for NamedMethod2 because the steps are larger than one line.
NamedMethod2
public Func<int, string> AnonymousFuncDelegate = (digit) => { return string.Format("Anonymous said: {0}", digit); };
public Func<int, string> AnonymousFuncDelegate2 = (digit) => { NamedMethod2; };
How can I generate that with lambda? What do I have to do?
static void Main(string[] args)
On the edge, is it possible to call the main function without static, if so, how?
Best regards, Markus
// Here is my code.
class Program
{
public delegate string Foo(DateTime time);
static public string BarTime(DateTime value)
{
var str = value.ToString("t");
Console.WriteLine("{0}", str);
return str;
}
static public string BarDate(DateTime value)
{
var str = value.ToString("dd/MM/yyyy");
Console.WriteLine("{0}", str);
return str;
}
static void Main(string[] args)
{
var now = DateTime.UtcNow;
Foo fooTime,
fooDate,
fooMulticast;
fooTime = BarTime;
fooDate = BarDate;
fooMulticast = fooTime + fooDate;
Console.Write("FooTime: \n----\n");
fooTime(now);
Console.WriteLine();
/* - OUTPUT
FooTime:
----
04:23
*/
Console.Write("FooDate: \n----\n");
fooDate(now);
Console.WriteLine();
/* - OUTPUT
FooDate:
----
21/04/2016
*/
Console.Write("FooMulticast: \n----\n");
fooMulticast(now);
/* - OUTPUT
FooMulticast:
----
04:23
21/04/2016
*/
Action myAction = new Action();
myAction.Example();
Func myFunc = new Func();
myFunc.Example();
myFunc.NamedFuncDelegate = NamedMethodGlobal;
string namedGlobalResult = myFunc.NamedFuncDelegate.Invoke(99);
Console.WriteLine(namedGlobalResult);
}
static public string NamedMethodGlobal(int digit)
{
return string.Format("Named Global said: {0}", digit);
}
// --------------------------
public class Action
{
public Action<string, int> NamedActionDelegate { get; set; }
public Action<string, int> AnonymousActionDelegate = (text, digit) => Console.WriteLine("Anonymous said: {0} {1}", text, digit);
public Action()
{
}
public void NamedMethod(string text, int digit)
{
Console.WriteLine("Named said: {0} {1}", text, digit);
}
public void Example()
{
// Named method
this.NamedActionDelegate = NamedMethod;
this.NamedActionDelegate.Invoke("Hi", 5);
// Output > Named said: Hi 5
// Anonymous Function > Lambda
this.AnonymousActionDelegate.Invoke("Foooo", 106);
// Output > Anonymous said: Foooo 106
}
}
// --------------------------
public class Func
{
public Func<int, string> NamedFuncDelegate { get; set; }
public Func<int, string> AnonymousFuncDelegate = (digit) => { return string.Format("Anonymous said: {0}", digit); };
// public Func<int, string> AnonymousFuncDelegate2 = (digit) => { NamedMethod2; };
public Func()
{
}
public void Example()
{
// Named method
this.NamedFuncDelegate = NamedMethod;
string namedResult = this.NamedFuncDelegate.Invoke(5);
Console.WriteLine(namedResult);
// Output > Named said: 5
// Anonymous Function > Lambda
string anonyResult = this.AnonymousFuncDelegate.Invoke(106);
Console.WriteLine(anonyResult);
// Output > Anonymous said: 106
}
public string NamedMethod(int digit)
{
return string.Format("Named said: {0}", digit);
}
public string NamedMethod2(int digit)
{
return string.Format("Named2 said: {0}", digit);
}
}
}
Continue reading...
I want to create a function for NamedMethod2 because the steps are larger than one line.
NamedMethod2
public Func<int, string> AnonymousFuncDelegate = (digit) => { return string.Format("Anonymous said: {0}", digit); };
public Func<int, string> AnonymousFuncDelegate2 = (digit) => { NamedMethod2; };
How can I generate that with lambda? What do I have to do?
static void Main(string[] args)
On the edge, is it possible to call the main function without static, if so, how?
Best regards, Markus
// Here is my code.
class Program
{
public delegate string Foo(DateTime time);
static public string BarTime(DateTime value)
{
var str = value.ToString("t");
Console.WriteLine("{0}", str);
return str;
}
static public string BarDate(DateTime value)
{
var str = value.ToString("dd/MM/yyyy");
Console.WriteLine("{0}", str);
return str;
}
static void Main(string[] args)
{
var now = DateTime.UtcNow;
Foo fooTime,
fooDate,
fooMulticast;
fooTime = BarTime;
fooDate = BarDate;
fooMulticast = fooTime + fooDate;
Console.Write("FooTime: \n----\n");
fooTime(now);
Console.WriteLine();
/* - OUTPUT
FooTime:
----
04:23
*/
Console.Write("FooDate: \n----\n");
fooDate(now);
Console.WriteLine();
/* - OUTPUT
FooDate:
----
21/04/2016
*/
Console.Write("FooMulticast: \n----\n");
fooMulticast(now);
/* - OUTPUT
FooMulticast:
----
04:23
21/04/2016
*/
Action myAction = new Action();
myAction.Example();
Func myFunc = new Func();
myFunc.Example();
myFunc.NamedFuncDelegate = NamedMethodGlobal;
string namedGlobalResult = myFunc.NamedFuncDelegate.Invoke(99);
Console.WriteLine(namedGlobalResult);
}
static public string NamedMethodGlobal(int digit)
{
return string.Format("Named Global said: {0}", digit);
}
// --------------------------
public class Action
{
public Action<string, int> NamedActionDelegate { get; set; }
public Action<string, int> AnonymousActionDelegate = (text, digit) => Console.WriteLine("Anonymous said: {0} {1}", text, digit);
public Action()
{
}
public void NamedMethod(string text, int digit)
{
Console.WriteLine("Named said: {0} {1}", text, digit);
}
public void Example()
{
// Named method
this.NamedActionDelegate = NamedMethod;
this.NamedActionDelegate.Invoke("Hi", 5);
// Output > Named said: Hi 5
// Anonymous Function > Lambda
this.AnonymousActionDelegate.Invoke("Foooo", 106);
// Output > Anonymous said: Foooo 106
}
}
// --------------------------
public class Func
{
public Func<int, string> NamedFuncDelegate { get; set; }
public Func<int, string> AnonymousFuncDelegate = (digit) => { return string.Format("Anonymous said: {0}", digit); };
// public Func<int, string> AnonymousFuncDelegate2 = (digit) => { NamedMethod2; };
public Func()
{
}
public void Example()
{
// Named method
this.NamedFuncDelegate = NamedMethod;
string namedResult = this.NamedFuncDelegate.Invoke(5);
Console.WriteLine(namedResult);
// Output > Named said: 5
// Anonymous Function > Lambda
string anonyResult = this.AnonymousFuncDelegate.Invoke(106);
Console.WriteLine(anonyResult);
// Output > Anonymous said: 106
}
public string NamedMethod(int digit)
{
return string.Format("Named said: {0}", digit);
}
public string NamedMethod2(int digit)
{
return string.Format("Named2 said: {0}", digit);
}
}
}
Continue reading...