方法内的功能(简单问题?) [英] Function within method (Simple question?)

查看:50
本文介绍了方法内的功能(简单问题?)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

来自Delphi,我试图在一个方法中声明一个函数,但是

不能让编译器swollow it。它是否可能?


即:


SomeMethod()

{


LocalFunction()

{

//陈述;

}


LocalFunction();

//语句;

LocalFunction();

}


谢谢!


问候H?kan Johansson

解决方案

H?kan,


您使用的是C#2.0吗?如果是这样,那么下面就会编译。


void SomeMethod()

{

ThreadStart方法=委托()

{

//声明

}


method.DynamicInvoke(null);

//语句

method.DynamicInvoke(null);

}


Brian


H?kan Johansson写道:


来自Delphi,我试图在方法中声明一个函数,但是

无法使编译器吞噬它。它是否可能?


即:


SomeMethod()

{


LocalFunction()

{

//陈述;

}


LocalFunction();

//语句;

LocalFunction();

}


谢谢!


问候H?kan Johansson


Mark R. Dawson写道:


你好H?kan,
$ C $ b在C#中你不能在另一个方法中声明一个方法。



马克,


你可以在2.0。你不能说出他们的名字。这就是为什么他们被称为

匿名方法:)


Brian

您使用的是C#2.0吗?如果是,那么以下将编译。


>

void SomeMethod()

{

ThreadStart方法=代表()

{

//陈述

}

method.DynamicInvoke(null);

//语句

method.DynamicInvoke(null);

}



为什么使用DynamicInvoke?这很好:


void SomeMethod()

{

String.Threading.ThreadStart localFunction = delegate

{

//语句

}


localFunction();

localFunction( );

}


最诚挚的问候,

Dustin Campbell

Developer Express Inc.


Coming from Delphi, I''ve tried to declare a function within a method, but
can''t get the compiler to "swollow it". Is it at all possible?

That is:

SomeMethod()
{

LocalFunction()
{
//Statements;
}

LocalFunction();
//Statements;
LocalFunction();
}

Thank you!

Regards H?kan Johansson

解决方案

H?kan,

Are you using C# 2.0? If so then the following would compile.

void SomeMethod()
{
ThreadStart method = delegate()
{
// Statements
}

method.DynamicInvoke(null);
// Statements
method.DynamicInvoke(null);
}

Brian

H?kan Johansson wrote:

Coming from Delphi, I''ve tried to declare a function within a method, but
can''t get the compiler to "swollow it". Is it at all possible?

That is:

SomeMethod()
{

LocalFunction()
{
//Statements;
}

LocalFunction();
//Statements;
LocalFunction();
}

Thank you!

Regards H?kan Johansson


Mark R. Dawson wrote:

Hi H?kan,
in C# you cannot declare a method inside another method.

Mark,

You can in 2.0. You just can''t name them. That''s why they''re called
anonymous methods :)

Brian


Are you using C# 2.0? If so then the following would compile.

>
void SomeMethod()
{
ThreadStart method = delegate()
{
// Statements
}
method.DynamicInvoke(null);
// Statements
method.DynamicInvoke(null);
}

Why use DynamicInvoke? This is fine:

void SomeMethod()
{
String.Threading.ThreadStart localFunction = delegate
{
// Statements
}

localFunction();
localFunction();
}

Best Regards,
Dustin Campbell
Developer Express Inc.


这篇关于方法内的功能(简单问题?)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆