C # Data Type

Source: Internet
Author: User

I. Value Type and reference type

1. Value Type:

Storage location: stored in the memory Stack

Including: simple type (INT, double, float, etc)

Structure Type (struct types)

Enumeration type (enmu types)

2. Reference Type:

Storage location: stored in the memory heap

Including: Class, interface, array, string

 

3. Value Type and reference type

Features

Value Type

Reference Type

Storage location

Stack

Heap

Default Value

0

Null

Parameters passed to the Method

Copy Value

Reference

Variable Storage

Actual value

Reference address

 

4. packing and unpacking

Packing: The value type is automatically converted to the reference type.

Example: int num = 200;

Object OBJ = num;

 

Binning: converts a reference type to a value type.

Example: object OBJ = 200;

Int num = (INT) OBJ;

 

Disadvantages of packing: it consumes more time than unpacking

Note: Do not pack or unpack the box at will in the program.

 

 

Ii. enumeration: belongs to the value type and is used to define a group of constants.

 

1. Definition restrictions: you cannot define your own methods, implement interfaces, or define attributes or indexes.

2. Statement:

Statement Syntax:

Access modifier Enum name

{

Enumeration member

}

Note: Members are separated by commas (,).

Example: Public Enum sex

{

Boy,

Girl

}

3. Usage:

Use Cases:

A) defines static constants and values are arranged in order

B) the members can be exhaustive.

 

 

Iii. structure (value type)

1. concept: it is a programmer-defined data type that is similar to a class and has different implementation methods and classes.

2. Comparison with classes

Class

Structure

Reference Type

Value Type

Initial value that can be assigned

Initial values cannot be assigned to declared fields.

The parameter-free constructor can be declared.

No parameter-free constructor can be declared

Must be instantiated

New is not required for instantiation.

Can be inherited except sealed class

Cannot be inherited

3. Declaration Syntax:

Struct name

{

Member

}

Note: The members are fields, attributes, methods, and parameter structures.

 

 

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.