0

I am pretty new to C# programming, and I am still learning. I was wondering if there is any particular reason to not use the static modifier when creating a method, for me it just seem much simpler. I tried looking at MSDN but that isn't much help if I don't know what half the words mean.

For example this:

using System;

namespace StaticModifier
{
    class Program
    {
        static void Main(string[] args)
        {
            SayHi();
        }

        static void SayHi()
        {
            Console.WriteLine("Hi");
        }
    }
}

Seems much simpler than this:

using System;

namespace StaticModifier
{
    class Program
    {
        static void Main(string[] args)
        {
            Program p = new Program();
            p.SayHi();
        }

        void SayHi()
        {
            Console.WriteLine("Hi");
        }
    }
}

So would anyone be willing to explain? Keep in mind that I am pretty new so please keep it simple :)

Sofus Øvretveit
  • 323
  • 1
  • 3
  • 10
  • Note that I already tried looking around at stackoverflow, but most responses were to advanced for me. – Sofus Øvretveit Aug 15 '15 at 21:36
  • 3
    Read up a bit on object oriented programming, classes and class instances - the reason will become apparent soon enough. – Chris Aug 15 '15 at 21:38
  • You can use it as often as you want to; I do. Just don't force yourself into twisting the natural logic! Your example quite obviously doesn't make any sense at all.. – TaW Aug 15 '15 at 21:38
  • Static methods cannot access properties of an instance, they can only access static properties. – dmeglio Aug 15 '15 at 21:39
  • possible duplicate of [Class with single method -- best approach?](http://stackoverflow.com/questions/205689/class-with-single-method-best-approach) – NightOwl888 Aug 15 '15 at 23:34
  • Okay guys, this got a bit too complicated for me, haha. I will continue to learn and maybe one day I will look back at this and facepalm at how little I actually understood. Thanks for taking your time to answer anyway, and i hope this may still help someone else! – Sofus Øvretveit Aug 16 '15 at 00:02

2 Answers2

1

A non-static method of the class Program provides the current instance of the class Program..

Since Program doesn't contain any variables or properties that are used, you don't need to molest your CPU with unnecessary objects.

And Program p = new Program(); is not the way you would instantiate your "Program". The Main() method is static and is usually contained within a static class with static methods. You can't call dynamic methods from a static method, since there is no instance of the object.

TL;DR

If you want to pass this (the current object) to the method and your method utilizes its properties, then don't use static. Otherwise there is absolutely no reason not to use static!

bytecode77
  • 14,163
  • 30
  • 110
  • 141
0

You wouldn't use the static modifier if you had a class that would require more than 1 instance of itself. For example if you have a class named car that included the static keyword you would only ever be access the members of that class via the static reference of that class. So you could never have 2 instances of a car object.

Which depending on what your program is trying to do maybe appropriate. However you may look to use the static keyword if you had a class names player score. As this would only ever exist once, and unless you have more that one player you would want 1 instance of the player score. Which means you could either consider a singleton instance or just have a static class.

There are many many reasons why you either would or wouldn't make use of the static modifier. However to some up when you should use it is impossible and depends on the specific scenario of the application you are creating.

Luke Stoward
  • 1,480
  • 14
  • 24