What is the difference between public static, publ

2019-04-14 22:30发布

问题:

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 method
    • a public method
    • a static method
  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 static release the function memory after it is used?

回答1:

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.

Basically:

Accessibility:

  • 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


回答2:

Your static method with no access specifier will be private. You can't access it outside the class.

Consider the following class.

class TestClass
{
    public int MyProperty { get; set; }
    static void SomeStaticMethod()
    {
    }

    public static void SomeOtherStaticMethod()
    {
        SomeStaticMethod(); // You can use the static method inside
    }

    public void InstanceMethod()
    {
        SomeStaticMethod();
    }
}

when you are using it:

TestClass tc = new TestClass();
tc.InstanceMethod();
TestClass.SomeOtherStaticMethod();
TestClass.SomeStaticMethod(); // Thats an error because SomeStaticMethod is private and not accessible


回答3:

Class members are private by default, so if you do not specify that your static method is plublic you won't be able to access your method from outside your class.

For more information about access modifiers see: Access Modifiers (C# Programming Guide)



回答4:

It's all very well of people to provide you examples, but these things are well documented already on the Internet, and a very simple search can yield definitive results. Let me indulge you, finding the MSDN references on the topics (two topics, by the way, member access and non-instance members are not strictly related):

Firstly you have access modifiers, specifically public in this case:

public

The type or member can be accessed by any other code in the same assembly or another assembly that references it.

This is the case whether the member is static or not, that's irrelevant.

Then you have static, non-instance stuff:

static

Static classes and class members are used to create data and functions that can be accessed without creating an instance of the class. Static class members can be used to separate data and behavior that is independent of any object identity

So, any differences are a combination of possible access and 'instance' rules.

Memory management is a different thing; no one method, property, field, regardless of access and context, is going to magically reduce memory, that's something you as a developer must consider with each line of code (with the help of the built-in memory management of the CLR when coding appropriately).



回答5:

public is an access modifier. so wherever it is applied it refers to the scope.



回答6:

static Foo

is not public, which means it is not visible outside of the class.