Function within method (Simple question?)

  • Thread starter Thread starter Håkan Johansson
  • Start date Start date
H

Håkan Johansson

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
 
Mark said:
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.
 
Håkan Johansson said:
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?

As others have said, the only similar thing is anonymous methods. See
http://www.pobox.com/~skeet/csharp/csharp2/delegates.html

However, unless I needed delegate behaviour, I'd normally just create
another private method... any reason you want to create it within the
method?
 
Dustin said:
Why use DynamicInvoke? This is fine:

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

localFunction();
localFunction();
}

Because it is was an order of magnitude more complicated than your
suggestion. Clearly, any self respecting programmer would use my
suggestion, if for nothing else, because it portrays a sense of
superiority and a command of the .NET Framework. :)
 
Jon said:
As others have said, the only similar thing is anonymous methods. See
http://www.pobox.com/~skeet/csharp/csharp2/delegates.html

However, unless I needed delegate behaviour, I'd normally just create
another private method... any reason you want to create it within the
method?

One reason can be so it can access locals declared in the scope of the
enclosing method. Anonymous delegates have this behaviour too - via
closure / capturing.

-- Barry
 
Why use DynamicInvoke? This is fine:
Because it is was an order of magnitude more complicated than your
suggestion. Clearly, any self respecting programmer would use my
suggestion, if for nothing else, because it portrays a sense of
superiority and a command of the .NET Framework. :)

ROFL

Best Regards,
Dustin Campbell
Developer Express Inc
 
Barry Kelly said:
One reason can be so it can access locals declared in the scope of the
enclosing method. Anonymous delegates have this behaviour too - via
closure / capturing.

True, true. Unless I actually needed to pass a delegate somewhere (or
call BeginInvoke or something) I would usually make that state explicit
somewhere.
 
IIRC, it is another method is *another class with an unknown name. Not that
it matters, but it is a bit more then sugar imo.

--
William Stacey [C# MVP]

| :-) I guess you can use anonymous methods, good call, but it is syntactic
| sugar for really creating another method in the class.
|
| Mark.
| --
| http://www.markdawson.org
| http://themightycoder.spaces.live.com
|
|
| "Brian Gideon" wrote:
|
| > 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
| >
| >
 
yup, it is good stuff. Have been using more and more as of late.

--
William Stacey [C# MVP]

| Jon Skeet 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?
| >
| > As others have said, the only similar thing is anonymous methods. See
| > http://www.pobox.com/~skeet/csharp/csharp2/delegates.html
| >
| > However, unless I needed delegate behaviour, I'd normally just create
| > another private method... any reason you want to create it within the
| > method?
|
| One reason can be so it can access locals declared in the scope of the
| enclosing method. Anonymous delegates have this behaviour too - via
| closure / capturing.
|
| -- Barry
|
| --
| http://barrkel.blogspot.com/
 
IIRC, it is another method is *another class with an unknown name. Not
that
it matters, but it is a bit more then sugar imo.

That's correct. An anonymous method in code results in an additional class
being created by the compiler, not an additional method in the current
class. The reason for this approach are mainly captures, i.e. the ability
to reference variables from the "outer" method inside the "inner" method.
Look at Reflector, it's interesting to see.


Oliver Sturm
 
That's correct. An anonymous method in code results in an additional class
being created by the compiler, not an additional method in the current
class. The reason for this approach are mainly captures, i.e. the ability
to reference variables from the "outer" method inside the "inner" method.
Look at Reflector, it's interesting to see.

Bah... I hate having to follow up on myself, but I feel I should have
written this more clearly.

The thing is, that additional class I'm talking about is created only if
it's needed, which happens as a result of captures actually being used. So
if you want to look at Reflector as I suggest, and see what I mean, you'll
have to make sure there's actually a variable from the outer method being
used inside the anonymous method.

Hm. I hope that's better. Just didn't want to let it stand like that :-)


Oliver Sturm
 
William Stacey said:
IIRC, it is another method is *another class with an unknown name. Not that
it matters, but it is a bit more then sugar imo.

It depends on whether there are any captured variables. IIRC (and I'm
afraid I'm too lazy to check tonight):

1) If the method doesn't capture any variables, or just static ones, an
extra static method is used

2) If the method captures instance variables of the current instance,
an extra instance method is used

3) If any other variables (locals, parameters etc) are used, a new
class is created. Sometimes more than one class is required, if
different variables are captured with different scopes.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top