C # value types, custom value types (struct,enum)

Source: Internet
Author: User

/*C # VALUE type ValueType * 1. There are two types of custom values: structure, enumeration, * 2. Value type (value types) and reference type (reference type): The difference arises from the different replication policies, which in turn cause each type to be stored differently in memory. Value type: Directly contains the value, in other words, the location of the reference is the value in memory * The actual storage location, the address is directly addressed, the value type is stored in the stack, the reference type is stored in the heap, the variable name is stored in the stack, the value type needs a memory copy; * value type is worth copying ; A reference is a shared block of memory; A variable of a reference type is associated with two storage locations: the storage location that is associated directly with the variable, and the storage location referenced by the value stored in the variable, which is equivalent to an indirection, the heap requires garbage collection, and when the value of the reference type is copied, only the reference is assigned, A 32-bit machine is a reference to the 8B, if the cost of copying the reference * type is higher than the reference type, it is designed to be a reference type, requiring a value type of not more than 16B, except that object and string type are reference types, all C # built-in types * are value types; use Keyword struct to declare a vlaue type;declaring a reference type as a struct would create a value type larger than-byt Es * 3. Structs cannot explicitly include parameterless constructors, and each structure automatically gets an argument-free constructor that initializes all fields to their default values, default is no write-inherited from, and the default inherits from System.ValueType * 4.readonly can only modify fields, It is not possible to modify the properties of the * 5. You cannot assign an initial value to a field at the time of declaration, but this is not an indication that the field does not need to be initialized unless all fields are initialized, and the local variable of the struct type cannot be used * 6 7. The support fields of the auto-implemented properties must be fully assigned to return control to the caller; * 8. The methods and properties are initialized to all fields to be accessible and need to be initialized directly to all fields, including the support fields of the attributes; * 9. Using the new operator for reference types will cause the "runtime" A new instance of the object is created on the heap and all fields are initialized to the default values, and then the constructor is called, and the reference to the instance is passed to the constructor, and the result is a reference to the final * instance, which will be copied to the final destination; * 10. For value typesUsing the new operator causes the runtime to create a new instance of the object in the temporary storage pool, initializes all fields to the default values, and then invokes the constructor (the temporary storage location is passed to the constructor as a ref variable), and the result * is the value of the temporary storage location, which is copied to the final destination; 11. The struct does not support finalizers (Finalize), only class types can contain finalizers, reference types have "referential identities", structures are copied in a worthwhile form; * 12. Any value type can use the new variable type (); * 13. All value types are implicitly sealed, All value types other than the enumeration type (extends System.Enum and it inherits from the ValueType Class) are derived from system.value. This means that the inheritance chain is always from object to System.value to structure; 14: Value type can implement interface; * 15. Once again, there are only two forms under the namespace: internal and public * 16. The enumeration type is more than one field in the CIL, and each item is a field, in the form of Int32, The order of the enumerations represents different values, so do not insert the middle when new items are added to the enumeration in the future, and at the end, * or the assigned value that is displayed; * The fields in 17.enum are compiled into static * 18. Specification: Do not define the structure unless he logically represents a single value, consumes less than 16B of fire storage space, immutable, and rarely boxed; the main function of the enumeration is the readability of the code; * User:yuanwei *   DATE:2015/1/19 * time:15:24 **/usingSystem;namespacelearningvaluetype{classProgram { Public Static voidMain (string[] args) {            //todo:implement functionality hereConnectionstate1[] states= (connectionstate1[]) (Array)Newconnectionstate2[ the];//float fnum=23f;//byte bnum=3;//sbyte sbnum=4;            intfirstnum= at; intsecondnum= -;//UINT uinum=24;            floatfnum=New float();//Any value type can use the new variable type (); Is the same as float fnum=0;default (int)Console.WriteLine (Fnum); Angle Angle=NewAngle ( -, -, -); Console.WriteLine (NewProgram ().            ADD (Firstnum, secondnum)); Console.readkey (true); }         Public intADD (intFirstvalue,intsecondvalue) {            returnFirstvalue+SecondValue; }    }     Public structAngle {Interfaceidisplyable {//interfaces can be declared in structs        }         enumSex//inside the structure, the default is private .{man, Woman}//Public Angle ()//is implicitly constructed and cannot be explicitly given;//        {        //error: Struct cannot explicitly include parameterless constructor//        }        /// <summary>        ///constructor, cannot explicitly include an argument-free constructor/// </summary>        /// <param name= "degrees" >degree</param>        /// <param name= "Minutes" >points</param>        /// <param name= "seconds" >seconds</param>         PublicAngle (intDegreesintMinutesintseconds) {_degrees=degrees; _minutes=minutes; _seconds=seconds; }         Public intdegrees{Get{return_degrees;}}//property of Degrees        Private ReadOnly int_degrees;//Filed of _degrees//private int num=23;//Error: Cannot assign an initial value to the field at declaration time, but this is not a description field does not need to be initialized, unless all fields are initialized, the local variables of the struct type cannot be used         Public intminutes{Get{return_minutes;}} Private ReadOnly  int_minutes;  Public intseconds{Get{return_seconds;}} Private ReadOnly int_seconds;//public int num{get;set;}//Error: The support field must be fully assigned to the auto-implemented property to return control to the caller.          PublicAngle Move (intDegreesintMinutesintseconds) {            return NewAngle (degrees+degrees,minutes+minutes,seconds+seconds); }//~angle ()//        {//Error: Only class type can contain destructors//        }         Public stringReportsex () {return(Sex.man).        ToString (); }    }     enumGender//The default is internal, in CIL is confined class, internal words is private,public is public{Male, Female}enumConnectionState1 {disconnection, connecting, Connected, disconnecting} enumConnectionState2 {disconnection, connecting, Connected, disconnecting}}

C # value types, custom value types (struct,enum)

Related Article

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.