-2

im trying to do an delegate that can take anything or return anything.Why simply run and understanding the limit. So far , the only way i know is by using the "object" key word everywhere and passing a list of onbject for parameter. To me it seem pretty unsafe and not realy easy to use.

exemple :

     delegate object doAnyFunction(List<object> inputs);

            public static object writeMessages(List<object> inputs)
            {

                Console.WriteLine("hi " + inputs[0] + " i K=know u have " + inputs[1]+ " year old");
                return null;
            }

            static void Main(string[] args)
            {
                string name = "tomas";
                int age = 26;

                doAnyFunction doAny = new doAnyFunction(writeMessages);
                doAny(new List<object> {name,age});

                Console.Read();

            }

console output result : hi tomas i know u have 26 year old

So eveything work for shure. Any have an idia on how i can improve this ? more safe more easy to use .... etc

thanks! feel free to debate!


Update:22/07/2015

Good I finally try all possibility and I finally come to a really simple result! Thanks to Mike Hixson and his idea.... he put me on the right track.

So the big thing about action and func<> is you can’t put them as a parameter in a function. So le just show an example real quick....

    Public delegate object DoAnything();
    Class Program
    {

        Static int additioner(int num1 , int num2)
        {

            return num1 + num2;
        }
        static object dostuff(DoAnything doit)
        {
            return doit();
        }
        static object writeMessage(string message)
        {
            Console.WriteLine(message);
            return true;
        }

        static void Main(string[] args)
        {
            DoAnything del1 = delegate() { return additioner(1, 4); };
            DoAnything del2 = delegate() { return writeMessage("hi it Work! haha"); };
            Console.WriteLine(dostuff(del1));
            del2();

            Console.Read();

        }

Console return :

5 Hi it Work! haha

So all result is what expected.

Has u can see i can use the return of the function i use with my delegate and use a function with any number or type of parameter. Goal achieved! The only little thing that is not perfect is ... with this way i have no choice of using a function with a return.... but a little return true on a function never heart anyway.

If any have a better idea or an improvement just say it i will be glad to test it.

Kazimar
  • 375
  • 1
  • 3
  • 12
  • Why would you want to do this? What problem are you trying to solve? – Blorgbeard Jul 22 '15 at 03:25
  • I dont realy have any problem . Im just imagining what cool stuff i can do with it ... like doing framework or stuff like that .... it would be cool to have , let say a frame workk for a design pattern observer that u can use extremely easy ... some thing like subject.subscribe(oberver , delegate for doing what the notify do). I dont know i realy cleat haha , but i hope u can understand – Kazimar Jul 22 '15 at 03:29
  • I you believe that giving up on type-safety will allow you to do cooler things you may want to consider 'going back' to `C` .. – TaW Jul 22 '15 at 06:17

1 Answers1

2

If you want a type-safe delegate that can take in a number of arguments you can use the Action delegate:

https://msdn.microsoft.com/en-us/library/018hxwa8(v=vs.110).aspx

There are 16 implementations of this delegate that allow for 1 to 16 arguments to be specified.

If your delegate must return a value, then use the Func delegate:

https://msdn.microsoft.com/en-us/library/bb534960(v=vs.110).aspx

This one also has 16 implementations to allow for 1 to 16 arguments.

UPDATE

Here is an example. Setup 2 functions that can be used with Action<string, int>:

static void WriteMessageOriginal(string name, int age)
{
    Console.WriteLine("hi " + name + " i K=know u have " + age + " year old");
}

static void WriteMessageNew(string name, int age)
{
    Console.WriteLine("hi {0}, I know you are {1} years old", name, age);
}

Create a function that can take Action<string, int> and invoke it with a string and an integer.

static void DoMessageDelgate(Action<string, int> mydelegate)
{
    mydelegate("tomas", 26);
}

Call the method with different delegates.

static void Main()
{
    DoMessageDelgate(WriteMessageOriginal);
    DoMessageDelgate(WriteMessageNew);
}
Mike Hixson
  • 5,071
  • 1
  • 19
  • 24
  • Ok ty for your idia! it seen real better alternative! let me try it and i update the result! – Kazimar Jul 22 '15 at 03:45
  • HUmm the probleme with action/func is i dont find anyway to use them on a parameter of a function ..... ? do you know any way? cause i can with the delegate. ex : delegate void mydeg(); --> public void methode(mydeg deg){deg() } – Kazimar Jul 22 '15 at 04:38