C # A Brief Description of static classes, static methods, and static variables,

Source: Internet
Author: User

C # A Brief Description of static classes, static methods, and static variables,

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 ~~


In the C language, what is the symbol (->) and how to use it?

This is a symbol in the struct pointer. Write a program to explain it, for example:
# Include <stdio. h>
Struct STU // define a struct
{
Int num;
} Stu;
Int main ()
{
Struct STU * p; // defines a struct pointer.
P = stu; // p points to the struct variable stu.
Stu. num = 100; // attaches an initial value to the struct member num.
Printf ("% d", p-> num); // output the num value in stu
Return;
}
As you can see, the-> method is to reference the variable in the struct !!
Format: p-> struct member (such as p-> num)
The function is equivalent to stu. num or (* p). num.
I don't know. You don't understand, and don't understand call me. O (∩ _ ∩) O ~
Hope to adopt it.

In the C language, what is the symbol (->) and how to use it?

This is a symbol in the struct pointer. Write a program to explain it, for example:
# Include <stdio. h>
Struct STU // define a struct
{
Int num;
} Stu;
Int main ()
{
Struct STU * p; // defines a struct pointer.
P = stu; // p points to the struct variable stu.
Stu. num = 100; // attaches an initial value to the struct member num.
Printf ("% d", p-> num); // output the num value in stu
Return;
}
As you can see, the-> method is to reference the variable in the struct !!
Format: p-> struct member (such as p-> num)
The function is equivalent to stu. num or (* p). num.
I don't know. You don't understand, and don't understand call me. O (∩ _ ∩) O ~
Hope to adopt it.

Related Article

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.