Infovark Underground

  • news
    • infoblog
    • underground
  • product
  • download
  • buy
  • support
  • about
    • ← REST for the Weary
    • The Address Bar as the New Command Line →

    Calling a Static Method from an Object Instance in C#

    28 Mar 2008 by Dean in .NET / 7 Comments

    The short answer: You can’t call a static method from an object instance in C#. It’s allowed in VB.NET, but in C# you must use reflection or delegates instead.

    Of the two workarounds, delegates is the preferred option. Delegates perform faster, and you get compile-time errors rather than run-time errors.

    But it’s Friday, and I’m feeling a bit lazy. Here’s the syntax for using reflection:


    typeof(<type name>).GetMethod("<method name>").Invoke(null, new object[]{<list of parameters if any>});

    If you want the long answer to why C# doesn’t allow static methods to be called from instances, you’ll need to read the posts and comments for parts one, two and three on Eric Lippert’s blog, Fabulous Adventures in Coding.

    But maybe not on a Friday. :-)

    Related posts

    1. Reflecting on reflection in .NET
    2. P/Invoke Hooray();
    3. WCF Instance Context
    4. Switch By Type in C#
    5. Using WCF to return HTML
    • Tweet

    7 Comments

    • Simon

      Those blog links seem to mainly be about calling statics on generic parameters… (rather than just a standard object instance). Which is indeed annoying, but I’d also say, generally not necessary in a normal design; you’re probably better off using some sort of singleton instance or something.

      29 Mar 2008 11:03 pm
      Reply
      • Dean

        You’re right Simon. (Did I mention I was being lazy?) The main insight I derived from reading all the articles and comments was that the “static” keyword meant more than I thought it did. In C#, “static” means that the compiler must know — at compile time — exactly which method to call. That rules out most polymorphic behavior. It also makes calling static methods from instances problematic. VB.Net doesn’t quibble here, but C# opts for compiler errors.

        06 May 2008 01:05 am
        Reply
        • Dutt

          Thanks for this. very nice one.

          can you also please provide a sample on using delegates to static methods using object.

          Thanks, Dutt

          15 Dec 2009 03:12 am
          Reply
          • nayak

            short & good explanation, but seems it is controdicting the what says in below articles. just sharing the link incase uses feel useful.

            Can static methods be called using object/instance in .NET

            http://msdotnetsupport.blogspot.com/2009/12/static-methods-calling-using-object-c.html

            16 Dec 2009 12:12 am
            Reply
            • Dean

              Dutt, there’s a good example on using delegates to call static methods in C# on MSDN. Check out Microsoft’s delegates tutorial: http://msdn.microsoft.com/en-us/library/aa288459%28VS.71%29.aspx

              18 Dec 2009 10:12 am
              Reply
              • Dean

                Nayak, the article you linked to makes the same point I was making in my comment. Using Invoke() to call a static method won’t give you the expected polymorphic behavior. It will always call the static method on the base class.

                18 Dec 2009 10:12 am
                Reply
                • Rajeev

                  How about this?
                  ddlVenue.FillValues();

                  method is defined as:

                  public static void FillValues(this DropDownList ddl)
                  {
                  ddl.DataValueField = “value”;
                  ddl.DataTextField = “test”;
                  ddl.DataBind();
                  }

                  19 Oct 2011 04:10 am (@Twitter)
                  Reply

                  Leave a Comment

                  Posting your comment...

                  Subscribe to these comments via email

                  • Categories

                    • .NET (41)
                    • AJAX (3)
                    • Books (7)
                    • HTML (9)
                    • Infovark (8)
                    • Programming (48)
                    • REST (11)
                    • SQL (3)
                    • Testing (3)
                    • Tools (13)
                    • UI (3)
                    • WCF (11)
                    • Web Services (8)
                    • WPF (4)
                    • XML (4)
                  • Archives

                  • Get future articles


                     

                  • Blogroll

                    • Ajaxian
                    • Anne Van Kesteren
                    • Brain.Save()
                    • Coding Horror
                    • Eric Sink
                    • Joel Spolsky
                    • John Resig
                    • Mark Pilgrim
                    • Raymond Chen
                    • Scott Hansleman
                    • Secret Geek
                    • Steve Yegge
                    • The Daily WTF
                    • The Database Programmer
                  • Meta

                    • Log in
                    • Entries RSS
                    • Comments RSS
                    • WordPress.org
                • Site map

                  • News
                  • Product
                  • Download
                  • Buy
                  • Support
                  • About
                • Recent Posts

                  • Review: Brownfield Application Development in .NET
                  • Using Modal Dialogs with a Splash Screen in WPF
                  • Highlighting query terms in a WPF TextBlock
                  • Getting XAML Hyperlink text to wrap
                  • How to format the XAML Hyperlink NavigateUri
                • Twitter

                  Copyright 2011 Infovark, Inc. All rights reserved.