什么是“静态方法”?在 C# 中?

发布于 2024-10-01 15:44:39 字数 169 浏览 3 评论 0原文

将 static 关键字添加到方法中意味着什么?

public static void doSomething(){
   //Well, do something!
}

您可以将 static 关键字添加到类中吗?那意味着什么?

What does it mean when you add the static keyword to a method?

public static void doSomething(){
   //Well, do something!
}

Can you add the static keyword to class? What would it mean then?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(9

橘和柠 2024-10-08 15:44:39

与常规(实例)函数不同,静态函数不与类的实例关联。

static 类是只能包含static 成员的类,因此无法实例化。

例如:

class SomeClass {
    public int InstanceMethod() { return 1; }
    public static int StaticMethod() { return 42; }
}

为了调用 InstanceMethod,您需要该类的实例:

SomeClass instance = new SomeClass();
instance.InstanceMethod();   //Fine
instance.StaticMethod();     //Won't compile

SomeClass.InstanceMethod();  //Won't compile
SomeClass.StaticMethod();    //Fine

A static function, unlike a regular (instance) function, is not associated with an instance of the class.

A static class is a class which can only contain static members, and therefore cannot be instantiated.

For example:

class SomeClass {
    public int InstanceMethod() { return 1; }
    public static int StaticMethod() { return 42; }
}

In order to call InstanceMethod, you need an instance of the class:

SomeClass instance = new SomeClass();
instance.InstanceMethod();   //Fine
instance.StaticMethod();     //Won't compile

SomeClass.InstanceMethod();  //Won't compile
SomeClass.StaticMethod();    //Fine
旧故 2024-10-08 15:44:39

从另一个角度来看:
考虑一下您想要对单个字符串进行一些更改。
例如你想把字母变成大写等等。
您为这些操作创建另一个名为“Tools”的类。
创建“Tools”类的实例是没有意义的,因为该类中没有任何类型的可用实体(与“Person”或“Teacher”类相比)。
所以
我们使用 static 关键字是为了在不创建任何实例的情况下使用“Tools”类,当您在类名(“Tools”)后面按点时,您可以访问您想要的方法。

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ConsoleApplication1
{
class Program
{
    static void Main(string[] args)
    {
        Console.WriteLine(Tools.ToUpperCase("Behnoud Sherafati"));
        Console.ReadKey();
    }
}

public static class Tools
{
    public static string ToUpperCase(string str)
    {
        return str.ToUpper();

    }
}
}

From another point of view:
Consider that you want to make some changes on a single String.
for example you want to make the letters Uppercase and so on.
you make another class named "Tools" for these actions.
there is no meaning of making instance of "Tools" class because there is not any kind of entity available inside that class (compare to "Person" or "Teacher" class).
So
we use static keyword in order to use "Tools" class without making any instance of that, and when you press dot after class name ("Tools") you can have access to the methods you want.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ConsoleApplication1
{
class Program
{
    static void Main(string[] args)
    {
        Console.WriteLine(Tools.ToUpperCase("Behnoud Sherafati"));
        Console.ReadKey();
    }
}

public static class Tools
{
    public static string ToUpperCase(string str)
    {
        return str.ToUpper();

    }
}
}
风吹雨成花 2024-10-08 15:44:39

即使尚未创建类的实例,也可以在类上调用静态方法、字段、属性或事件。如果创建了该类的任何实例,则它们不能用于访问静态成员。静态字段和事件只存在一份副本,静态方法和属性只能访问静态字段和静态事件。静态成员通常用于表示不随对象状态而改变的数据或计算;例如,数学库可能包含用于计算正弦和余弦的静态方法。
静态类成员在成员的返回类型之前使用 static 关键字声明

A static method, field, property, or event is callable on a class even when no instance of the class has been created. If any instances of the class are created, they cannot be used to access the static member. Only one copy of static fields and events exists, and static methods and properties can only access static fields and static events. Static members are often used to represent data or calculations that do not change in response to object state; for instance, a math library might contain static methods for calculating sine and cosine.
Static class members are declared using the static keyword before the return type of the member

断念 2024-10-08 15:44:39

静态函数意味着它与类相关联(不是类的特定实例,而是类本身),即使不存在类实例也可以调用它。

静态类意味着该类仅包含静态成员。

Static function means that it is associated with class (not a particular instance of class but the class itself) and it can be invoked even when no class instances exist.

Static class means that class contains only static members.

野心澎湃 2024-10-08 15:44:39

很快您就无法实例化静态类:
例如:

static class myStaticClass
{
    public static void someFunction()
    { /* */ }
}

你不能这样制作:

myStaticClass msc = new myStaticClass();  // it will cause an error

你只能制作:

myStaticClass.someFunction();

Shortly you can not instantiate the static class:
Ex:

static class myStaticClass
{
    public static void someFunction()
    { /* */ }
}

You can not make like this:

myStaticClass msc = new myStaticClass();  // it will cause an error

You can make only:

myStaticClass.someFunction();
尴尬癌患者 2024-10-08 15:44:39

当您向方法添加“static”关键字时,这意味着底层实现为该类的任何实例提供相同的结果。
不用说,结果会随着参数值的变化而变化

When you add a "static" keyword to a method, it means that underlying implementation gives the same result for any instance of the class.
Needless to say, result varies with change in the value of parameters

梦境 2024-10-08 15:44:39

static 关键字的核心,您在 RAM 中只会拥有该关键字的一份副本(方法 /variable /class )
所有通话共享

Core of the static keyword that you will have only one copy at RAM of this (method /variable /class )
that's shared for all calling

草莓酥 2024-10-08 15:44:39

静态变量不与类的对象链接。可以使用类名来访问它。类的所有对象将共享静态变量。

通过将函数设置为静态,它将限制该文件内该函数的访问。

Static variable doesn't link with object of the class. It can be accessed using classname. All object of the class will share static variable.

By making function as static, It will restrict the access of that function within that file.

夏末 2024-10-08 15:44:39

static 关键字应用于类时,告诉编译器创建该类的单个实例。这样就不可能“新建”该类的一个或多个实例。静态类中的所有方法本身都必须声明为静态。

拥有非静态类的静态方法是可能的,而且通常是可取的。例如,在创建另一个类的实例时,工厂方法通常被声明为静态,因为这意味着不需要包含因子方法的类的特定实例。

有关方式、时间和地点的详细说明,请参阅

The static keyword, when applied to a class, tells the compiler to create a single instance of that class. It is not then possible to 'new' one or more instance of the class. All methods in a static class must themselves be declared static.

It is possible, And often desirable, to have static methods of a non-static class. For example a factory method when creates an instance of another class is often declared static as this means that a particular instance of the class containing the factor method is not required.

For a good explanation of how, when and where see MSDN

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文