Static methods

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

Hi,

How is a static method different from a normal method (apart from the
obvious -- ie. you don't need to initialise the class to call the method).

thank you,

coderazor
 
That's about it really. There isn't a difference beyond that (and what
it can see on the type, it can't see instance fields, only static fields).

Hope this helps.
 
Hi,

Well the class DOES get initialized before calling your static method, if
the class has a static constructor it gets called as well as any static
variable that is declared with an initial value.

What you meant is that you do not need to create an instance of the class.

Basically a static method is a method that do not depend of any instance
member. They are accesible from the type and not fomr an instance.


cheers,
 
<"Ignacio Machin \( .NET/ C# MVP \)" <ignacio.machin AT
dot.state.fl.us> said:
Well the class DOES get initialized before calling your static method, if
the class has a static constructor it gets called as well as any static
variable that is declared with an initial value.

Just to be incredibly pedantic, the class *may* not be initialized if
you don't have a static constructor and if the static method doesn't
touch any of the static fields. For instance:

public class Test
{
static string x = Foo("Hello");

public static string Foo (string y)
{
Console.WriteLine (y);
return;
}
}

Calling Test.Foo("Hi")

doesn't *guarantee* that Hello will be printed as well as Hi.

In practice on the .NET CLR it does actually get printed, but the class
is only guaranteed to have its type initializer run at some point
before Test.x is first touched. This is a bit of an odd area...

See http://www.pobox.com/~skeet/csharp/beforefieldinit.html

for more information. (And yes, it's almost certainly irrelevant in
this case. I just like bringing it up 'cos it's odd :)
 
====================================================

because Static methods can only access static data and it describes
information for all objects of a class

and as i know that static method is called on the class, not the object.

ihope this little inforamtion i know was useful
thanx
 
Back
Top