I know this has been asked before, but I can't find any simple answers for a noob like me in C#. What exactly is a static method. I mean what does it mean to be static vs. nonstatic.

I know in the code with the main method we use

public static void Main(string []args) what does the static change about the main method that distinguishes it from the rest in regards to it being static.

  • Thanks

The method belongs to the class and not any single instance of the class.

I can understand when you say the method belongs to the class, but can you explain when you say and not any single instance of the class

A static method is accessed through the class itself. Non-static through an instance of the class

public class Junk {
    public static String Name() {
        return "My name is Junk";
    }

    public String OtherName() {
        return "I'm not static!";
    }
}

In this sample, we can call the static method (and have to do so) through the class, i.e. Junk.Name();. The other method has to be called from an instance of the class, so we have to create one then we can call it:

Junk j = new Junk();
j.OtherName();

As an example, let's say you want to count the number of times people create an instance of your class. We do this by creating a static counter (belongs to the class, not an instance).

public class MyCountingClass() {
    private int count = 0;

    private static void AddOneToCount() {
        count++;
    }

    public static int Count {
        get {
            return count;
        }
    }

    public MyCountingClass() {
        AddOneToCount();
    }

    public void OtherMethods() {
    }
}

Static items are loaded in at run time. Non-dynamic. If you have a piece of code that's reused a lot and doesn't really need to store data between uses Static can work nicely for that.

Just remember, it's initialized at run time, unlike, a variable you initialize (that's why you can call it straight up)

as per my understanding static methods are faster than instance methods . If you have a class , you can create any number of objects or instances . But for all the objects some times there may be some common data , which you must declare it as a static data using static variable , for manipulating any static thing use static method .

Kalothna, I don't believe you can declare static variables within a non static class/function. If you want items to be constant you would use the "const" piece of code when declaring the value.

That's how I understand it.

However, yes Static methods can be faster, as they are already loaded into memory. There is no need to allocate space during run time for them as it was already done before hand (however, I can't remember how say a resized array would be handled here, if it's tacted onto the end of the variable part of the stack, or if the static portion is expanded)

a non-static class can have static variables and methods. Take a look at the Complex class as it has many static methods and is not a static class.

.. DOH! I just realized, I did this exact thing in my QRCode generator program. Creating static functions within a non-static class. Thanks for reminding me Momerath

Thanks so much guys :)

In general, dynamic means energetic, fit for activity and/or change, or strong, while static means stationary or settled. In machine wording, element generally implies equipped for activity and/or change, while static means altered. Both terms can be connected to various distinctive sorts of things, for example, programming dialects

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.