Are a Static method's variables staic too?

G

Guest

Consider this a continuation of 2/27/2007 thread by Zytan entitled"Subject:
Does C# have static local variables like C++?"

In C++:
void mysub()
{
static double x;
blah; blah;
}

In C++ the variable x retained its value for subsequent entries into the
method

That being the objective, will the following accomplish the same in C sharp?

static void mysub()
{
double x;
blah; blah;
}
 
P

Peter Duniho

[...]
In C++ the variable x retained its value for subsequent entries into the
method

That being the objective, will the following accomplish the same in C
sharp?

static void mysub()
{
double x;
blah; blah;
}

No, it won't. Even in C++ it doesn't do the same thing.

Of course, counting the time it takes to receive even the first reply, you
could have figured it out yourself faster just by trying it. :)

Pete
 
W

Willy Denoyette [MVP]

Peter Duniho said:
[...]
In C++ the variable x retained its value for subsequent entries into the
method

That being the objective, will the following accomplish the same in C sharp?

static void mysub()
{
double x;
blah; blah;
}

No, it won't. Even in C++ it doesn't do the same thing.

Hmmm... this is perfectly valid C++...

static void Foo()
{
static int s;
s = 5;
}

Willy.
 
W

Willy Denoyette [MVP]

mark said:
Consider this a continuation of 2/27/2007 thread by Zytan entitled"Subject:
Does C# have static local variables like C++?"

In C++:
void mysub()
{
static double x;
blah; blah;
}

In C++ the variable x retained its value for subsequent entries into the
method

That being the objective, will the following accomplish the same in C sharp?

static void mysub()
{
double x;
blah; blah;
}

No, it won't, and no, static locals aren't supported in C#.

Willy.
 
P

Peter Duniho

Hmmm... this is perfectly valid C++...

static void Foo()
{
static int s;
s = 5;
}

So? That doesn't change the fact that *this*:

"static void mysub()
{
double x;
blah; blah;
}"

doesn't do what the poster wants.

Pete
 
W

Willy Denoyette [MVP]

Hmmm... this is perfectly valid C++...

static void Foo()
{
static int s;
s = 5;
}

So? That doesn't change the fact that *this*:

"static void mysub()
{
double x;
blah; blah;
}"

doesn't do what the poster wants.

Pete



Sorry, must have mis-interpreted this...
"No, it won't. Even in C++ it doesn't do the same thing."
as meaning "no you can't have static locals C++ either", which is the OP's real question,
isn't it?

Willy.
 
P

Peter Duniho

[...]
Sorry, must have mis-interpreted this...
"No, it won't. Even in C++ it doesn't do the same thing."
as meaning "no you can't have static locals C++ either", which is the
OP's real question, isn't it?

You did misinterpret. My comment refers to the code that I quoted in my
post. The OP stated his objective (static local in C#), and asked if the
code he posted would accomplish the objective. Not only would the code he
posted as a possible solution not achieve his objective in C#, it would
also not achieve his objective in C++.

"No, it won't" is in response to the question "will the following
accomplish the same in C sharp?". "Even in C++ it doesn't do the same
thing" is referencing the quoted code proposed as a solution C# (that is,
not only does it not accomplish the objective in C#, it doesn't even
accomplish it in C++). My point being that making a function static,
whether in C++ or C#, doesn't change the dynamic nature of the local
variables.

It's true that I failed to answer one of the questions in the post ("Does
C# have static local variables like C++?"). I overlooked it, given the
subject line (which seemed to focus on the question of the method's
attribute) and the code posted. Thankfully, you did address that one in
your reply to him.

Sorry for any confusion.

Pete
 
G

Guest

Thank you:

I'm very rusty at C. What is the effect of declaring a method as static as in:

static void mysub()
{
double x;
blah; blah;
}


Peter: "Of course, counting the time it takes to receive even the first
reply, you
could have figured it out yourself faster just by trying it. :)" You are
entirely correct sir, sorry. Did just that in fact. (I switched to decaf
Peter.)
 
K

Kevin Spencer

Hi Mark,

The effect is that the class in which the method resides does not have to be
instantiated to call the method. With a non-static method, you would have to
create an instance of the class which contains it in order to call it. The
System.Math static class is an example of a static class holding all static
methods. So that, for example, when you want to calculate the square root of
a number, you don't need to (and in fact, cannot, because the class itself
is static) create an instance of the System.Math class to use the
System.Math.Sqrt method. Example:

double d = Math.Sqrt(4D); // Yields the double 2D

--
HTH,

Kevin Spencer
Microsoft MVP

Printing Components, Email Components,
FTP Client Classes, Enhanced Data Controls, much more.
DSI PrintManager, Miradyne Component Libraries:
http://www.miradyne.net
 
J

Jon Skeet [C# MVP]

Kevin Spencer said:
The effect is that the class in which the method resides does not have to be
instantiated to call the method.

That's certainly true for C#, but C itself doesn't even *have*
instances. I can't remember the exact meaning of "static" in C, but
it's not the same as in C#.
 
P

Peter Duniho

That's certainly true for C#, but C itself doesn't even *have*
instances. I can't remember the exact meaning of "static" in C, but
it's not the same as in C#.

In C/C++, when applied to the actual function definition (ie "at file
scope") the "static" keyword prevents the function from being exposed in
the generated object file. That is, it can only be referenced by code
within the same file as no linkage information is produced for that
function.

When used that way it has nothing to do with the lifetime of local
variables within the function.

Pete
 
J

Jon Skeet [C# MVP]

Peter Duniho said:
In C/C++, when applied to the actual function definition (ie "at file
scope") the "static" keyword prevents the function from being exposed in
the generated object file. That is, it can only be referenced by code
within the same file as no linkage information is produced for that
function.

So sort of like private, right?
When used that way it has nothing to do with the lifetime of local
variables within the function.

Thank heavens for consistent usage, eh? ;)
 
P

Peter Duniho

[...] That is, it can only be referenced by code
within the same file as no linkage information is produced for that
function.

So sort of like private, right?

I think more like "internal" in C#.
Thank heavens for consistent usage, eh? ;)

Exactly. :) And as if C wasn't bad enough, C++ came along and added yet
another meaning to the keyword "static".

Of course, it's not like .NET is free of that sort of confusion either.
For example, the variant behavior of Invoke/BeginInvoke depending on
whether you use it on a form or a delegate. It's almost as if there's
something about language designers that drive them to overload names
within the language. :)

Pete
 
K

Kevin Spencer

Hi Jon,

I did assume that he was talking about C#, even though he specifically said
"C." Looking back, I'm not so certain that he was talking about C#.

--
HTH,

Kevin Spencer
Microsoft MVP

Printing Components, Email Components,
FTP Client Classes, Enhanced Data Controls, much more.
DSI PrintManager, Miradyne Component Libraries:
http://www.miradyne.net
 

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

Top