novice advice on static use

G

gordon

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
 
G

Greg Young

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
 
G

Greg Young

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

Cheers,

Greg
 
M

Michael

* 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
 
C

Cor Ligthert [MVP]

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
 
N

Nicholas Paldino [.NET/C# MVP]

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.
 

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