In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-31 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)05/31 Report--
This article mainly introduces "how to use the static method of C#". In the daily operation, I believe that many people have doubts about how to use the static method of C#. The editor consulted all kinds of materials and sorted out simple and easy-to-use operation methods. I hope it will be helpful to answer the doubt of "how to use the static method of C#"! Next, please follow the editor to study!
I. brief introduction
Methods that use the static modifier are static, and vice versa, non-static. A static method is a special member method that does not belong to a specific instance of the class, but to the class itself. Therefore, for static methods, it is not necessary to create an instance of the class first, but to use the class name. The format of the static method. In addition, there are the following differences between static and non-static methods:
1. Static methods can be called without class instantiation, whereas non-static methods can only be called after instantiation.
two。 Static methods can only access static members and methods, and non-static methods can access them.
3. Static methods cannot be marked as override, resulting in derived classes that cannot be overridden but can be accessed
4. Static members are initialized the first time they are used. Non-static members are when creating objects, static is continuous in terms of memory allocation, and non-static is discrete in memory storage, so static methods and non-static methods must be faster in terms of calling speed, because non-static methods need to be instantiated and allocate memory, but static methods are not used, but the difference in speed is negligible.
The 5.static method is a member method in the class, which belongs to the whole class, that is, it can be called directly without creating any objects. There are only static variables and other static methods inside the static. And this.... cannot be used in the static method. And other keywords, because it belongs to the whole class!
Static method: the principle is that sharing code snippets will not cause any problems because code snippets are used for "reading" by CPU, unless you maliciously "modify" the runtime code snippets, so static methods can be safely used.
Static variables: the principle is that the shared data segment will not cause a problem as long as there is no "write" operation, but the data is usually used for reading and writing, so static variables should be used carefully.
Second, the code case public class A {public static void Method1 () {Console.WriteLine ("I am Method1, is a static method"); / / get the class name string className = System.Reflection.MethodBase.GetCurrentMethod (). ReflectedType.FullName; Console.WriteLine ("class of the Method1 method: {0}", className); / / namespace. Class name} public void Method2 () {Console.WriteLine ("I am Method2, not a static method"); / / non-static method gets the class name string className = this.GetType (). FullName; Console.WriteLine ("class of Method2 method: {0}", className); / / namespace. Class name; same class as the above method, so the output is the same} class Program {public static void Method3 () {Console.WriteLine ("I am Method3, not a static method");} static void Main (string [] args) {A.Method1 (); / / A.Method2 () / / error report, non-static methods must be instantiated to refer to An a = new A (); a.Method2 (); Method3 ();}} this ends the study of "how to use static methods of C#", hoping to solve everyone's doubts. The collocation of theory and practice can better help you learn, go and try it! If you want to continue to learn more related knowledge, please continue to follow the website, the editor will continue to work hard to bring you more practical articles!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.