Dhwani Dhwani - 1 year ago 85
C# Question

What is the difference between public static, public and static method?

I have a few related questions about method scope in C#, and best case usage in ASP.Net:

  1. In C#, what is the difference between:

    • a
      public static

    • a

    • a

  2. I am using MVC and web-services; in terms of method scope in my question #1, what would be the resulting difference in the case of memory occupancy for each
    method scope
    type, e.g., Will
    release the function memory after it is used?

Answer Source

public by itself means this is an instance-based member that is accessible to external callers (those with access to the type itself).

static by itself means the member is not instance-based: you can call it without needing any particular instance (or even any instance at all); without an accessibility qualifier, non-public is assumed - so the member will not be accessible to external callers.

public static is a static method that is accessible to external callers.

Memory usage is identical in both cases: any variables declared in the method are scoped to the method-call itself (as an implementation detail: via the stack; also: I'm assuming no "captured variables", and no async or yield usage),

Nothing in this is specific to ASP.NET / MVC. However, "action" methods on controllers are, IIRC, expected to be public / instance, so with the public modifier, and without the static modifier.



  • none specified: defaults to "private" (or "internal" for outer-classes)
  • "private": only available to code inside that type
  • "protected": available to code inside that type or sub-types
  • "internal": available to code in the same assembly
  • "protected internal": either "protected" or (union) "internal"
  • "public": available to all callers with access to the type

Static / etc:

  • none specified: instance-based; an instance is required, and code has automatic access to instance-members (via this.) and static members
  • "static": no instance is required; code has automatic access to static members only
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download