Basic _ C # data type, default value, New Keyword, and formatted value result table

Source: Internet
Author: User
C # data type, default value, New Keyword, formatted value result table

Data Type Overview

The data type can be described as follows:

  • Built-in data types, such as Int or char,

  • User-Defined data types, such as class or interface.

  • The data type can also be defined:

  • Value Type (C # reference) (used to store values ),

  • Reference Type (C # reference) (used to store references to actual data ).

Built-in type table

 

C # type . NET Framework type

Bool

System. Boolean

Byte

System. byte

Sbyte

System. sbyte

Char

System. Char

Decimal

System. Decimal

Double

System. Double

Float

System. Single

Int

System. int32

Uint

System. uint32

Long

System. int64

Ulong

System. uint64

Object

System. Object

Short

System. int16

Ushort

System. uint16

String

System. String

 

Default table

Remember: uninitialized variables are not allowed in C.
The following table shows the default values of the value types returned by the default constructor. The default constructor is called through the new operator.

 

 

Value Type Default Value

Bool

False

Byte

0

Char

'/0'

Decimal

0.0 m

Double

0.0d

Enum

Value generated by expression (e) 0, where E is the enum identifier.

Float

0.0f

Int

0

Long

0l

Sbyte

0

Short

0

Struct

Set all value type fields to the default value and set all reference type fields to null.

Uint

0

Ulong

0

Ushort

0

 

Format the numeric result table

 

C or C

Currency

Console. Write ("{0: c}", 2.5 );

Console. Write ("{0: c}",-2.5 );

$2.50

($2.50)

D Or d

Decimal number

Console. Write ("{0: D5}", 25 );

00025

E or E

Scientific type

Console. Write ("{0: e}", 250000 );

2.500000e + 005

F or F

Fixed Point

Console. Write ("{0: F2}", 25 );

Console. Write ("{0: F0}", 25 );

25.00

25

G or G

General

Console. Write ("{0: g}", 2.5 );

2.5

N or N

Number

Console. Write ("{0: n}", 2500000 );

2,500,000.00

X or X

Hexadecimal

Console. Write ("{0: x}", 250 );

Console. Write ("{0: x}", 0 xFFFF );

Fa

FFFF

 

New Keyword

Int Myint = new int (); equivalent to int Myint = 0;

Point P = new point (); // after this call, the structure is considered to have been explicitly assigned values; that is, all members of the structure have been initialized to their default values.

In C #, The New Keyword can be used as an operator, modifier, or constraint.

  • Used to create objects and call constructors. Example: class1 o = new class1 ();
  • The new operator is also used to call the default constructor of the value type. For example, int I = new int ();

  • When used as a modifier, The New Keyword can explicitly hide members inherited from the base class. Hiding inherited members means that the derived version of the member replaces the base class version. See

  • The new constraint specifies that any type parameter in the generic class declaration must have a common non-parameter constructor. When a generic class creates a new instance of the type, apply this constraint to the type parameter. For more information, see

Remember, it is wrong to declare the default constructor for the structure, because each value type has a common default constructor implicitly. You can declare a parameterized constructor on the structure type to set its initial value. However, this is required only when a value other than the default value is required.

Value Type objects (such as structures) are created on the stack, and reference type objects (such as classes) are created on the stack. Both types of objects are automatically destroyed. However, a value-based object is destroyed when the range is exceeded, the object based on the reference type is destroyed at an uncertain time after the last reference of the object is removed. For reference types that occupy fixed resources (such as a large amount of memory, file handles, or network connections), deterministic termination is sometimes required to ensure that the object is destroyed as soon as possible. For more information, see using statements (C # reference ).

The new operator cannot be overloaded.

If the new operator fails to allocate memory, an exception outofmemoryexception is thrown.

 

Value Type
The value type consists of two types: Structure enumeration.

The structure is divided into the following types:
User-defined structure of the floating point decimal bool of the numeric (numeric) type Integer type.

Main functions of Value Type
A value-type variable directly contains values. When a value type variable is assigned to another value type variable, the included values are copied.
Different from the reference type, the new type cannot be derived from the value type. But what is the same as the reference type is that the structure can also implement interfaces.
Unlike the reference type, the value type cannot contain null values. However, the null type function allows null to be assigned to the value type.
Each value type has an implicit default constructor to initialize the default value of this type.

Reference Type

 

Variables of the reference type, also known as objects, can store references to actual data. The following keywords are used to declare the reference type:

  • Class

  • Interface

  • Delegate

 

The following built-in reference types:

  • Object

  • String

 

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.