当前位置:网站首页>C# 数据类型_摘自菜鸟教程
C# 数据类型_摘自菜鸟教程
2022-07-26 21:16:00 【unity工具人】
值类型(Value types)
值类型变量可以直接分配给一个值。它们是从类 System.ValueType 中派生的。
值类型直接包含数据。比如 int、char、float,它们分别存储数字、字符、浮点数。当您声明一个 int 类型时,系统分配内存来存储值。
下表列出了 C# 2010 中可用的值类型:
| 类型 | 描述 | 范围 | 默认 |
|---|---|---|---|
| bool | 布尔值 | True 或 False | False |
| byte | 8 位无符号整数 | 0 到 255 | 0 |
| char | 16 位 Unicode 字符 | U +0000 到 U +ffff | ‘\0’ |
| decimal | 128 位精确的十进制值,28-29 有效位数 | (-7.9 x 1028 到 7.9 x 1028) / 100 到 28 | 0.0M |
| double | 64 位双精度浮点型 | (+/-)5.0 x 10-324 到 (+/-)1.7 x 10308 | 0.0D |
| float | 32 位单精度浮点型 | -3.4 x 1038 到 + 3.4 x 1038 | 0.0F |
| int | 32 位有符号整数类型 | -2,147,483,648 到 2,147,483,647 | 0 |
| long | 64 位有符号整数类型 | -9,223,372,036,854,775,808 到 9,223,372,036,854,775,807 | 0L |
| sbyte | 8 位有符号整数类型 | -128 到 127 | 0 |
| short | 16 位有符号整数类型 | -32,768 到 32,767 | 0 |
| uint | 32 位无符号整数类型 | 0 到 4,294,967,295 | 0 |
| ulong | 64 位无符号整数类型 | 0 到 18,446,744,073,709,551,615 | 0 |
| ushort | 16 位无符号整数类型 | 0 到 65,535 | 0 |
如需得到一个类型或一个变量在特定平台上的准确尺寸,可以使用 sizeof 方法。表达式 sizeof(type) 产生以字节为单位存储对象或类型的存储尺寸。下面举例获取任何机器上 int 类型的存储尺寸:
实例
using System;
namespace DataTypeApplication
{
class Program
{
static void Main(string[] args)
{
Console.WriteLine("Size of int: {0}", sizeof(int));
Console.ReadLine();
}
}
}
当上面的代码被编译和执行时,它会产生下列结果:
Size of int: 4
引用类型(Reference types)
对象(Object)类型
对象(Object)类型 是 C# 通用类型系统(Common Type System - CTS)中所有数据类型的终极基类。Object 是 System.Object 类的别名。所以对象(Object)类型可以被分配任何其他类型(值类型、引用类型、预定义类型或用户自定义类型)的值。但是,在分配值之前,需要先进行类型转换。
当一个值类型转换为对象类型时,则被称为 装箱;另一方面,当一个对象类型转换为值类型时,则被称为 拆箱。
object obj;
obj = 100; // 这是装箱
动态(Dynamic)类型
您可以存储任何类型的值在动态数据类型变量中。这些变量的类型检查是在运行时发生的。
声明动态类型的语法:
dynamic <variable_name> = value;
例如:
dynamic d = 20;
动态类型与对象类型相似,但是对象类型变量的类型检查是在编译时发生的,而动态类型变量的类型检查是在运行时发生的。
字符串(String)类型
字符串(String)类型 允许您给变量分配任何字符串值。字符串(String)类型是 System.String 类的别名。它是从对象(Object)类型派生的。字符串(String)类型的值可以通过两种形式进行分配:引号和 @引号。
例如:
String str = "runoob.com";
一个 @引号字符串:
@"runoob.com";
C# string 字符串的前面可以加 @(称作"逐字字符串")将转义字符(\)当作普通字符对待,比如:
string str = @"C:\Windows";
等价于:
string str = "C:\\Windows";
@ 字符串中可以任意换行,换行符及缩进空格都计算在字符串长度之内。
string str = @"<script type=""text/javascript""> <!-- --> </script>";
用户自定义引用类型有:class、interface 或 delegate。
指针类型(Pointer types)
指针类型变量存储另一种类型的内存地址。C# 中的指针与 C 或 C++ 中的指针有相同的功能。
声明指针类型的语法:
type* identifier;
例如:
char* cptr;
int* iptr;
边栏推荐
- Development to testing: a six-year road to automation from scratch
- day07-
- matlab 画短时能量图
- 正规方程法(Normal Equation)原理以及与梯度下降法的区别
- Technology sharing | do you know the functions of the server interface automated testing and requests library?
- Implementation of MATLAB short-time autocorrelation
- Go----Go 语言中的标识符和关键字
- Overview of MPLS Basics
- 方法重载与方法重写
- js验证复杂密码
猜你喜欢

Thorough load balancing

matlab 基音周期估计后处理

Just one dependency to give swagger a new skin, which is simple and cool~
![[mysql]substr usage - query the value of specific digits of a field in the table](/img/d5/68658ff15f204dc97abfe7c9e6b354.png)
[mysql]substr usage - query the value of specific digits of a field in the table

JDBC总结

LDAP——实现用户统一登录管理

Logical vulnerability - arbitrary account registration

Isilon's onefs common operation commands (I)

6、 Wechat applet release process

Altium designer 22 Chinese character garbled
随机推荐
When deploying Flink on a single machine and creating the connection table of oracle19c RAC, the error ora-12505 is reported. Who can help
Search Yiwu shopping (PAI Li Tao) API by image
测试员:“我有五年测试经验”HR: “不,你只是把一年的工作经验用了五年”
新来个技术总监要我做一个 IP 属地功能~
Pytoch -- used by visdom
JS 延迟执行window.onload
开发转测试:从零开始的6年自动化之路
Altium Designer 22 修改选中元件的层属性
matlab 短时自相关实现
A friend with a monthly salary of 50000 told me that you were just doing chores
一篇让小百彻底搞懂性能调优
Happens-Before原则深入解读
Highlight the secondary and tertiary columns under the primary column of pbootcms
VB.net Chart1的处理
京东一面:Redis 如何实现库存扣减操作?如何防止商品被超卖?
Flash source code startup phase
Logical vulnerability - arbitrary account registration
也谈数据治理
会用redis吗?那还不快来了解下redis protocol
JDBC summary