Shared variable--[asp.net]

Source: Internet
Author: User

usingSystem;usingSystem.Collections.Generic;usingSystem.Linq;usingsystem.web;usingSystem.Web.UI;usingSystem.Web.UI.WebControls;namespacedemo5{ Public Partial classGongxiang:System.Web.UI.Page { Public classUser {Private Static intCount//define static variables Count            Private Static stringName//defining static variables name             PublicUser ()//default constructor{Count++; }            StaticUser ()//Static Constructors{Count=0; Name="Initial value"; }             Public stringName//Name Attribute            {                Get                { returnname;} Set{Name=value; }            }             Public Static stringOnename//Static Properties            {                 Get                { returnname;} }             Public Static intCount//how many objects are defined with this class            {                Get {                    returnCount//We only know this number, so read only                }                        }             Public Static stringMn (stringnname) {                return "object, the result of sharing a method is;"+Nname; }        }        protected voidPage_Load (Objectsender, EventArgs e) {Response.Write ("The present is:"+user.count +"object, the initial name is:"+user.onename); User U1=NewUser (); U1. Name="days of the boom wear"; Response.Write ("<br> currently this is"+user.count +User. Mn (U1.            Name)); User U2=NewUser (); U2. Name="Tianhongchuan"; Response.Write ("<br> currently this is"+ User.count +User.mn (U2.            Name)); User U3=NewUser (); U2. Name="Huangxiaomei"; Response.Write ("<br> currently this is"+ User.count +user.mn (U3.        Name)); }    }}

Results:

If there is no instantiation, call the static constructor and assign the initial value to the variable.

If instantiated, it is executed according to the General constructor. After instantiating a class, you can access static methods, although you cannot access other static variables.

Shared variable--[asp.net]

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.