38. My C # learning Note 4

Source: Internet
Author: User

Data type:

variable types in C # can be divided into two categories depending on their definition: one is a value type and one is a reference type. The difference between the two types is how the data is stored. The variables of the value type themselves store the data directly. A reference type stores a reference to the actual data, and the program finds the real data through this reference.

Value-type variables store their data values directly, mainly including integer types, floating-point types, and Boolean types. Value type variables are allocated in the stack and are therefore highly efficient, and the primary purpose of using value types is to improve performance. Value types have the following characteristics:

1. value types are stored in the stack.

2. When you access a value type variable, you typically access its instance directly.

3. each value-type variable has its own copy of the data, so an operation on a value-type variable does not affect other variables.

4. when copying a value type variable, the value of the variable is copied, not the address of the variable.

5. A value type variable cannot be nulland must have a certain value.


A value type is a type that inherits from the System.ValueType class.

Integer type:

Integer types are used to store integer values, that is, numeric values that do not have fractional parts. Can be a positive number, or it can be a negative number. There are 3 Representations of integer data in C # : Decimal, octal, hexadecimal, respectively.

Where octal must start with 0 . For example:0123.

Hex must start with 0x or 0X .

c# sbyte short int long  byte USHORT  uint ulong

Floating-point type variables are primarily used to process numeric data containing decimals, and floating-point types mainly contain float,double , two numeric types.

If you do not make any settings, the numeric value that contains the decimal point is considered to be a double type. If you want to treat a numeric value as a float Type, you should specify it by forcing F or F to Float type.

If you want to force a numeric value to be specified as a double type, you should use D or D to set it.

Boolean types are primarily used to represent true or false values. A variable of a Boolean type whose value can only be true or false. You cannot assign other values to a Boolean type variable. A Boolean type variable cannot be converted from another type. Boolean types are often used in process control as a condition of judgement.

Reference type:

Reference types are The primary object type data for building C # applications. During application execution, a predefined object type creates an object type with new and is stored in the heap. A heap is a system-resilient memory space that has no specific size and time to live, so it can be flexibly applied to object access. Reference type is similar to the life of agents, agents do not own products, but agent manufacturers of products, so it is like their own products.

A reference type has the following characteristics:

1. you must allocate memory for reference type variables in the managed heap.

2. Use the new keyword to create a reference type variable.

3. Each object that is allocated in the managed heap has additional members associated with it, and those members must be initialized.

4. reference type variables are managed by the garbage collection mechanism.

5. multiple reference-type variables can refer to the same object, in which case the operation on one variable affects the same object that is referenced by the other variable.

6. the value before the reference type is assigned is null.

7. all referred to as "classes" are reference types. These include: classes, interfaces, arrays, and delegates.


For example: Create a console application in which you create a class C, establish a field Value in this class, and initialize to 0. Then, you Create a reference-type variable for this class through new in the other location of the program. The code is as follows:

Class Program

{

Class C

{

public int value=0;

}

public static void Main (string[] args)

{

C c1=new C ();

C1. value=100;

}

}



This article is from the "Smile" blog, please be sure to keep this source http://yiyiweixiao.blog.51cto.com/2476874/1976416

38. My C # learning Note 4

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.