C # value type \ value type \ reference type \ constant \ variable \ type conversion

Source: Internet
Author: User
Tags constant modifier

Value type

In the previous chapter we learned a simple C # program, the main composition of the following we have to learn the basic C # fundamental syntax, the first learning is the data type of value types and reference types, as well as constant variables.

Value types can be categorized as follows: Simple type, struct type, enum type. They are derived from the class System.ValueType.

Simple types include integer (intergral types), decimal (decimal types), floating-point (floating point types), Boolean (bool types), character type (char types). The integer data contains int, sbyte, short, ushort, uint, long, ulong, Byte, char.

struct types (struct) can declare constructors, constants, fields, properties, methods, indexes, operators, and nested types, which we will specify in the chapter on structure.

An enumerated type (enum) is a unique type consisting of a specified set of constants, which we will also describe in a later chapter.

For example, int, char, float, they store numbers, letters, floating points. When you declare an int type, the system allocates memory to store the value.

Identifiers that define value types there are also bool, byte, Decimal, double, long, sbyte, short, and Uint,ulong,ushort.

We want to define variables of a Int,char type and assign them a value:

int a=5; Defines an int type variable and initializes it;
Char c= ' 2 '; Define a char variable and initialize it;

Reference type


Reference types do not contain actual data stored in a variable, but they contain a reference to a variable. In other words, they refer to a memory location. When you use multiple variables, a reference type can point to a memory location. If the data in the memory location is changed by a variable, the other variables automatically reflect the change in that value. The built-in reference types are: object, dynamic, and string.

Object type is the ultimate base class for all data types in the C # Common type system (Common type system-cts). Object is an alias for the System.Object class. So the object type can be assigned any other type of value. However, before assigning a value, you need to convert the type first.

When a value type is converted to an object type, it is referred to as boxing and, on the other hand, when an object type is converted to a value type, it is called a unboxing.

Object obj;
obj = 100; This is boxing.
A dynamic type can store values of any type defined by: dynamic = value; For example, the following code:

Dynamic d = 20;
The string type allows you to assign any string value to a variable. The string type is an alias for the System.String class. It is derived from the object type. Values of string types can be assigned in two forms: quotes and @ quotes.

A C # string literal can be preceded by an @ (called a "verbatim string") to treat the escape character (\) as a normal character, such as:

String str = @ "C:\Windows";
Equivalent to:

String str = "C:\\Windows";
The @ string can be wrapped in any line, and the line breaks and indentation spaces are counted within the string length.

String str = @ "<script type=" Text/javascript ">
<!--
-->
</script> ";
We will also describe the string in detail later.

Constant


A constant is the amount at which the value is fixed. From the data type point of view, the type of a constant can be any value type or reference type.

The declaration of a constant is the name of the constant being used in the declaration program and its value. As with variables, we can declare one or more constants of a given type at the same time, and the declaration format of constants is as follows.

Constant-modifier const type constant-declarators;
Where the constant modifier constant-modifier can be these: new, public, protected, internal, private. Const is a keyword, and type can be byte, short, int, uint, ulong, long, char ... As well as enumeration types or reference types.

public const int MaxValue = 10
Integer constants can be decimal, octal, or hexadecimal constants. prefix specifies cardinality: 0x or 0X for hexadecimal, 0 for octal, no prefix for decimal.

A floating-point constant consists of an integer part, a decimal point, a fractional part, and an exponent. You can use decimal form or exponential form to represent floating-point constants.

The constants quantity is enclosed in single quotes, for example, ' X ', and can be stored in a simple character type variable. A character constants can be a normal character (such as ' X '), an escape sequence (such as ' \ t '), or a common character (such as ' \u02c0 ').

String constants are enclosed in double quotes "", or are enclosed in @ "".

212/* Legal * *
0xFeeL/* Legal *
85/* Decimal * *
0213/* Octal * *
0X4B/* Hex *
/* int */
30U/* unsigned int */
30l/* long *
30ul/* unsigned long * *
3.14159/* Legal * *
314159E-5L/* Legal *
\ \ \ Character
\ ' character
\ "" Character
\? ? Character
"Hello" Hello string

Variable

A variable is simply the name of a storage area for program operations. In C #, each variable has a specific type, and the type determines the memory size and layout of the variable. How do you define variables? The definition format is as follows

Data_type variable_list;
Here, data_type must be a valid C # data type, which can be char, int, float, double, or other user-defined data types. Variable_list can consist of one or more comma-delimited identifier names.

A variable can be initialized at the time of declaration (specifying an initial value). Initialization consists of an equal sign followed by a constant expression.

int d = 3, F = 5; /* Define initialize D and f */
byte z = 22; /* Define Initialize z/*
Double pi = 3.14159; /* Definition of the approximate value of the pi * *
char x = ' x '; /* Defines the value of the variable x as ' x ' *

Type conversions

In C #, there are two forms of type conversions:

Implicit type conversions-these conversions are the default security-mode conversions of C #. For example, a small integer type converts to a large integer type, from a derived class to a base class.

Explicit type conversions-these transformations are done explicitly by the user using predefined functions. An explicit conversion requires a cast operator.

The syntax for the cast is: (data_type) variable; The parentheses cannot be omitted, Data_type is the type descriptor to be converted, variable is the variable to be converted.

Double d = 5673.74;
int i;
i = (int) d; Cast double to int
The execution result is 5673.

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.