В C# существует ли Функциональный тип?

Python, 436 символов

(443 включая дополнительные новые строки)

print """
eJzlk91xxCAMhN9TxRZwSRMp4GpQjGw04ccDunOu+wjcgMkkk8vlxRjQst9q4JygnjFLqQpHN+QZ
r75I1UgV8QYtF0bIV8ZC9tGMyCCsVLSIWxiS2pSpWCnzy9N5P7HylJM7fqRuGXopGhjOtuoJZPID
RuqljPh4E2MunCYPz8mMvmw9Z1P5496tHhOFIGnBm2E38+/kkXkIR9o8B4diQGbz03xVPkb4rBwL
c7X79hzoZlCnX2DmK6ch6iZA3ShV466bxNjJ7yINy6J+JE0XIJK4liZKeN/D3HPIJIlHMrZ6BHLC
1URp6tR/MbiO3VQrt7XSAwamtUM/TDM4jL7cXYFVVi61DR38sbukG4fr0GvZFXDlEmPrU//Z8/+j
/n0CsHRGTA==
""".decode("base64").decode("zlib")
19
задан balpha 21 August 2009 в 17:43
поделиться

5 ответов

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.

36
ответ дан 30 November 2019 в 02:16
поделиться

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!

8
ответ дан 30 November 2019 в 02:16
поделиться

They (references to functions) are called delegates

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

6
ответ дан 30 November 2019 в 02:16
поделиться

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 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.

5
ответ дан 30 November 2019 в 02:16
поделиться

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());

    }
4
ответ дан 30 November 2019 в 02:16
поделиться
Другие вопросы по тегам:

Похожие вопросы: