Wiki Home

Static Methods


Namespace: DotNet
I attended Kevin McNeish's C#/VB.Net session at WhilFest2002 ("FoxPro is Magic" was the theme). Today I'm looking through his book and in his hello world program I see:

using system;

class MyFirstCSharpProgram
{
   public static int Main()
   {
      Console.WriteLine("Hello .NET World!");
      return 0;
   }
}


I hear everything in C# is a class, but hey, 'Console' isn't created as a new instance of some class in this hello world program. What's going on here? I know the 'using' directive makes it available, but who created the instance used in this program? It's not magic. Joe Kuhn
Answer:

Console is a class that has a static member named WriteLine(). A static property or method means an instance doesn't have to be available, and can kind of be seen as public variables and UDFs. They just can't access any non-static data (properties and fields). -- Mike Helland

This is the part that I love the best about pure object-orientation. People trying to impress you will tell you "every thing needs to be a class in C# or Java". But then, as Mike pointed out, for all practical purposes a static method is pretty much like a UDF stored in a PRG in VFP. As Doug Henning said once, long live PRGs :) -- Hector Correa

Well, its closer to a method on an object that is global in scope (you get the benefit of properties and fields with static methods), but I mostly agree. I honestly don't see how it would be practical to build a large system without static members. Mike Helland

The console class must be static as well. Joe Kuhn

I could be wrong, but I don't think that the class can be static. It can only implement static members. http://www.c-sharpcorner.com/Code/2002/Sept/StaticConstructors.asp -- Mike Helland

I'm left with the question: Who creates 'console', an instance of something, for the program to use? The language must. Joe Kuhn

The language? Perhaps. Or some sort of library. Like MessageBoxA() in C++ - not defined by your code, but also not defined by the language. -- Peter Crabtree

Comment:

I started investigating the keyword 'static' myself. Whoa. You've got to understand so much of the language before you can understand some simple hello world program, it's amazing. Students of C based languages ignore quite a bit in the early learning stages. Later, the stuff you've been ignoring becomes important. This is what makes learning these languages so difficult, in my opinion. Joe Kuhn
I hope this clarifies things a little bit: "A static member belongs to the class rather than to the objects of the class." http://www.csharphelp.com/archives/archive148.html In other words, even if you have an instance of class console, let say oMyConsole, you cannot call oMyConsole.WriteLine() because the static method belongs to the class not the oMyConsole instance. Therefore, you must call Console.WriteLine() -- Hector Correa

Sorry Hector. My brain isn't cooperating today. I care about the class, not the member. How is there an instance of 'console' called 'console' for me to use? Joe Kuhn

There is no instance. You just use the class name. For example, say you got something like this:

class MyFunkyClassName{

static void MyStaticMethod(){}

}

Now, anywhere in your code, you can call MyStaticMethod() just by doing this:

MyFunkyClassName.MyStaticMethod();

Theres no instance. As long as the class is available (IOW, you are using its namespace), you can just address the class directly and call its static members. -- Mike Helland

Weird. I'd like to know how that's done internally. Joe Kuhn

Look at the IL output. Its probably very similar to how its done on an instance. First the compiler resolves the name of the class (whether it has to go through an instance or not doesn't seem important) and then it executes the method.

A little more info, I have a class that looks like:

	public class Class1
	{
		public void Method1()
		{
		}

		public static void Method2()
		{
		}	
	}


In a Windows Form, I call:

			Class1 c = new Class1();
			c.Method1();
			Class1.Method2();

Next, I compile the app, and then fire up the .NET Command Prompt, type in ILDasm and load my EXE. In the method where the above three lines are, the IL is:

  IL_0000:  newobj     instance void RealStaticTest.Class1::.ctor()
  IL_0005:  stloc.0
  IL_0006:  ldloc.0
  IL_0007:  callvirt   instance void RealStaticTest.Class1::Method1()
  IL_000c:  call       void RealStaticTest.Class1::Method2()


This is how it works internally (the last two lines show the difference between an instance call and a static, note that it looks like its calling a static in both cases. Also note, that I am not an IL expert yet :-) -- Mike Helland

Cool. Notice the difference in the last two lines:
callvirt   instance
call

Wonder what a virtual instance is. Joe Kuhn

"Call a virtual method on an object instance." -- Mike Helland

Oh. Virtual method. Ok. And how's that different from 'call'? Joe Kuhn

call = "Call a method" and callvirt = "Call a method on an object instance". -- Mike Helland
Category DotNet Category CSharp
( Topic last updated: 2007.05.24 09:24:26 AM )