Using Static methods ... when to?
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 5 of 5

Thread: Using Static methods ... when to?

  1. #1
    Join Date
    Dec 2009
    Posts
    7

    Question Using Static methods ... when to?

    Hi, I've been reading up on methods in C# and know about the different kinds. Instantiated can only be called if you create an object of the class, where-as with static methods you don't need to create an object (correct me if I'm wrong here, I'm new to all this)

    My question is, why and when would you use one or the other? What advantages are static methods over instance methods (and visa-versa)? Speed? Memory usage? Things you can and can't do with them? In all the tutorials I've read, they explain the differences but not WHY there are differences and their plus/minus points.

    Thanks for any pointers.

    JB

  2. #2
    Join Date
    Jun 2008
    Posts
    2,477

    Re: Using Static methods ... when to?

    When you have a method that does not rely on the internal state of an object. For example, consider the Graphics.FromImage method. It takes an image and returns a Graphics object for "drawing" onto said image. You don't need to have an object of type Graphics to use this and it wouldn't even make sense to do so.

    On the other hand, consider the String.Length method. A static method here would be impossible; you need to have a string object before you can find its length. The length of a string is dependent upon the state of the string object, and thus cannot be a static method.

  3. #3
    Join Date
    Jan 2002
    Location
    Scaro, UK
    Posts
    5,940

    Re: Using Static methods ... when to?

    Statics are used when you don't need an instance of a class or cannot add a member function to an existing class e.g.

    Code:
    static class Example
    {
        static public int Add(int a, int b)
        {
            return a + b;
        }
    }
    The other use for statics is for extending the functionality of a class, otherwise you could potentially end up with hundreds of different methods on the class which is widely regarded as bad practice.

    Take string for instance, and string.IsNullOrEmpty which is a static method.

    You could, potentially, have IsNullOrEmpty as a method on the string class but the same is true of a lot of functionality - even regular expressions !

    string would then grow to be an enormous class with much too much responsibility - hence the use of static 'helper' functions.

    C# 3.5 has extension methods which are just neat ways of defining static methods e.g.

    Code:
    class MyClass
    {
        public int Value { get; set; }
    }
    
    static class MyClassExtensions
    {
        // note 'this'
        static public int AddToMyClass(this MyClass myClass)
        {
            return myClass.Value + 10;
        }
    }
    
    // to use, the extension method appears as an instance method of 
    // MyClass, but in fact is still just a static method call
    MyClass myClass = new MyClass() { Value = 100 };
    
    // this will be turned into a normal call to the static method, but in C#
    // it can be called as if it were an instance method
    Console.WriteLine(myClass.AddToMyClass());
    There are plenty of reasons to use instance methods : virtual functions, interface implementations etc.

    However if your method doesn't implement a method in an interface and isn't virtual then I think you should consider using a static method instead - if you can safely expose the internal data of the class in question (via properties).

    But as with all object oriented abilities, it really does depend on context. There is no hard or fast rule.

    Darwen.
    Last edited by darwen; December 21st, 2009 at 05:27 PM.
    www.pinvoker.com - PInvoker - the .NET PInvoke Interface Exporter for C++ Dlls.

  4. #4
    Join Date
    Jun 2001
    Location
    Melbourne/Aus (C# .Net 4.0)
    Posts
    686

    Re: Using Static methods ... when to?

    Consider declaring a class as static, if there is only going to be one 'instance' of it at runtime.
    Code:
    public static class DbAccess
    {
        public static void ListEmployees(EmployeeDataSet ds)
        {
            ...
        }
    
        public static void RemoveEmployee(Guid id)
        {
            ...
        }
    
        ...
    }
    
    ...
    DbAccess.RemoveEmployee(bossesId);
    
    EmployeeDataSet ds = new EmployeeDataSet();
    DbAccess.ListEmployees(ds);
    ...
    Rob
    -
    Ohhhhh.... Old McDonald was dyslexic, E O I O EEEEEEEEEE.......

  5. #5
    Join Date
    Dec 2009
    Posts
    7

    Re: Using Static methods ... when to?

    Thanks peeps, I think I get the reasoning. I know you must think these are daft questions but this one was bugging me so I had to ask.

    Thanks again.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  


Windows Mobile Development Center


Click Here to Expand Forum to Full Width

This is a CodeGuru survey question.


Featured


HTML5 Development Center