C # Programming Fundamentals-Data types

Source: Internet
Author: User

C # is a strongly typed language, so each variable and object must have a claim type. The data type can be described as a built-in data type (such as int or char) and a user-defined data type (such as class or interface). Data types can also be defined as value types (for storing values), reference types (used to store references to actual data).

Value type

The contents of a value type are stored in the memory of the stack allocation, and if a variable is declared using one of the basic built-in data types or a user-defined structure, the variable is a value type. However, except for the string data type, it is a reference type. C # Value types are divided into simple types, struct types, and enumeration types.

1. Simple Type

C # provides a set of simple types that are already defined, grouped into integers, Boolean types, character types, and real types.

2. Structure type

Structs in C # are similar to classes, but lack some functionality, such as inheritance. Also, because structs are a value type, it is often faster to create structs than to create classes.

In programming, a group of related information is often put together. A series of related variables are formed into a single entity, and this single entity type is called a structure. struct types are declared using the keyword struct.

3. Enumeration type

An enumeration type (also known as an enumeration) provides an efficient way to define a set of named integer constants that can be assigned to variables. Declares that an enumeration type begins with an enum. For example: Enum Weekday{sunday,monday,tuesday,wednesday,thursday,friday,saturday};

An element of an enumeration type can only use a type of long,int,short,byte. The default type is int, and the first element has a value of 0, and each successive element is incremented by 1. Enumeration element values can be changed. For example: Enum Weekday{sunday=1,monday,tuesday,wednesday,thursday,friday,saturday};

Reference type

A variable of a reference type does not store the contained value directly, but instead stores the address of the value to be stored. There are four types of reference in C #: Classes, arrays, delegates, and interfaces.

Packing and unpacking

Boxing is the process of converting a value type to a reference type. Boxing a variable is to create a reference variable that points to a new copy on the heap.

Unpacking, extracting value types from objects, converting reference types to value types.

C # Programming Fundamentals-Data types

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.