C # BASIC programming----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 a copy of its own data (in addition to the ref and out parameter variables), so the operation on one variable cannot affect the other variable.


C # Value types can be categorized as simple types (easy type), enum types (enum type), struct types (struct type), and nullable (Nullale type) types.

C # Reference types can be categorized into class types (classes type), interface types (Interface type), array types (arrays type), and delegate types (Delegate type).


C # Value types (value type):

1. Simple Type:

Signed integer: sbyte (8 bit), short (+ bit), int (+ bit), long (with a bit)

unsigned integer: Byte (8 bit), ushort (+ bit), uint (+ bit), ulong

Unicode character: Char, which represents a UTF-16 encoding unit.

Float type: float (triple bit), double, which is represented in IEEE754 format with single and double precision.

High precision decimal type: decimal bit, suitable for use in financial calculations and currency calculations.

Boolean: bool, used to represent a Boolean value of True and false.

2. Enumeration type (enum type):

Enum E {...} form of user-defined type

3. Struct types (struct type):

struct S {...} form of user-defined type

4. Nullable type (Nullable type):

Extension of all other value types with null values


C # Reference type (Reference type):

1. Class Type:

Final base class for all other types: Object

Unicode string: string

Class C {...} form of user-defined type

2. Interface type (Interface type):

Interface I {...} form of user-defined type

3. Array type:

One-dimensional arrays and multidimensional arrays, such as int[] and int[,]

4. Delegate type (Delegate type):

delegate int D (...) form of user-defined type


The range of values for simple types in C # value types:

Signed Integral type:

SByte, 8 bit, value range: 128 ~ 127

Short, bit, value range: 32678 ~ 32767

int, bit, value range: 2147483648 ~ 2147483647

Long, a bit, value range: –9223372036854775808 ~ 9223372036854775807


Unsigned integral type:

Byte, 8 bit, value range: 0 ~ 255

ushort, range of values: 0 ~ 65535

UINT, with a value range: 0 ~ 4294967295

ULONG, 0 bit, value range: 18446744073709551615


Floating point type:

Float, three bit, value range: 1.5 * 10^-45 ~ 3.4 * 10 ^ 38, 7 bit precision

Double, scale bit, value range: 1.5 * 10^-324 ~ 1.7 * 10^308,15 bit accuracy


High Precision Decimal Type:

Decimal, bit, Value range: 1.0 * 10^-28 ~ 7.9 * 10^28,28 bit accuracy


Character type:

Char, with a value range: 0 ~ 65535


Boolean type:

bool, value range: True/false


C # programs use Type Declaration to create new types. The type declaration specifies the name and member of the hand type. In C # type classifications, five classes are user-definable: class type, struct type, interface type, enum type, and delegate type.

A class type defines a data structure that contains data members and function members. Class types support single inheritance and polymorphism, and derived classes can be used to extend and specialize the mechanism of a base class.

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.

The class type, struct type, interface type, and delegate type all support generics, which 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.

A nullable type can also be used without declaring it. For each value type T that is not ull, there is a corresponding nullable type T?, which can hold additional value NULL.

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 treated as objects. The value of a value type is handled by the object by boxing and unpacking it.

When the value of a value type is converted to type object, an object instance (also called 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.


C # variables include fields, array elements, local variables, and parameters.

Variables represent storage variables, and each variable has a type that determines what values can be stored in variables, as outlined below:

1. Non-nullable value type variable: A value of exactly the same type.

2. Nullable value type variable: null value and type exactly the same value.

3. Object variable: A null reference, a reference to an object of any reference type, or a boxed reference to any value type.

4. Class-type variable: A null reference, a reference to an instance of that class type, or a reference to an instance of a class derived from that class type.

5. The interface type variable: A null reference, a reference to an instance of the class type that implements the interface type, or a reference to the boxed value of a value type that implements the interface type.

6. Array type variable: A null reference, a reference to an instance of the array type, or a reference to an instance of a compatible array type.

7. Delegate type variable: a null reference or a reference to an instance of the delegate type.

This article is from the "Time is the truth of the Best friend" blog, please be sure to keep this source http://joycejm.blog.51cto.com/9781632/1626273

C # BASIC programming----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.