IT story

C #의 "정적 방법"은 무엇입니까?

hot-time 2020. 5. 24. 10:52
반응형

C #의 "정적 방법"은 무엇입니까?


정적 키워드를 메소드에 추가 할 때의 의미는 무엇입니까?

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

static수업에 키워드를 추가 할 수 있습니까 ? 그러면 무슨 의미입니까?


static함수는 정규 (달리 인스턴스 ) 함수 클래스의 인스턴스와 관련된 것은 아니다.

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

다른 관점에서 : 단일 문자열을 일부 변경하려고합니다. 예를 들어 글자를 대문자 등으로 만들려고합니다. 이러한 작업을 위해 "도구"라는 다른 클래스를 만듭니다. 해당 클래스 내에 사용 가능한 엔티티 유형이 없기 때문에 "도구"클래스의 인스턴스를 작성한다는 의미는 없습니다 ( "개인"또는 "교사"클래스와 비교). 따라서 인스턴스를 만들지 않고 "도구"클래스를 사용하기 위해 정적 키워드를 사용하며 클래스 이름 뒤에 점을 누르면 ( "도구") 원하는 메소드에 액세스 할 수 있습니다.

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();

    }
}
}

클래스의 인스턴스가 생성되지 않은 경우에도 클래스에서 정적 메서드, 필드, 속성 또는 이벤트를 호출 할 수 있습니다. 클래스의 인스턴스가 작성되면 정적 멤버에 액세스하는 데 사용할 수 없습니다. 정적 필드 및 이벤트의 사본은 하나만 존재하며 정적 메소드 및 특성은 정적 필드 및 정적 이벤트에만 액세스 할 수 있습니다. 정적 멤버는 종종 객체 상태에 따라 변하지 않는 데이터 또는 계산을 나타내는 데 사용됩니다. 예를 들어, 수학 라이브러리에는 사인 및 코사인을 계산하기위한 정적 메서드가 포함될 수 있습니다. 정적 클래스 멤버는 membe의 리턴 유형 전에 static 키워드를 사용하여 선언됩니다.


정적 함수는 클래스 (클래스의 특정 인스턴스가 아니라 클래스 자체)와 연관되며 클래스 인스턴스가없는 경우에도 호출 할 수 있음을 의미합니다.

Static class means that class contains only static members.


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();

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


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.


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


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

참고URL : https://stackoverflow.com/questions/4124102/whats-a-static-method-in-c

반응형