(五十五)C#编程基础复习——C#指针变量与unsafe
为了保持类型的安全性,默认情况下C#是不支持指针的,但是如果使用unsafe关键字来修饰类或类中的成员,这样的类或类中的成员就会被视为不安全代码,C#允许在不安全代码中使用指针变量。在公共语言运行时(CLR)中,不安全代码是指无法验证的代码,不安全代码不一定是危险的,只是公共语言运行时(CLR)无法验证代码的安全性。因此CLR仅会执行新程序集中包含的不安全代码。
一、指针变量
在C#中,指针同样是一个变量,但是它的值是另一个变量的内存地址,在使用指针之前我们同样需要先声明指针,声明指针的语法格式如下:
type * var_name;
下表中列举了一些定义指针的示例:
与声明变量相同,我们同样可以在一行代码中同时声明多个指针,如下所示:
int * p1,p2,p3;//定义p1、p2、p3三个整数指针
注意:指针类型不能从对象中继承,并且装箱和拆箱也不支持指针,但是不同的指针类型以及指针与整数之间可以进行转换。
示例如下:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace _051
{
internal class Program
{
static unsafe void Main(string[] args)
{
double f = 3.1415;
double* p = &f;
Console.WriteLine("数据的内容是:{0}",f);
Console.WriteLine("数据在内存中的地址是:{0}", (int)p) ;
Console.ReadKey();
}
}
}
二、使用指针检索数据的值
在C#中,我们可以使用ToString()来获取指针变量所指向的数据的值,如下所示:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace _051
{
internal class Program
{
static unsafe void Main(string[] args)
{
unsafe
{
int var = 12345;
int* p = &var;
Console.WriteLine("变量var的值为:{0}",var);
Console.WriteLine("指针p指向的值为:{0}", p->ToString()) ;
Console.WriteLine("指针p的值为:{0}",(int)p);
}
Console.ReadKey();
}
}
}
三、将指针作为参数传递给函数
我们可以将指针变量作为参数传递给函数,示例如下:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace _052
{
internal class Program
{
public unsafe void swap(int* p,int *q)
{
int temp = *p;
*p = *q;
*q = temp;
}
static unsafe void Main(string[] args)
{
Program p = new Program();
int var1 = 10;
int var2 = 20;
int* x = &var1;
int* y = &var2;
Console.WriteLine("调用Swap的函数前:var1:{0},var2:{1}",var1,var2);
p.swap(x, y);
Console.WriteLine("调用Swap的函数后:var1:{0},var2:{1}",var1,var2);
Console.ReadKey();
}
}
}
四、使用指针访问数组元素
在C#中,数组和指向该数组且与数组名称相同的指针是不同的数据类型,例如int* p和int [] p就是不同的数据类型。你可以增加指针变量p的值,因为它在内存中不是固定的,但数组地址在内存中是固定的,因此你不能增加数组p的值。如果你需要使用指针变量访问数组数据,可以像我们在C或C++中所做的那样,使用fixed关键字来固定指针。下面通过示例演示下:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace _053
{
internal class Program
{
static unsafe void Main(string[] args)
{
int[] list = { 10, 100, 200 };
fixed (int* ptr = list)
/*显示指针中数组地址*/
for (int i = 0; i < 3; i++)
{
Console.WriteLine("list[{0}]的内存地址为:{1}", i, (int)(ptr + i));
Console.WriteLine("list[{0}]的值为:{1}",i,*(ptr+i));
}
Console.ReadKey();
}
}
}
五、编译不安全代码
为了编译不安全代码,在编译时必须使用unsafe命令,例如编译包含不安全代码的demo.cs程序的命令如下:
csc /unsafe demo.cs
或
csc -unsafe demo.cs
如果您使用的是 Visual Studio,那么您需要在项目属性中启用不安全代码,具体步骤如下:
- 通过双击资源管理器(Solution Explorer)中的属性(properties)节点,打开项目属性(project properties);
- 点击 Build 标签页;
- 选择选项“Allow unsafe code”。
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】凌霞软件回馈社区,博客园 & 1Panel & Halo 联合会员上线
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】博客园社区专享云产品让利特惠,阿里云新客6.5折上折
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· DeepSeek “源神”启动!「GitHub 热点速览」
· 我与微信审核的“相爱相杀”看个人小程序副业
· 上周热点回顾(2.17-2.23)
· 如何使用 Uni-app 实现视频聊天(源码,支持安卓、iOS)
· C# 集成 DeepSeek 模型实现 AI 私有化(本地部署与 API 调用教程)