A simple description of static classes, static methods, and static variables in C #

Source: Internet
Author: User

Static methods, like static variables, belong to the class itself and do not belong to an object of any class. Call a method that is defined as static, only by adding the name of this class before it. Generally, it can be called through an instance. I haven't tested other languages yet, but it doesn't work in C #. I can only passType name reference it(So are static variables ).

1 class class1 2 {3 Public static string no {set; get;} 4 Public String second {set; get;} 5 6 public static void WMT () 7 {8 NO = "I Am a static member"; 9} 10 11} 12 13 class class214 {15 public void test () 16 {17 class1.wmt (); 18 class1.no = "I Am a static member"; 19 var class1 = new class1 (); 20 class1.second = "I am a non-static member"; 21 22} 23 24}

Other points:

1. Static methods can only call static members of a class, but they can define variables internally (constants can also be placed in static methods (the compiler will implicitly set them to static ))

2. The general method can also call static members.

1 class class1 2 {3 Public static string no {set; get;} 4 Public String second {set; get;} 5 6 public static void WMT () 7 {8 string variate = "I Am a variable"; 9 NO = "I Am a static member"; 10} 11 12 public void WWW () 13 {14 NO = "I Am a static member"; 15} 16 17}

If a class only contains static methods and attributes, It is a static class.

1 static class class12 {3     public static string No { set; get; }4 5     public static void Wmt()6     {7 8     }9 }

Let's mention the difference between a constant and a read-only field:

A constant is a variable that contains a value that cannot be modified. It is static. Read-Only fields can be initialized not only during declaration,You can also assign values to constructors.Is suitable for determining the initial value only after calculation. The read-only field can also be an instance field. Every object in the class can have different values. Note: It is not necessary to assign a value to a read-only field in the constructor. If no value is assigned, its value is the default value of a specific data type.

 1 using System; 2 using System.Collections.Generic; 3 using System.Linq; 4 using System.Text; 5  6 namespace ConsoleApplication3 7 { 8     class Program 9     {10         static void Main(string[] args)11         {12             var cl = new class1("I‘m readonly");13             var c2 = new class1("I‘m readonly too");14             var c3 = new class1();15             Console.WriteLine(cl.Wmt);16             Console.Write(c2.Wmt);17             Console.Write(c3.Wmt);18             Console.Read();19         }20     }21     public class class122     {23         public readonly string Wmt;24         public class1(string words)25         {26             Wmt = words;27         }28 29         public class1()30         {31         }32     }33 34 }

Running result:

I will review my knowledge and look at the basic things. Don't try it out ~~

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.