C # types and variables

Source: Internet
Author: User

There are two types in C #: The value type and the reference type (reference type). Value-type variables directly include their data, whereas reference-type variables store their data references, which are considered objects. For reference types, it is possible that two variables refer to the same object, so an operation on one of the variables may affect the object referenced by another object. For value types, each variable has its own copy of the data, so operations on one variable cannot affect other variables (exceptions to ref and out parameter variables).

The value types of C # are further divided into simple types, enum types (enum type) and struct types (struct type), and the reference types of C # are further divided into class types (classes type), interface types (interface type), The array type and the delegate type (delegate type).

Table 1.1 provides an overview of the entire C # type system.

Table 1.1 Overview of the C # type system

8 integer types support both signed or unsigned formats for 8-, 16-, 32-, and 64-bit integers, respectively.

Two floating-point types, float and double, are represented in the IEEE754 format of 32-bit single-precision and 64-bit double precision respectively.

Decimal is a 128-bit data type that is suitable for financial and monetary calculations.

The bool type of C # is used to denote a Boolean value of--true or false.

In C #, the processing of characters and strings uses Unicode encoding. The char type represents a 16-bit Unicode encoding unit, and the string type represents a sequence of 16-bit Unicode encoding units.

Table 1.2 summarizes the numeric types of C #.

Table 1.2 numeric types for C #

C # programs use type declarations to create new types. The type declaration specifies the name and members of the new type. There are 5 types of C # that can be customized by the User: class type, struct type, interface type, enum type, and delegate type.

A class type defines a data structure that includes data members (fields) and function members (methods, properties, and others). Class types support inheritance and polymorphism, which is the mechanism by which derived classes can extend and specialize a base class.

struct types are similar to class types and represent structures with data members and function members. However, unlike class types, structs are value types and do not require heap allocation. Structs do not support user-specified inheritance, and all struct types implicitly inherit type object.

An interface type defines a convention that is named as a set of function members. A class or struct that implements an interface must provide an implementation of an interface function member. Interfaces may inherit from multiple base interfaces, and classes or structs may also implement multiple interfaces.

Enumeration types are unique types with named constants. Each enumeration type has a lower-level type, which must be one of 8 integral types. The set of values for an enumeration type is the same as the value set of the underlying type.

A delegate type represents a reference to a method through a specific parameter list and return type. The delegate processes the method as an entity, the entity can assign the value to the variable, and is passed as a parameter. Delegates are similar to method pointers in some programming languages, except that delegates are object-oriented and type-safe.

C # supports any type of one-dimensional and multidimensional arrays. Unlike other types, array types do not have to be declared before they are used. In fact, the construction of an array type is preceded by a type name with square 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 # 's type system is uniform so that any type of value can be processed into an object. Each of the types in C # inherits directly or indirectly from the object class, and object is the ultimate base class for all types. Value type values can be handled as objects by performing boxing (boxing) and unboxing (unboxing) operations. In the following example, int is converted to object and then reversed to int.

When a value of type values is cast to object, the object instance (also known as a "box") that holds the value is assigned, and the value is copied into the box. Conversely, when an object reference is coerced to a value type, check to see if the reference type is a box of the current value type, and if so, the value in the box will be copied.

C # Unified type system means that value types can be converted "On demand" to objects. Because of this uniformity, a generic class library of type object is used, for example. NET Framework, which can be used by reference types and value types.

There are several variables in C #, including fields, array elements, local variables, and parameters. A variable represents the location of the store, and each variable has a type that determines what value can be stored in the variable, as shown in Table 1.3.

Table 1.3 C # variables

C # types and variables

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.