1.3 types and variables

Source: Internet
Author: User

C # contains two types: Value type valuetype and reference type ). Value-type variables directly include their data, while reference-type variables store their data references, which are called objects. For reference types, two variables may reference the same object. Therefore, operations on one of the variables may affect the objects referenced by another object. For value types, each variable has its own data copy. Therefore, operations on one variable cannot affect the operations on another variable ).


C # value types are further divided into simple type simpletype), enumeration type enum type) and structure type struct type );

The reference type of C # is further divided into class type classtype, interface type), array type) and delegate type ).

C # type system overview table:

Category

Description

Value Type

Simple Type

Signed integer: sbyte, short, int, long

Unsigned integer: byte, ushort, uint, ulong

Unicode Character: char

IEEE floating point: float, double

High-Precision decimal

Boolean: bool

Enumeration type

User-Defined type: enum E {...}

Structure Type

User-Defined type: struct S {...}

Reference Type

Class type

Base class of all user types: object

Unicode string: string

User-Defined type: class C {...}

Interface Type

User-Defined type: interface I {...}

Array type

Single-dimension and multi-dimensional arrays: int [], int [,]

Delegate type

User-Defined type: delegate t d (...)


C # The program uses the type description to create a new type. Type declaration specifies the names and members of the new type. Five types of C # can be customized by users: class type, interface type, delegate type, enumeration type, and structure type.

650) this. width = 650; "width =" 19 "height =" 18 "src ="/e/u/themes/default/images/spacer.gif "word_img =" file: /// C:/Users/liu/AppData/Local/Temp/msohtmlclip1/01/clip_image001.gif "style =" url (/e/u/themes/default/images/word.gif) no-repeat center; border: 1px solid # ddd "/> the class type defines a data structure, which includes data member fields), function member methods, attributes, and others ). The class type supports inheritance and polymorphism, that is, the extended and special base class mechanism of the derived class.

650) this. width = 650; "width =" 19 "height =" 18 "src ="/e/u/themes/default/images/spacer.gif "word_img =" file: /// C:/Users/liu/AppData/Local/Temp/msohtmlclip1/01/clip_image002.gif "style =" url (/e/u/themes/default/images/word.gif) no-repeat center; border: 1px solid # ddd "/> 650) this. width = 650; "width =" 19 "height =" 19 "src ="/e/u/themes/default/images/spacer.gif "word_img =" file: /// C:/Users/liu/AppData/L Ocal/Temp/msohtmlclip1/01/clip_image003.gif "style =" url (/e/u/themes/default/images/word.gif) no-repeat center; border: 1px solid # ddd "/> the structure type is similar to the class type, indicating the structure with data members and function members. However, unlike the class type, the structure is a value type and does not require heap allocation. Structures do not support user-assigned inheritance. All structure types are implicitly inherited objects.

650) this. width = 650; "width =" 19 "height =" 18 "src ="/e/u/themes/default/images/spacer.gif "word_img =" file: /// C:/Users/liu/AppData/Local/Temp/msohtmlclip1/01/clip_image004.gif "style =" url (/e/u/themes/default/images/word.gif) no-repeat center; border: 1px solid # ddd "/> the interface type defines a convention as a set of function Member names. The class or structure that implements the interface must provide the Implementation of the interface member function. Interfaces may be inherited from multiple basic interfaces, and classes or structures may also implement multiple interfaces.

The enumerated type is a unique type with a named constant. Each Enumeration type has an underlying type, which must be one of eight integer types. The value set of the enumeration type is the same as that of the underlying type.

650) this. width = 650; "width =" 19 "height =" 18 "src ="/e/u/themes/default/images/spacer.gif "word_img =" file: /// C:/Users/liu/AppData/Local/Temp/msohtmlclip1/01/clip_image005.gif "style =" url (/e/u/themes/default/images/word.gif) no-repeat center; border: 1px solid # ddd "/> Delegate types refer to methods through special parameter lists and return types. The delegate processes the method as an entity. The entity can assign values to variables and pass them as parameters. Delegation is similar to method pointers in some programming languages. The difference is that delegation is object-oriented and the type is secure.


C # supports any types of one-dimensional or multi-dimensional arrays. Unlike other types, array types do not need to be declared before they are used. In fact, the structure of the data type is composed of a type name plus brackets. For example, int [] is a one-dimensional array of int, int [,] is a two-dimensional array of int, and int [] [] is a one-dimensional array of one-dimensional arrays of int.


C # The type system is uniform, so that any type of value can be processed as an object. In C #, each type is directly or indirectly inherited from the object class, and the object type is the final base class of all types. Values of the value type can be processed as objects by Running boxing) and unboxing. In the following example, the int type is converted to the object type and converted back to the int type.

Using System;

Class test

{

Static void Main (){

Int I = 123;

Object o = I; // boxed

Int j = (int) o; // unboxing

}

}


When a value of the value type is forcibly converted to an object, it is assigned to an instance that holds this value, also known as a "box"), and the value is also copied to that box. On the contrary, when an object reference is forcibly converted to a value type, you must check whether this type reference is a box of the current value type. If yes, the value in the box will be copied.

C # A unified type system means that the value type can be converted to an object as needed. Due to this uniformity, class libraries of general object types can be used by value types and reference types, such as collection classes in. NET Framework.

C # contains several variables, including fields, array elements, local variables, and parameters. A variable represents the storage location, and each variable has a type that determines the value that can be stored in the variable.


This article from the "CSharp development topics" blog, please be sure to keep this source http://cssoffer.blog.51cto.com/7059827/1242390

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.