在什么情况下我应该使用静态功能? [英] In What Situation I Should Use Static Function ?

查看:90
本文介绍了在什么情况下我应该使用静态功能?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在什么情况下我应该使用静态功能?

我理解公共静态和公共功能之间的区别。

它是

声明类属性或方法作为静态使它们可以访问而无需实例化类。声明为static的属性无法使用实例化的类对象访问(虽然静态方法可以)。

但我应该使用静态?

我可以访问一个函数无论是静态还是公共静态。

那么将它声明为公共静态是什么意思?

IN what situation i should use static function ?
I understood the difference between public static and public functions.
it is
Declaring class properties or methods as static makes them accessible without needing an instantiation of the class. A property declared as static can not be accessed with an instantiated class object (though a static method can).
but where i should use static?
I can access a function irrespective to whether it is static or public static.
Then what is the point of declaring it as public static ?

推荐答案

static mymethod()编译为 internal ,这意味着你只能在包含它的DLL中访问它。



public static mymethod()对所有引用它的DLL都是公开的。



所以,如果你的一般规则是定义 internal ,除非你知道你将在其他项目中使用它。



至于何时使用静态方法通常当方法不改变类中某些东西的状态使用时包含类的状态,或者用于传递param的计算函数结束并获得结果。
static mymethod() is compiled as internal which means you can only access it within the DLL which contains it.

public static mymethod() is public to all DLL's which reference it.

So if you the general rule is define things internal unless you know you will be using it in other projects.

As for when to use static methods generally when the method does not change state of something in the class or uses state of the containing class, or for computation functions where you pass parameters and get a result.


检查我的文章中的静态类和方法详细信息。

基本C#OOP概念 [ ^ ]



现在让我们关注你的问题

所以你问的是静态方法和公共静态方法之间的区别



如果你知道我们将类或静态或方法声明为静态,我们需要为对象创建对象访问那些方法。

我们可以直接访问静态方法在那里使用类

示例Yourclass.YourStaticMethod();



如果你默认只声明静态方法它将是私有的,所以外部类不能访问该方法。

如果你声明静态方法为公共外部类可以访问该方法。



请参阅下面的示例控制台程序,了解静态方法和公共静态方法。



Check my Article for Static Class and Method Details.
Basic C# OOP Concept[^]

Now lets focus on your question
So you are asking the differance between static method and Public Static Method

As you know if we declare class or static or method as static we need to create object for to access that methods.
We can directly access the static method usign there class
example Yourclass.YourStaticMethod();

if you declare only static method by default it will be as private so outside class cannot access that method.
If you declare the static method as public outside class can access that method.

see the sample console program below for static method and public static method.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace ConsoleApplication1
{
    public class YourstaticClass
    {
        static void welcomewithprivate()
        {
            Console.WriteLine("iam in Private Static Clss Method");
        }
        public static void WelcomewithPublic()
        {
            Console.WriteLine("iam in Private Static Clss Method");
        }
    }
    class Program
    {
       
        static void Main(string[] args)
        {
            //This line will be as error due to private static method can not be accessed in another class
            //YourstaticClass.welcomewithprivate(); 
            // Public Static method
            YourstaticClass.WelcomewithPublic();
            

        }
    }
}


Quote:

声明为static的属性无法使用实例化的类对象访问

A property declared as static can not be accessed with an instantiated class object

这不是真的,可以从任何地方访问静态属性。相反,静态方法不能直接访问实例属性。



That's not true, a static property can be accessed from everywhere. It is true the opposite, a static method cannot directly access instance properties.

引用:

但是我应该使用static?

but where i should use static?

当实现的功能不依赖于特定的实例状态时,你应该使用静态方法。

You should use static method when the implemented functionality does not depend on particular instance state.


这篇关于在什么情况下我应该使用静态功能?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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