By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
445,812 Members | 1,324 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 445,812 IT Pros & Developers. It's quick & easy.

novice advice on static use

P: n/a
Hi

I am learning C# from books. I am trying to understand the use of the word
'Static' on a method.

When i look in the autos area (I often like to see what values are being
resolved) i see that there are also static members and non public members.

Could you please explain to me how the static method differs from others,
how static members differ from others?

Some text and an example would be great, but a link to a novice focuseed
website would be good to.

Thanks in advance

Doug
Sep 1 '06 #1
Share this Question
Share on Google+
7 Replies


P: n/a
A static item is bound to the type as opposed to the instance.

A great example would be ...

public class Foo {
public static int Bar;
public int Bar2;
}

Foo f = new Foo();
Foo f1 = new Foo();

f.Bar = 22;
f.Bar2 = 22;
f1.Bar = 33;
f.Bar2 = 33;

f.Bar is 33
f.Bar2 is 22
f1.Bar is 33
f1.Bar2 is 33

Sothe variable is shared amoungst all instances of the class ..

for methods they are called differently ..

public class Foo {
public static void Bar() {
Cons0ole.WriteLine("Bar");'
}
public static void Bar2() {
Cons0ole.WriteLine("Bar");'
}
}

I can call ...

Foo.Bar() (note I am using the typename)

If I were to try
Foo.Bar2() I would get an error (it is an instance member and needs to be
called from an instance such as)

Foo f = new Foo();
f.Bar2();

Does this make sense?

Cheers,

Greg Young
MVP - C#
http://codebetter.com/blogs/gregyoung
"gordon" <go**********@optusnet.com.auwrote in message
news:44***********************@news.optusnet.com.a u...
Hi

I am learning C# from books. I am trying to understand the use of the
word 'Static' on a method.

When i look in the autos area (I often like to see what values are being
resolved) i see that there are also static members and non public members.

Could you please explain to me how the static method differs from others,
how static members differ from others?

Some text and an example would be great, but a link to a novice focuseed
website would be good to.

Thanks in advance

Doug

Sep 1 '06 #2

P: n/a
Sorry I meant to make properties in the first Foo class (one hitting a
static one hitting an instance variable).

Cheers,

Greg
"Greg Young" <dr*******************@hotmail.comwrote in message
news:um**************@TK2MSFTNGP05.phx.gbl...
>A static item is bound to the type as opposed to the instance.

A great example would be ...

public class Foo {
public static int Bar;
public int Bar2;
}

Foo f = new Foo();
Foo f1 = new Foo();

f.Bar = 22;
f.Bar2 = 22;
f1.Bar = 33;
f.Bar2 = 33;

f.Bar is 33
f.Bar2 is 22
f1.Bar is 33
f1.Bar2 is 33

Sothe variable is shared amoungst all instances of the class ..

for methods they are called differently ..

public class Foo {
public static void Bar() {
Cons0ole.WriteLine("Bar");'
}
public static void Bar2() {
Cons0ole.WriteLine("Bar");'
}
}

I can call ...

Foo.Bar() (note I am using the typename)

If I were to try
Foo.Bar2() I would get an error (it is an instance member and needs to be
called from an instance such as)

Foo f = new Foo();
f.Bar2();

Does this make sense?

Cheers,

Greg Young
MVP - C#
http://codebetter.com/blogs/gregyoung
"gordon" <go**********@optusnet.com.auwrote in message
news:44***********************@news.optusnet.com.a u...
>Hi

I am learning C# from books. I am trying to understand the use of the
word 'Static' on a method.

When i look in the autos area (I often like to see what values are being
resolved) i see that there are also static members and non public
members.

Could you please explain to me how the static method differs from others,
how static members differ from others?

Some text and an example would be great, but a link to a novice focuseed
website would be good to.

Thanks in advance

Doug


Sep 1 '06 #3

P: n/a
*"gordon" <go**********@optusnet.com.auwrote in message
news:44***********************@news.optusnet.com.a u...
* Hi
*
* I am learning C# from books. I am trying to understand the use of the
word
* 'Static' on a method.
*
* When i look in the autos area (I often like to see what values are being
* resolved) i see that there are also static members and non public members.
*
* Could you please explain to me how the static method differs from others,
* how static members differ from others?
*
* Some text and an example would be great, but a link to a novice focuseed
* website would be good to.
*
* Thanks in advance
*
* Doug
*
*

Doug,

Another quick answer: With static methods, you don't have to create an
object to use its static methods.
What does this mean? Suppose we have a Math class that returns the square
root of a number.
If it were not a static method (therefore an instance method),
you'd have to create a Math object (using the new operator)
and then call the square root method using the reference to your newly
created Math object.
Something like,
Math m = new Math( ); // Create new Math object
int i = m.SquareRoot(16); // Call the SquareRoot method from our newly
created Math object, m.

now, if the method is declared static, you don't need to create an object of
the Math class.
You can just use the method, but you need to let the compiler know the
method is in the Math class.
So you put the class name followed by the dot operator (.) and then the
static method name.
Something like,
int i = Math.SquareRoot(16);

This works for constants and whatnot as well, not just methods.
Perhaps, some common Math constants like pi, e, and whatnot would be
available to you w/o
needing to create a Math object first before having access to them.
A silly example:
double myNumber = Math.Pi + Math.e ;
-MH




Sep 1 '06 #4

P: n/a
Gordon,

Another short answer.

A static member is (in Microsoft Systems) always non destructable static
placed in the main stack of a program.

A non static member has to be instanced and is placed somewhere else and can
be created as much times as you want.

In other words, using only static classes means the same as non OOP
programming (more modulair programming).

Cor

"gordon" <go**********@optusnet.com.auschreef in bericht
news:44***********************@news.optusnet.com.a u...
Hi

I am learning C# from books. I am trying to understand the use of the
word 'Static' on a method.

When i look in the autos area (I often like to see what values are being
resolved) i see that there are also static members and non public members.

Could you please explain to me how the static method differs from others,
how static members differ from others?

Some text and an example would be great, but a link to a novice focuseed
website would be good to.

Thanks in advance

Doug

Sep 2 '06 #5

P: n/a
Thanks Greg, Michael and Cor - your advice and assistance is appreciated.

Doug

"gordon" <go**********@optusnet.com.auwrote in message
news:44***********************@news.optusnet.com.a u...
Hi

I am learning C# from books. I am trying to understand the use of the
word 'Static' on a method.

When i look in the autos area (I often like to see what values are being
resolved) i see that there are also static members and non public members.

Could you please explain to me how the static method differs from others,
how static members differ from others?

Some text and an example would be great, but a link to a novice focuseed
website would be good to.

Thanks in advance

Doug

Sep 2 '06 #6

P: n/a
Cor,

See inline:
A static member is (in Microsoft Systems) always non destructable static
placed in the main stack of a program.
It is not in the stack. The instance of the static type which holds the
fields lives on the heap.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com
>
"gordon" <go**********@optusnet.com.auschreef in bericht
news:44***********************@news.optusnet.com.a u...
>Hi

I am learning C# from books. I am trying to understand the use of the
word 'Static' on a method.

When i look in the autos area (I often like to see what values are being
resolved) i see that there are also static members and non public
members.

Could you please explain to me how the static method differs from others,
how static members differ from others?

Some text and an example would be great, but a link to a novice focuseed
website would be good to.

Thanks in advance

Doug


Sep 5 '06 #7

P: n/a
Nicholas if I remember correctly it gets treated specially and lives in the
LOH since it is known to have a long lifespan.

Cheers,

Greg Young
MVP - C#
http://codebetter.com/blogs/gregyoung

"Nicholas Paldino [.NET/C# MVP]" <mv*@spam.guard.caspershouse.comwrote in
message news:u$**************@TK2MSFTNGP05.phx.gbl...
Cor,

See inline:
>A static member is (in Microsoft Systems) always non destructable static
placed in the main stack of a program.

It is not in the stack. The instance of the static type which holds
the fields lives on the heap.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com
>>
"gordon" <go**********@optusnet.com.auschreef in bericht
news:44***********************@news.optusnet.com. au...
>>Hi

I am learning C# from books. I am trying to understand the use of the
word 'Static' on a method.

When i look in the autos area (I often like to see what values are being
resolved) i see that there are also static members and non public
members.

Could you please explain to me how the static method differs from
others, how static members differ from others?

Some text and an example would be great, but a link to a novice focuseed
website would be good to.

Thanks in advance

Doug



Sep 5 '06 #8

This discussion thread is closed

Replies have been disabled for this discussion.