序号 | 系列文章 |
---|---|
0 | 【C#基础】初识编程语言C# |
1 | 【C#基础】C# 程序结构 |
2 | 【C#基础】C# 基础语法 |
😄大家好,我是writer桑,前面一章已经学习了C#的基础语法,那本章就开始学习C#程序的数据类型,希望看完大家能够有所收获,感谢支持!
C# 是一种强类型语言。 每个变量和常量都有一个类型,每个求值的表达式也是如此。每个方法声明都为每个输入参数和返回值指定名称、类型和种类(值、引用或输出)。C# 分别以值类型和引用类型作为类型的两个主要类别。值类型的变量直接包含它们的数据。引用类型的变量存储对数据(称为“对象”)的引用。
值类型的变量包含类型的实例,不同于引用类型的变量包含对类型实例的引用。默认情况下,在分配中,通过将实参传递给方法并返回方法结果来复制变量值。 对于值类型,每个变量都具有其自己的数据副本,对一个变量执行的操作不会影响另一个变量(ref 和 out 参数变量除外)。
代码理解:
using System;public struct MutablePoint
{public int X, Y;public MutablePoint(int x, int y) => (X, Y) = (x, y);public override string ToString() => $"({X}, {Y})";
}public class Program
{public static void Main(string[] args){var p1 = new MutablePoint(1, 2);var p2 = p1;Console.WriteLine($"{nameof(p1)} after {nameof(p2)} is modified:{p1} ");Console.WriteLine($"{nameof(p2)}: {p2}");MutateAndDisplay(p2);Console.WriteLine($"{nameof(p2)} after passing to a method: {p2}");}private static void MutateAndDisplay(MutablePoint p){p.X = 100;Console.WriteLine($"Point mutated in a method: {p}");}
}/*
* 预期的输出:
* p1 after p2 is modified:(1, 2)
* p2: (1, 2)
* Point mutated in a method: (100, 2)
* p2 after passing to a method: (1, 2)
*/
下表列出了 C# 11 中值类型的简单类型:
类型/关键字 | 描述 | 范围 | 默认值 | .NET类型 |
---|---|---|---|---|
bool | 布尔值 | True 或 False | False | System.Boolean |
char | 16位 Unicode 字符 | U+0000 到 U+FFFF | ‘\0’ | System.Char |
float | 32 位单精度浮点型 | ±1.5 x 10-45 至 ±3.4 x 1038 | 0.0F | System.Single |
double | 64 位双精度浮点型 | ±5.0 × 10 -324 至±1.7 × 10308 | 0.0D | System.Double |
decimal | 128 位精确的十进制值,28-29 有效位数 | ±1.0 x 10-28 至7.9228 x 10^28 | 0.0M | System.Decimal |
sbyte | 8 位有符号整数类型 | -128 到 127 | 0 | System.SByte |
byte | 无符号的 8 位整数 | 0 到 255 | 0 | System.Byte |
short | 有符号 16 位整数 | -32,768 到 32,767 | 0 | System.Int16 |
ushort | 无符号 16 位整数 | 0 到 65,535 | 0 | System.UInt16 |
int | 带符号的 32 位整数 | -2,147,483,648 到 2,147,483,647 | 0 | System.Int32 |
uint | 带符号的 32 位整数 | 0 到 4,294,967,295 | 0 | System.UInt32 |
long | 64 位带符号整数 | -9,223,372,036,854,775,808 到 9,223,372,036,854,775,807 | 0L | System.Int64 |
ulong | 无符号 64 位整数 | 0 到 18,446,744,073,709,551,615 | 0 | System.UInt64 |
nint | 带符号的 32 位或 64位整数 | 取决于(在运行时计算的)平台 | 0 | System.IntPtr |
nuint | 带符号的 32 位或 64 位整数 | 取决于(在运行时计算的)平台 | 0 | System.UIntPtr |
除了上述值类型的简单类型之外,C# 程序还包括以下用关键字声明的值类型种类:
enum Season
{Spring,Summer,Autumn,Winter
}
public struct MutablePoint
{public int X, Y;public MutablePoint(int x, int y) => (X, Y) = (x, y);public override string ToString() => $"({X}, {Y})";
}
(double, int) t1 = (4.5, 3);
Console.WriteLine($"Tuple with elements {t1.Item1} and {t1.Item2}.");
// Output:
// Tuple with elements 4.5 and 3.(double Sum, int Count) t2 = (4.5, 3);
Console.WriteLine($"Sum of {t2.Count} elements is {t2.Sum}.");
// Output:
// Sum of 3 elements is 4.5.
// 值类型可隐式转换为相应的可为空的值类型
double? pi = 3.14;
char? letter = 'a';int m2 = 10;
int? m = m2;bool? flag = null;// 可空值类型的数组:
int?[] arr = new int?[10];
一些针对值类型的常用操作:
Console.WriteLine($"size of nint = {sizeof(nint)}");
Console.WriteLine($"size of nuint = {sizeof(nuint)}");// 在64位进程中运行时输出
//size of nint = 8
//size of nuint = 8// 在32位进程中运行时输出
//size of nint = 4
//size of nuint = 4
Console.WriteLine($"nint.MinValue = {nint.MinValue}");
Console.WriteLine($"nint.MaxValue = {nint.MaxValue}");//nint.MinValue = -9223372036854775808
//nint.MaxValue = 9223372036854775807
// 隐式数据转换举例:int 转换为 long
int a = 11;
long b = a; // 显示数据转换:long 转换为 int
int a1 = 123;
long b1 = a;
int c = (int)b1;
Console.WriteLine(default(bool)); // False
bool b = false;
Console.WriteLine(b.GetType()); // System.Boolean
引用类型的变量存储对其数据(对象)的引用,不同于值类型的变量直接包含其数据。 对于引用类型,多个变量可同时引用同一对象,多个变量之间互相传递的也是对这个对象的引用;因此,对一个变量执行的操作会影响另一个变量所引用的对象(这点区别于值类型)。
代码理解:
using System;// 类为引用类型
public class MutablePoint
{public int X, Y;public MutablePoint(int x, int y) => (X, Y) = (x, y);public override string ToString() => $"({X}, {Y})";
}public class Program
{public static void Main(string[] args){var p1 = new MutablePoint(1, 2);var p2 = p1;Console.WriteLine($"{nameof(p1)} after {nameof(p2)} is modified:{p1} ");Console.WriteLine($"{nameof(p2)}: {p2}");MutateAndDisplay(p2); // 显示和改变 Console.WriteLine($"{nameof(p2)} after passing to a method: {p2}");}private static void MutateAndDisplay(MutablePoint p){p.X = 100;Console.WriteLine($"Point mutated in a method: {p}");}
}/*
* 预期的输出:
* p1 after p2 is modified:(1, 2)
* p2: (1, 2)
* Point mutated in a method: (100, 2)
* p2 after passing to a method: (100, 2)
*/
以下表格列举出了 C# 内置引用类型:
类型/关键字 | 描述 | .NET 类型 |
---|---|---|
object | 对象类型 | System.Object |
string | 字符串类型 | System.String |
dynamic | 动态类型 | System.Object |
除了上述内置的引用类型之外,C# 程序还包括以下用关键字声明的引用类型:
// 声明 record class 引用类型:
public record Person
{public string FirstName { get; init; } = default!;public string LastName { get; init; } = default!;
};// 声明 record struct 值类型:
public record struct Point
{public double X { get; init; }public double Y { get; init; }
}
class TestClass
{//方法,属性,字段,事件,委托//和嵌套类到这里。
}
// 定义接口 ISampleInterface
interface ISampleInterface
{void SampleMethod();
}// 类 Program 继承接口并实现
public class Program : ISampleInterface
{public void SampleMethod(){throw new NotImplementedException();}
}
string notNull = "Hello";
string? nullable = default;
notNull = nullable!; // 给定为空值
一些针对引用类型的常用操作:
//将整型变量i进行了装箱并分配给对象o。
int i = 123;
object o = i;// 将对象o拆箱并分配给对象i;
i = (int)o;Console.WriteLine(i); // 123
string s1 = "hello, world";
string s2 = "hello, C#";// 定义相等运算符 == 和 != 比较 string 对象之间是否相等:
Console.WriteLine(s1 == s2); // False // 定义 + 连接运算符连接两个字符串:
Console.WriteLine(s1 + s2); // hello, worldhello, C# // 定义 [] 运算符可访问字符串指定位置的字符:
Console.WriteLine(s1[0]); // h
dynamic d = 20; // 运行时进行类型检查
C# 程序中在不安全的上下文中,类型除了是值类型或引用类型外,还可以是指针类型 , 通过指针类型可以直接操作对象的内存,指针类型声明采用下列形式之一:
//type* identifier;
void* identifier; // 允许但不建议 // 又例如:
char* cptr;
int* iptr;
(ps:指针类型不在本章进行详细的讨论。)
✔️ 以上就是 C# 数据类型的介绍,希望能够对大家有所帮助。望大家多多支持,你们的支持就是笔者创作最大的动力!