/*Static fields, static properties, static methods, static constructs, static classes, Readonly,const, nested classes, partial class * Static can be understood as global, sharing the same block of memory * can be the default value of each type through default (type name) * Static fields do not belong to a field but are subordinate to the class, and the instance fields belong to each field and have different values for each field * classes and objects can correlate data; * This is the access method used for the instance, so you can use this. Static member access * To obtain access to instance members in a static method, You must first obtain a reference to the specific instance that the field or method belongs to, and Main () is the static method * Static constructor is used to initialize the class (No is instance of Class), there is no static Terminator saying that the static constructor is not actually called, instead, The CLR automatically calls the static constructor when it accesses the class for the first time, remembering that the static constructor is called at run time, and the static constructor is translated into Il as A. cctor (), while the instance static constructor is. ctor () * Specification: inline (inline) should be used as far as possible Do not initialize with a static constructor, and avoid declaring that the static constructor is private, and that it is not allowed to have a modifier * because the static constructor does not allow the call to be displayed, no arguments are allowed. * Some classes do not contain any instance members. So this kind is going to be declare static class, that is, static class does not contain instance members, with the previous learning can know that the instance class can contain static members * Static classes are compiled into IL will be marked abstract and sealed means not extensible, You cannot derive any class from it * Static classes are compiled once CIL, no instance constructs, static constructs are inside the CLR * learning English: Generated generation, via via, invoke call, derived derivative, nested nested, literal literal * To declare an immutable value as a const constant, it is possible to change the declare to ReadOnly (only for fields, not for the data invariants) * ReadOnly fileds cannot be set outside the Costrustor. Either set the initial value directly at the time of declaration (only two cases can have values for readonly: Declaration, constructor) * A class can be defined in a class, called a nested class, and classes defined in a class can be private, However, only internal and public can be defined in the namespace;* Avoid using a common type of nested class * Partial class is the definition of a class into multiple folders, distribution methods can only be in the distribution class, at compile time directly into a class * distribution method can not have access modifiers or virtual,abstract,override,new, Sealed,huo extern modifier * User:yuanwei * DATE:2015/1/15 * time:16:30 * **/usingSystem;namespacelearningstatic{//Private class person// {// //Error: An element defined in a namespace cannot be displayed with a declaration of private,protected or protected internal, but can be declared as public and internal// } /*Partial classes: The primary function of a class is to divide the definition of a category into multiple files, often used in code generators, but you can also define multiple partial classes in the same class **/ Public Partial classPerson { Public stringname{Get;Set;} Public Doubleheight{Set;Get;} Partial voidTalk ()//distribution methods cannot have access modifiers or virtual,abstract,override,new,sealed,huo extern modifiers{}//of Private } Public Partial classPerson {Partial voidTalk (); PublicDateTime birthday{Get;Set;} }//Public Class program// {// //will give an error, the namespace already contains the definition of program, that is, the same clear space cannot contain the same class outside the partial class// } classProgram { Public Static voidMain (string[] args) { /*have an idea: if you want to make a mark on the strength members of a class. You can define a static field, each time the constructor assigns the value to a field and then self-increment * Then each object has a specific label **/ //todo:implement functionality herePerson person=NewPerson ();//Person can point out all the public members;Employee employee=NewEmployee (); Console.WriteLine (employee. Pi. ToString ()); Console.WriteLine (Employee.ID.ToString ()); //Console.WriteLine (default (int));Console.WriteLine (Employee.sex); Console.WriteLine (Employee.Name); Math.Abs (-1); Console.readkey (true); } } Public classEmployee { Public ReadOnly DoublePi=3.1415926;//instance read-only field, the value of ReadOnly can be modified in the constructor, and the const cannot be Public ReadOnly Static intId= at;//static Read-only fields are written in two ways, readonly static can be exchanged, and only set value when the constructor and declaration are initialized . Public Static ReadOnly intnum= $;//the formal wording Public Const stringsex="female";//is automatically converted to a static field, so you can pass the type name. Sex, so access is not accessible through instances Public Static stringName; Public intHeight; Public Static intage{Get;Set;}//static properties, encapsulating static fields, increased security Public Static voidAdd () {//This . Name= "Yuanwei";//error. Static members cannot be accessed with this//Use this point not to come out static membersEmployee.name="Hongli";//a static member can be accessed with the class name//This . height=180;//, error, the instance member is accessed with this (instance), so if you want to access instance members in a static method, you need to get a reference to the specific object first//This is invalid for static property, static method, or static field initial value settingEmployee employee=NewEmployee (); Employee. Height= the; Firstemployee.age= -;//can be called by a virtual class. Member name } Public DoubleSay () {//pi=3.1415926;//Error: Error:read-only Fileds cannot is set outside the constructor or when declaring returnMath.PI; }//Public Static Employee ()//error, the static constructor is not allowed access modifiers, so the static constructor is private// {} /// <summary> ///Instance Constructors/// </summary> PublicEmployee () {PI=3.14;//readonly modifications can be modified in the constructionEmployee.name="Xiaohong"; Console.WriteLine ("invoking instance constructs"); } /// <summary> ///Static Constructors/// </summary> StaticEmployee ()//high priority, so the value inside will be overwritten by the value of the call.{employee.name="xiaoming"; Console.WriteLine ("call a static construct"); } } /// <summary> ///Static Class/// </summary>//Public static Class Newemployee:employee//This definition is wrong because the static class must derive from the object// {// // } Public Static classNewEmployee//a static construct is automatically generated { Public Const intmaxnum=123456;//automatically compiled into a static literal field Public ReadOnly Static stringn="NI";//compile into static inionly string field//public static const int minval=123;//error, the constant cannot be marked as static }//Public static Class Newfirstemployee:newemployee//error, Static class also cannot derive from static class// {} Public Abstract classFirstemployee:employee//virtual classes can be derived from an instance class { //a protected constructor is automatically generated }//Public static Class Newfirstemployee:firstemployee//Error// {} Public Abstract classSecondemployee//The constructors of abstract classes are protected. { }}
C # statically static nested class partial class