[c# tutorial] C# data types

黄舟
Release: 2016-12-26 13:51:06
Original
1627 people have browsed it

C# data types

In C#, variables are divided into the following types:

Value types (Value types)

Reference types (Reference types)

Pointer types

Value types

Value type variables can be directly assigned to a value. They are derived from class System.ValueType.

Value types contain data directly. For example, int, char, and float store numbers, letters, and floating-point numbers respectively. When you declare an int type, the system allocates memory to store the value.

The following table lists the value types available in C# 2010:

Type

Description

Range

Default value

bool Boolean value True or False False

byte 8-bit unsigned integer 0 to 255 0

char 16-bit Unicode character U +0000 to U +ffff '

您可以存储任何类型的值在动态数据类型变量中。这些变量的类型检查是在运行时发生的。

声明动态类型的语法:

dynamic <variable_name> = value;
Copy after login

例如:

dynamic d = 20;
Copy after login

动态类型与对象类型相似,但是对象类型变量的类型检查是在编译时发生的,而动态类型变量的类型检查是在运行时发生的。

字符串(String)类型

字符串(String)类型 允许您给变量分配任何字符串值。字符串(String)类型是 System.String 类的别名。它是从对象(Object)类型派生的。字符串(String)类型的值可以通过两种形式进行分配:引号和 @引号。

例如:

String str = "w3cschool.cc";
Copy after login

一个 @引号字符串:

@"w3cschool.cc";
Copy after login

C# string 字符串的前面可以加 @(称作"逐字字符串")将转义字符(\)当作普通字符对待,比如:

string str = @"C:\Windows";
Copy after login

等价于:

string str = "C:\\Windows";
Copy after login

@ 字符串中可以任意换行,换行符及缩进空格都计算在字符串长度之内。

string str = @"<script type=""text/javascript"">
    <!--
    -->
    </script>";
Copy after login

用户自定义引用类型有:class、interface 或 delegate。我们将在以后的章节中讨论这些类型。

指针类型(Pointer types)

指针类型变量存储另一种类型的内存地址。C# 中的指针与 C 或 C++ 中的指针有相同的功能。

声明指针类型的语法:

type* identifier;
Copy after login

例如:

char* cptr;
int* iptr;
Copy after login

我们将在章节"不安全的代码"中讨论指针类型。


 以上就是【c#教程】C# 数据类型的内容,更多相关内容请关注PHP中文网(m.sbmmt.com)!

Related labels:
c#
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template