views:

603

answers:

5

I like to know if in C# there is a Function type like in AS3 for example. I would like to do somnthing like this (but in C#):

private function doSomething():void {
    // statements
}

var f:Function = doSomething
f()
+6  A: 

They (references to functions) are called delegates

Here's a wonderful primer: http://www.akadia.com/services/dotnet_delegates_and_events.html

Dinah
How they are implemented?
unkiwii
They're complete classes in the background, which constructors accept a function, but C# 2 and 3 have sugared that fact completely away, which is a good thing.
Dykam
+7  A: 

The concept of delegates in .NET are exactly what you are looking for.

There are examples on the linked page, as well as many tutorials and in-depth explanations of how they are implemented and used. Google away!

jscharf
Thank you very much! :)
unkiwii
You're welcome 8-)
jscharf
+5  A: 

Yes, there are type safe function pointers called Delegates. For example ...

void Main()
{
   object o1 = new object();
   object o2 = new object();

   Action<object, object> act = DoSomething;
   act( o1, o2 );
}

private void DoSomething( object o1, object o2 )
{
  // do that thang
}

Action<T> is the built in delegate ...

public delegate void Action<T1, T2>(T1 arg1, T2 arg2)

There are also anonymous functions called Lambda Expressions. For example ...

   List<int> lst = new List<int>() { 1, 2, 3, 4, 5 };
   var res = lst.Where( l => l > 3 );

The argument to the Where method is a Lambda Expression. Check out Eric White's blog for a great discussion on this stuff: Query Composition Using Functional Programming Techniques in C# 3.0.

JP Alioto
+20  A: 

Yes, they're called delegates in .NET, not function types.

You use the reserved keyword delegate to define new ones, and there's plenty that are predefined in the class libraries.

To define one that matches your example:

public delegate void DoSomethingDelegate(Object param1, Object param2);

Then to assign it:

DoSomethingDelegate f = DoSomething;
f(new Object(), new Object());

There's also two generic types of delegate types defined in the base class library, one for methods that return a value, and one for those who doesn't, and they come with variations over how many arguments you have.

The two are Func<..> for methods that return a value, and Action<..> for methods that doesn't.

In your case, this would work:

Action<Object, Object> f = DoSomething;
f(new Object(), new Object());

Note that in this last case, you don't have to declare DoSomethingDelegate.

Lasse V. Karlsen
Func and Action are heavily used in Linq.
Dykam
+4  A: 

I think you are looking for a C# delegate type.

    delegate void DoSomethingDelegate(object obj1, object obj2);

    void DoSomething(object a, object b)
    {
    }

    void RunDoSomething()
    {
        //use your delegate
        DoSomethingDelegate d = new DoSomethingDelegate(DoSomething);
        d(new object(), new object());

        //alernatively, use the built in generic version
        Action<object, object> d2 = new Action<object, object>(DoSomething);
        d2(new object(), new object());

    }
MedicineMan