C# 公共静态,公共和静态方法之间有什么区别?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/14212437/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-10 11:04:34  来源:igfitidea点击:

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

c#asp.net-mvc-3

提问by Dhwani

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

我有一些关于 C# 中方法范围的相关问题,以及 ASP.Net 中的最佳案例用法:

  1. In C#, what is the difference between:
    • a public staticmethod
    • a publicmethod
    • a staticmethod
  2. I am using MVCand 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 scopetype, e.g., Will staticrelease the function memory after it is used?
  1. C# 中,有什么区别:
    • 一种public static方法
    • 一种public方法
    • 一种static方法
  2. 我正在使用MVC网络服务;就我的问题#1 中的方法范围而言,每种method scope类型的内存占用情况会产生什么差异,例如,使用后会static释放函数内存吗?

采纳答案by Marc Gravell

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

public本身意味着这是一个基于实例的成员,可以被外部调用者(可以访问类型本身的人)访问。

staticby 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.

static本身意味着该成员不是基于实例的:您可以在不需要任何特定实例(甚至根本不需要任何实例)的情况下调用它;如果没有可访问性限定符,则假定为非公开 - 因此外部调用者将无法访问该成员。

public staticis a staticmethod that isaccessible to external callers.

public staticstatic该方法向外部呼叫者访问。

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 asyncor yieldusage),

在这两种情况下内存使用是相同的:在方法中声明的任何变量都限定为方法调用本身(作为一个实现细节:通过堆栈;另外:我假设没有“捕获的变量”,也没有使用asyncyield使用),

Nothing in this is specific to ASP.NET / MVC. However, "action" methods on controllers are, IIRC, expected to be public / instance, so withthe publicmodifier, and withoutthe staticmodifier.

这里面没有什么是特定于 ASP.NET / MVC 的。然而,在控制器的“行动”的方法是,IIRC,有望成为公/实例,因此public修改,并没有static修改。

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
  • 未指定:默认为“私有”(或外部类的“内部”)
  • “private”:仅可用于该类型内的代码
  • “protected”:可用于该类型或子类型内的代码
  • “内部”:可用于同一程序集中的代码
  • “受保护的内部”:“受保护”(联合)“内部”
  • “public”:所有调用者都可以访问该类型

Static / etc:

静态/等:

  • none specified: instance-based; an instance is required, and code has automatic access to instance-members (via this.) and staticmembers
  • "static": no instance is required; code has automatic access to staticmembers only
  • 未指定:基于实例;需要一个实例,并且代码可以自动访问实例成员(通过this.)和static成员
  • “静态”:不需要实例;代码static只能自动访问成员

回答by Habib

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

回答by scartag

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

public是访问修饰符。所以无论它在哪里应用,它都是指范围。

回答by Wiktor Zychla

static Foo

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

不是公开的,这意味着它在班级之外是不可见的。

回答by polkduran

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)

有关访问修饰符的更多信息,请参阅: 访问修饰符(C# 编程指南)

回答by Grant Thomas

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):

人们为您提供示例非常好,但是这些事情已经在 Internet 上得到了很好的记录,并且非常简单的搜索可以产生明确的结果。让我放纵一下,找到有关主题的 MSDN 参考(两个主题,顺便说一下,成员访问和非实例成员没有严格的关系):

Firstly you have access modifiers, specifically publicin this case:

首先你有访问修饰符,特别是public在这种情况下:

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 staticor not, that's irrelevant.

无论是否为成员,情况都是static如此,这无关紧要。

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).

内存管理是另一回事;没有一种方法、属性、字段,无论访问和上下文如何,都会神奇地减少内存,这是您作为开发人员必须在每一行代码中考虑的事情(在编码时借助 CLR 的内置内存管理)适当)。