C # entity variable/class variable static

Source: Internet
Author: User

Today in Reading Stoneniqiu's "book Notes"-code neat way, encountered a word called "entity variables", forgive me so ignorant, so the basic things are not clear, ashamed. So quickly to see what this variable is ...

Entity variables (instance variables), say this stuff, you have to talk about class variables (static variables).

Let's look at a class first:

     Public classexamplevariable { Public Static intNUM1 {Get;Set; }  Public intNum2 {Get;Set; }  Public voidChangenums (intN1,intn2) {NUM1=N1;  This. Num2 =N2; }         Public int[] Getnums () {return New int[] {Num1, Num2}; }    }

The Num1 with static is the class variable, and the Num2 with no static is the entity variable. What difference do they have?

Look at a little bit of code:

    • Declare object 1, assign a value to 2 variables, display the variable data of object 1;
    • Declare object 2, assign values to 2 variables respectively, display the variable data of object 1 and object 2 respectively;
         PublicMainWindow () {InitializeComponent (); examplevariable example1=Newexamplevariable (); Example1. Changenums (Ten, One); int[] Nums1 =example1.            Getnums (); TBK1. Text=string. Format ("Example1:num1={0},num2={1}", nums1[0], nums1[1]) ;//static in class does not belong to any objectTBK1. text+= Environment.NewLine +Environment.NewLine; Examplevariable example2=Newexamplevariable (); Example2. Changenums ( -, +); Nums1=example1.            Getnums (); TBK1. Text+=string. Format ("Example1:num1={0},num2={1}", nums1[0], nums1[1]) ;//static in class does not belong to any objectTBK1. text+=Environment.NewLine; int[] Nums2 =example2.            Getnums (); TBK1. Text+=string. Format ("Example2:num1={0},num2={1}", nums2[0], nums2[1]);//static in class does not belong to any object}

Operation Result:

Can be seen:

The NUM1 in the ① class does not belong to this pointer, this. Num1 such a writing is not correct, but Num2 belongs to this, it is completely possible. Num2;

②NUM1 is static and can only be used in class examplevariable, if the class object needs to modify his value, it must be implemented by the member function,

But Num2 can be called directly in the object: object. Num2;

③num1 belongs to the entire class, and all objects have only 1 copies of it. When object 2 modifies Num1, the Num1 of object 1 will also change because 2 objects are common NUM1, but Num2 will not change.

C # entity variable/class variable static

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.