C # static members and static classes

Source: Internet
Author: User

Speaking of static classes, you might associate the instance class. The two are not difficult to distinguish, the former (static Class) only create one in memory, and the latter (instance Class) is once each instantiation, will be a copy of memory. Let's talk briefly about the understanding of static classes today.

Code scenario:

classprogram{Static voidMain (string[] args) {person P1=NewPerson (); P1. Name="ZS"; P1. Liveplanet="Earth"; Person P2=NewPerson (); P1. Name="ls"; P1. Liveplanet="Earth"; }     } Public classperson{ Public stringName {Get;Set; }  Public intAge {Get;Set; }  Public stringPhone {Get;Set; }  Public  stringliveplanet {Get;Set; }}

According to the above scenario, if I need to instantiate 100 objects, and the value of the Liveplanet attribute in person is unified to Earth, do not have to rewrite 100 times? Is this right? At least you should know that it violates the dry principle.

Well, let's implement this by static to improve the code:

classprogram{Static voidMain (string[] args) {person.liveplanet="Earth"Person P1=NewPerson (); P1. Name="ZS"; //P1. Liveplanet = "Earth";Person P2=NewPerson (); P1. Name="ls"; //P1. Liveplanet = "Earth";       }     } Public classperson{ Public stringName {Get;Set; }  Public intAge {Get;Set; }  Public stringPhone {Get;Set; }  Public  Static stringliveplanet {Get;Set; }}

We change the liveplanet to static in the person class so that we can directly person.liveplanet = "Earth" without the need for instantiation. This is not only the benefit of this, but it solves that when all objects share the same information, there is no need to repeat the instantiation to invoke the object.

Please note the following points:

1. The information stored in a static member is freed from memory only when the application exits.

2. Static members cannot be accessed through objects and can only be accessed directly through the class name.

3. Static members (content) store only one copy in memory.

4. Static members can be accessed anywhere in the entire application, so static members do not release memory until the program exits. Instance members, when no variables are used, can be garbage collected, and the memory is freed after recycling. (Some common tool functions can be encapsulated in a static class, easy to use)

Now please see what the problem is with this simple change, I'll add a static method to the person and invoke the static member through this.

 Public classPerson { Public Static voidSay () { This. Name ="Hello"; }         Public stringName {Get;Set; }  Public intAge {Get;Set; }  Public stringPhone {Get;Set; }  Public Static stringliveplanet {Get;Set; } }

You might think this is a person if you're out of the compiler, so there shouldn't be a problem with this call. Actually, this way, please note that I use static statics, while static members access instance members must be new to an instance. And this refers to the current object, when using a static member because no object has been instantiated, so this time this can not know their identity.

The changes are as follows:

 Public classPerson { Public Static voidSay () {person P=NewPerson (); P.name="Hello"; //This . Name = "Hello";        }         Public stringName {Get;Set; }  Public intAge {Get;Set; }  Public stringPhone {Get;Set; }  Public Static stringliveplanet {Get;Set; } }

This is the result we want.

So what is a static class? Take a look at the following code:

 Public Static class Mystaticclass    {        publicstaticstringgetset;}          Public Static void Sayhi ()        {            Console.WriteLine ("hi");        }            }

Observe the above code to see several characteristics of static classes:

1. There can be only static members in a static class, no instance members
2. Static classes cannot create objects, not new objects

Using static classes with static members is actually the same thing: You can use static classes when you want to share data throughout your application. So for those classes that contain a large number of methods, and the class does not need to create objects, you can use static classes.

C # static members and static classes

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.