What's a “static method” in C#?

前端 未结 9 2042
再見小時候
再見小時候 2020-11-22 06:48

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

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

Can you add the

相关标签:
9条回答
  • 2020-11-22 06:59

    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.

    0 讨论(0)
  • 2020-11-22 07:06

    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();
    
        }
    }
    }
    
    0 讨论(0)
  • 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.

    0 讨论(0)
  • 2020-11-22 07:18

    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
    
    0 讨论(0)
  • 2020-11-22 07:18

    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 membe

    0 讨论(0)
  • 2020-11-22 07:18

    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();
    
    0 讨论(0)
提交回复
热议问题