什么是公共静态,公共和静态方法之间的区别? [英] What is the difference between public static, public and static method?

查看:381
本文介绍了什么是公共静态,公共和静态方法之间的区别?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有在C#方法的范围,并在ASP.Net最好的情况下使用了几个相关问题:


  1. C#,之间有什么区别:

    • 公共静态

    • 公共

    • 静态


  2. 我使用的 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:

  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?

解决方案

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

这篇关于什么是公共静态,公共和静态方法之间的区别?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆