C # types and variables

Source: Internet
Author: User

There are two kinds of types in C #: Value types and reference types (reference type). Variables of value types directly contain their data, whereas a variable of a reference type stores a reference to their data, which is called an object. For reference types, two variables may refer to the same object, so an operation on one variable may affect the object referenced by another variable. For value types, each variable has its own copy of the data (in addition to the ref and out parameter variables), so the operation on one variable cannot affect the other variable.

C # 's value types are further divided into simple types, enum types (enum type), struct types (struct type), and nullable types (nullable type), and C # Reference types are further divided into class types (class Type), interface types (interface type), array types (arrays type), and delegate types (delegate type).

The following table provides an overview of the C # type system

The eight integer types support both signed and unsigned forms of 8-, 16-, 32-, and 64-bit integer values, respectively.

Two floating-point types: float and double, respectively, using 32-bit single-precision and 64-bit double-precision IEEE 754 format representations.

The decimal type is a 128-bit data type that is suitable for use in financial calculations and currency calculations.

The bool type of C # is used to represent a Boolean value of TRUE or false.

In C #, character and string processing uses Unicode encoding. The char type represents a UTF-16 encoding unit, and the string type represents a sequence of UTF-16 encoded cells.

The following table summarizes the numeric types of C #.

A C # program creates a new type by using a type declaration (type declaration). The type declaration specifies the name and member of the new type. In C # type classifications, there are five classes that are user definable: class type, struct type (struct type), interface type (interface type), enum type (enum type), and delegate type (delegate type).

A class type defines a data structure that contains data members (fields) and function members (methods, properties, and so on). Class types support single inheritance and polymorphism, which are mechanisms that derived classes can use to extend and specialize base classes.

A struct type is similar to a class type, representing a structure with data members and function members. However, unlike classes, structs are a type of value and do not require heap allocation. struct types do not support user-specified inheritance, and all struct types implicitly inherit from type Object.

An interface type defines a contract as a named set of members of a public function. A class or struct that implements an interface must provide the implementation of a function member of that interface. An interface can inherit from multiple base interfaces, and a class or struct can implement multiple interfaces.

A delegate type represents a reference to a method with a specific argument list and return type. By delegating, we can assign a method as an entity to a variable and pass it as a parameter. Delegates are similar to the concept of function pointers in some other languages, but unlike function pointers, delegates are object-oriented and type-safe.

Class types, struct types, interface types, and delegate types all support generics, so they can be parameterized by other types.

Enumeration types are unique types that have named constants. Each enumeration type has an underlying type, which must be one of eight integral types. The value set of the enumeration type is the same as the set of values for its underlying type.

C # supports one-dimensional and multidimensional arrays of any type. Unlike the types listed above, array types do not have to be declared for use. In fact, an array type is constructed by adding a pair of parentheses to a type name. For example, int[] is a one-dimensional int array, int[,] is a two-dimensional int array, int[][] is a one-dimensional array of one-dimensional int arrays.

A nullable type can also be used without declaring it. For each non-nullable value type T, there is a corresponding nullable type T?, which can hold additional value NULL. For example, int? The type can hold any 32-bit integer or null value.

C # 's type system is uniform, so any type of value can be handled by object. Each type in C # derives directly or indirectly from the object class type, and object is the final base class for all types. Values of reference types are treated as object types and are simply

To be treated as objects. The value of a value type is handled by object by boxing and unpacking it. The following example converts an int value to object and then converts it back to int.

Using System;

Class Test
{
static void Main () {
int i = 123;
Object o = i; Boxing
int j = (int) o; Unboxing
}
}

When the value of a value type is converted to type object, an object instance (also known as a "box") is assigned to contain the value and the value is copied into the box. Conversely, when an object reference is cast to a value type, the referenced object is checked for the correct value type, and if so, the value in the box is copied.

The unified type System of C # actually means that value types can be converted "On demand" to objects. Because of unification, common libraries that use type object can be used with reference types and value types.

There are several variables (variable) in C #, including fields, array elements, local variables, and parameters. Variables represent storage locations, and each variable has a type that determines what values can be stored in variables, as shown in the following table.

C # types and variables

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.