C# 关键字与基本数据类型

简介: 编译器预定义的保留字。这些关键字不能用作标识符,但是,如果您想使用这些关键字作为标识符,可以在关键字前面加上。方法可以得到数据类型在当前环境下的准确尺寸,返回值为。中,有些关键字在代码的上下文中有特殊的意义,如。(Reserved Keywords)和。值类型变量可以直接存储对应数据。下面我们将为大家介绍最基本的。
✅作者简介:人工智能专业本科在读,喜欢计算机与编程,写博客记录自己的学习历程。
🍎个人主页: 小嗷犬的博客
🍊个人信条:为天地立心,为生民立命,为往圣继绝学,为万世开太平。
🥭本文内容:C# 关键字与基本数据类型

@TOC


1.关键字

关键字C# 编译器预定义的保留字。这些关键字不能用作标识符,但是,如果您想使用这些关键字作为标识符,可以在关键字前面加上 @ 字符作为前缀。

C# 中,有些关键字在代码的上下文中有特殊的意义,如 getset,这些被称为上下文关键字(Contextual keywords)。

下表列出了 C# 中的保留关键字(Reserved Keywords)和上下文关键(Contextual Keywords):

保留关键字:

1 2 3 4 5 6 7
abstract as base bool break byte case
catch char checked class const continue decimal
default delegate do double else enum event
explicit extern false finally fixed float for
foreach goto if implicit in in (genericmodifier) int
interface internal is lock long namespace new
null object operator out out(genericmodifier) override params
private protected public readonly ref return sbyte
sealed short sizeof stackalloc static string struct
switch this throw true try typeof uint
ulong unchecked unsafe ushort using virtual void
volatile while

上下文关键字:

1 2 3 4 5 6 7
add alias ascending descending dynamic from get
global group into join let orderby partial(type)
partial(method) remove select set

2.基本数据类型

C# 中变量有以下3种类型:

  • 值类型(Value types)
  • 引用类型(Reference types)
  • 指针类型(Pointer types)

下面我们将为大家介绍最基本的 值类型(Value types)

值类型

值类型是从类 System.ValueType 中派生的。

值类型变量可以直接存储对应数据。比如 intcharfloat,它们分别存储整数字符浮点数

下表列出了 C# 中可用的值类型:

类型 描述 范围 默认值
bool 布尔值 True 或 False False
byte 8 位无符号整数 0 到 255 0
char 16 位 Unicode 字符 U +0000 到 U +ffff '\0'
decimal 128 位精确的十进制值,28-29 有效位数 (-7.9 x 10^28^ 到 7.9 x 10^28^) / 10^0~28^ 0.0M
double 64 位双精度浮点型 (+/-)5.0 x 10^-324^ 到 (+/-)1.7 x 10^308^ 0.0D
float 32 位单精度浮点型 -3.4 x 10^38^ 到 + 3.4 x 10^38^ 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 方法可以得到数据类型在当前环境下的准确尺寸,返回值为 int 类型,单位为 字节
using System;

namespace DataTypeApplication
{
    class Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine("Size of int: {0}", sizeof(int));
            Console.WriteLine("Size of bool: {0}", sizeof(bool));
            Console.WriteLine("Size of char: {0}", sizeof(char));
            Console.WriteLine("Size of float: {0}", sizeof(float));
        }
    }
}
目录
相关文章
|
2月前
|
C#
C#学习相关系列之数据类型类的三大特性(二)
C#学习相关系列之数据类型类的三大特性(二)
|
2月前
|
存储 C#
C#数据类型之结构体介绍
C#数据类型之结构体介绍
|
2月前
|
C#
C#数据类型之枚举类型
C#数据类型之枚举类型
|
2月前
|
存储 编译器 数据处理
C#基础入门之数据类型
C#基础入门之数据类型
|
2月前
|
C#
30.C# 关键字 this初步学习
30.C# 关键字 this初步学习
15 1
|
2月前
|
C#
28.c#关键字base初步学习
28.c#关键字base初步学习
12 0
|
2月前
|
C#
27.c#关键字sealed修饰类
27.c#关键字sealed修饰类
12 0
|
3月前
|
SQL 开发框架 .NET
EntityFramework数据持久化复习资料3、C#拓展方法与yield关键字使用
EntityFramework数据持久化复习资料3、C#拓展方法与yield关键字使用
23 0
|
4月前
|
开发框架 .NET 编译器
C# 9.0中的静态匿名函数:引入static关键字的新用法
【1月更文挑战第15天】C# 9.0为匿名函数带来了一个新的修饰符static,允许开发者明确指定匿名函数不会捕获其包含作用域中的任何变量。这一特性增强了代码的性能和可读性,同时减少了因不小心捕获变量而导致的潜在错误。本文将详细探讨C# 9.0中静态匿名函数的语法、使用场景以及它们如何影响代码的性能和安全性。
|
5月前
|
存储 C# 图形学
【Unity 3D】C#数据类型和变量、命名规范的讲解(附源码)
【Unity 3D】C#数据类型和变量、命名规范的讲解(附源码)
59 1