Access static method from non static class possible in java and not in c#

Posted by sagar_kool on Stack Overflow See other posts from Stack Overflow or by sagar_kool
Published on 2012-09-15T09:24:58Z Indexed on 2012/09/15 9:37 UTC
Read the original article Hit count: 539

Access static method from non static class with object. It is not possible in C#. Where it is done by JAVA. How it works?

example of java

    /**
   * Access static member of the class through object.
   */
   import java.io.*;

   class StaticMemberClass {
  // Declare a static method.
  public static void staticDisplay() {
   System.out.println("This is static method.");
   }
   // Declare a non static method.
   public void nonStaticDisplay() {
   System.out.println("This is non static method.");
   }
  }

   class StaticAccessByObject {

   public static void main(String[] args) throws IOException {
    // call a static member only by class name.
   StaticMemberClass.staticDisplay();
   // Create object of StaticMemberClass class.
   StaticMemberClass obj = new StaticMemberClass();
   // call a static member only by object.
   obj.staticDisplay();
   // accessing non static method through object.
   obj.nonStaticDisplay();
  }
   }
   Output of the program:
    This is static method.
    This is static method.
    This is non static method.

How to do this in C#? thanks in advance..

© Stack Overflow or respective owner

Related posts about c#

Related posts about static