3.1 数据类型概述
C# 是强类型语言,每个变量都必须有明确的类型。C# 类型分为两大类:
值类型(Value Types)
直接存储数据,分配在栈上:
- 整数类型:
sbyte, byte, short, ushort, int, uint, long, ulong
- 浮点类型:
float, double, decimal
- 字符类型:
char
- 布尔类型:
bool
- 枚举:
enum
- 结构体:
struct
引用类型(Reference Types)
存储对象的引用,分配在堆上:
string(字符串)
object(所有类型的基类)
dynamic(动态类型)
- 类(
class)
- 接口(
interface)
- 数组(
array)
- 委托(
delegate)
3.2 整数类型
| 类型 |
大小 |
范围 |
示例 |
sbyte |
8 位 |
-128 ~ 127 |
sbyte a = -50; |
byte |
8 位 |
0 ~ 255 |
byte b = 200; |
short |
16 位 |
-32,768 ~ 32,767 |
short c = -1000; |
ushort |
16 位 |
0 ~ 65,535 |
ushort d = 50000; |
int |
32 位 |
-21 亿 ~ 21 亿 |
int e = 100000; |
uint |
32 位 |
0 ~ 42 亿 |
uint f = 3000000000u; |
long |
64 位 |
极大范围 |
long g = 10000000000L; |
ulong |
64 位 |
0 ~ 极大 |
ulong h = 10000000000UL; |
整数示例
1 2 3 4 5 6
| int age = 25; long population = 8_000_000_000L; uint positiveNumber = 100u;
Console.WriteLine($"年龄: {age}"); Console.WriteLine($"人口: {population}");
|
3.3 浮点类型
| 类型 |
大小 |
精度 |
示例 |
float |
32 位 |
7 位 |
float a = 3.14f; |
double |
64 位 |
15-16 位 |
double b = 3.14159; |
decimal |
128 位 |
28-29 位 |
decimal c = 3.141592653589793m; |
浮点示例
1 2 3 4 5 6 7 8 9 10 11 12
| float temperature = 36.6f;
double pi = 3.14159265358979;
decimal price = 19.99m; decimal tax = 0.08m; decimal total = price * (1 + tax);
Console.WriteLine($"总价: {total:C}");
|
3.4 字符与布尔
char 字符
1 2 3 4 5 6 7 8 9 10 11
| char letter = 'A'; char digit = '5'; char symbol = '$';
char heart = '\u2665'; char smile = '\u263A';
Console.WriteLine($"字符: {letter}"); Console.WriteLine($"Unicode: {heart}");
|
bool 布尔
1 2 3 4 5 6 7 8 9
| bool isAdult = true; bool isStudent = false;
int age = 20; bool canVote = age >= 18;
Console.WriteLine($"是否成年: {isAdult}"); Console.WriteLine($"可以投票: {canVote}");
|
3.5 string 字符串
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
| string name = "Alice"; string greeting = "Hello, World!";
string path = @"C:\Users\Alice\Documents"; string multiLine = @" 第一行 第二行 第三行 ";
int age = 25; string message = $"{name} 今年 {age} 岁";
string upper = name.ToUpper(); string lower = name.ToLower(); int length = name.Length; bool contains = name.Contains("lic");
|
3.6 变量声明
基本声明
1 2 3 4 5 6 7 8 9 10
| int x = 10; string name = "Bob";
int y; y = 20;
int a = 1, b = 2, c = 3;
|
var 隐式类型
1 2 3 4 5 6 7 8
| var number = 42; var text = "Hello"; var flag = true; var list = new List<int>();
|
常量
1 2 3 4 5 6
| const double PI = 3.14159265358979; const int MAX_SIZE = 100;
|
3.7 值类型与引用类型区别
值类型示例
1 2 3 4 5 6
| int a = 10; int b = a; b = 20;
Console.WriteLine(a); Console.WriteLine(b);
|
引用类型示例
1 2 3 4 5 6 7 8 9 10 11
| class Person { public string Name { get; set; } }
Person p1 = new Person { Name = "Alice" }; Person p2 = p1; p2.Name = "Bob";
Console.WriteLine(p1.Name); Console.WriteLine(p2.Name);
|
3.8 可空类型
值类型可空
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
| int? nullableInt = null; int? age = 25;
int? score = null;
if (score.HasValue) { Console.WriteLine(score.Value); }
int finalScore = score ?? 0;
int? length = score?.ToString().Length;
|
引用类型可空(.NET 8+)
1 2 3 4 5 6
| string? name = null; string text = "Hello";
int? len = name?.Length;
|
3.9 默认值
值类型默认值
1 2 3 4
| int defaultInt = default; double defaultDouble = default; bool defaultBool = default; char defaultChar = default;
|
引用类型默认值
1 2
| string defaultString = default; object defaultObject = default;
|
3.10 本章小结
- C# 类型分为值类型和引用类型
- 整数类型:
int 最常用,long 用于大数
- 浮点类型:
double 默认,decimal 用于金融
string 是引用类型,char 是值类型
var 让编译器推断类型,必须初始化
const 定义编译时常量
- 可空类型
T? 允许值为 null
练习题
- 声明变量存储你的姓名、年龄、身高
- 使用
decimal 计算商品总价(含税费)
- 尝试使用
var 声明各种类型
- 以下代码输出什么?
1 2 3 4
| int a = 10; int b = a; b = 20; Console.WriteLine(a);
|