Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

What is the static field and instance field of C#

2025-03-28 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/02 Report--

This article mainly introduces "what is the static field and instance field of C#". In the daily operation, I believe that many people have doubts about what the static field and instance field of C# are. The editor consulted all kinds of materials and sorted out the simple and easy-to-use operation method. I hope it will be helpful to answer the doubt of "what is the static field and instance field of C#". Next, please follow the editor to study!

C# static field and C# instance field

There are two kinds of fields: C# static field and C# instance field. The field is passed by reference and can be of any type.

Example:

Private static int i = 0; / / name a static field private int j = 0; / / name an instance field static void Main (string [] args) {Program a = new Program (); / / create an object reference and instantiate it. Console.WriteLine (a.j); / / use the object to access the field j Console.WriteLine (Program.i); / / static fields need to be accessed with the class name}

We can see from the example that the static field belongs to the class and the instance field belongs to the object.

Readonly Guanjian words:

The readonly Guanjian word is used to declare a read-only field, that is, the field is not allowed to be rewritten, but with one exception, in the constructor, the read-only field can be rewritten.

Example:

Class Program {private readonly int i = 0 new Program / name A read-only field private readonly int j = 0; Program () {this.j = 10 strike / rewrite field j in the constructor} static void Main (string [] args) {Program a = new Program (); / / create an object reference and instantiate it. / / A.I = 10ramp / here is the error report Console.WriteLine (A.J); Console.WriteLine (A.I);}} at this point, the study of "what is the static field and instance field of C#" is over, 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.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report