什么是公共静态,公共和静态方法之间的区别? [英] What is the difference between public static, public and static method?
问题描述
我有在C#方法的范围,并在ASP.Net最好的情况下使用了几个相关问题:
- 在 C#,之间有什么区别:
- 在
公共静态
法 - 在
公共
法 - 在
静态
法
- 在
- 我使用的 MVC 和网络服务;在我的问题#1的方法范围而言,这将是在内存占用为每个
方法范围类型,例如,威尔
静态
解除该功能的内存?
公共
按本身就意味着这是一个基于实例的成员对外部呼叫者访问(那些访问类型本身)。
静态
按本身就意味着该成员不是基于实例:你可以调用它,而不需要任何特定实例(或全偶的实例);没有无障碍预选赛中,假定非公有制 - 这样的成员将不会对外部呼叫者访问
公共静态
是静态
方法是的访问外部呼叫者。
内存使用情况在两种情况下相同的:在方法声明的任何变量的作用域的本身(方法调用作为一个实现细节:通过堆栈;也:我假设没有捕获变量,并没有异步
或收益
使用),
在此没有什么是特定于ASP.NET / MVC。然而,行动上的控制器的方法是,IIRC,预计公共/实例,因此与的的公共
修改和无的的静态
修改
基本上是:
辅助功能:
- 没有指定,默认为私有(或内外班)
- 私:只适用于code该类型在
- 保护:适用于code类型或子类型 里面
- 内部:适用于code在同一个装配
- 受保护的内部:无论是受保护或(工会)的内部
- 公:可用于访问所有呼叫者的类型
静态的/ etc:
- 无指定:基于实例的;一个实例是必需的,code具有自动访问实例成员(通过
这一点。
)和静态
成员 - 静态:无需实例; code具有
静态自动访问
成员只有
I have a few related questions about method scope in C#, and best case usage in ASP.Net:
- In C#, what is the difference between:
- a
public static
method - a
public
method - a
static
method
- a
- 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., Willstatic
release the function memory after it is used?
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.
) andstatic
members - "static": no instance is required; code has automatic access to
static
members only
这篇关于什么是公共静态,公共和静态方法之间的区别?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!