前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >C# 关键字与基本数据类型

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

作者头像
小嗷犬
发布2022-11-15 15:32:41
2830
发布2022-11-15 15:32:41
举报

✅作者简介:人工智能专业本科在读,喜欢计算机与编程,写博客记录自己的学习历程。 🍎个人主页:小嗷犬的博客 🍊个人信条:为天地立心,为生民立命,为往圣继绝学,为万世开太平。 🥭本文内容:C# 关键字与基本数据类型


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


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 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 方法可以得到数据类型在当前环境下的准确尺寸,返回值为 int 类型,单位为字节

代码语言:javascript
复制
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));
        }
    }
}
本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2022-11-08,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • C# 关键字与基本数据类型
  • 1.关键字
  • 2.基本数据类型
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档