*内容和&地址。&获取一个十进制的uint地址,而*获得地址的内容。
/// <summary>
/// 指针,存储的是一个地址的整数。
/// </summary>
class Program
{
delegate void Methods();
static void Main(string[] args)
{
Demo6();
Console.ReadKey();
}
static void S(object o)
{
Console.WriteLine(o.ToString());
}
static void S(params object[] o)
{
foreach (object obj in o)
S(obj);
}
#region 指针简单示例
unsafe static void Demo1()
{
int x = 10;//整数类型
int* pX, pY;//指针坐标X、Y
pX = &x;//取pX地址
pY = pX;//将pX赋值给pY
x = 15;
*pX = 16;
*pY = 17;
S(x);
S(*pX);//取得地址的内容
S(*pY);
}
#endregion
#region 指针的类型转换
unsafe static void Demo2()
{
//NOTE:类型转换后,uint获得的是地址的十进制格式,并非获取地址的内容。
int x = 10;//整数类型
int* pX, pY;//指针坐标X、Y
pX = &x;//取pX地址
pY = pX;//将pX的地址赋值给pY
uint ux = (uint) pX;//类型转换,获取地址的十进制
int* pZ = (int*) ux;//类型转换,将地址赋值给pZ
S(x);
S(((uint) pX).ToString("x"));//十六进制内存地址
S((uint) pY);//十进制内存地址
S(ux);
S((uint) pZ);//十进制内存地址
S(*pZ);//输出地址的内容
}
#endregion
#region DWORD内存块
unsafe static void Demo3()
{
decimal m2 = 4.00m;
byte b = 100;
short s = 200;
int i = 300;
long l = 400;
float f = 1.00f;
double d = 2.00;
decimal m = 3.00m;
S(string.Format("byte:{0}", sizeof(byte)));
S(string.Format("short:{0}", sizeof(short)));
S(string.Format("int:{0}", sizeof(int)));
S(string.Format("long:{0}", sizeof(long)));
S(string.Format("float:{0}", sizeof(float)));
S(string.Format("double:{0}", sizeof(double)));
S(string.Format("decimal:{0}", sizeof(decimal)));
S("");
S("从高到矮……");
S(string.Format("decimal:{0} +16 ↑", (uint) &m2));
S(string.Format("byte:{0} +1 ↑", (uint) &b));
S(string.Format("short:{0} +2 ↑", (uint) &s));
S(string.Format("int:{0} +4 ↑", (uint) &i));
S(string.Format("long:{0} +8 ↑", (uint) &l));
S(string.Format("float:{0} +4 ↑", (uint) &f));
S(string.Format("double:{0} +8 ↑", (uint) &d));
S(string.Format("decimal:{0} +16 ↑", (uint) &m));
S("有没有发现byte和short也是4个字节的内存块?因为.NET约定,最少要占用4个字节。");
}
#endregion
#region 指针的运算
unsafe static void Demo4()
{
byte b = 8;
uint u = 3;
double d = 10.0;
byte* pByte = &b;
uint* pUint = &u;
double* pDouble = &d;
S(string.Format("byte:{0}", (uint) pByte));
S(string.Format("uint:{0}", (uint) pUint));
S(string.Format("double:{0}", (uint) pDouble));
pByte -= 3;
++pUint;
S("\n");
double* pDouble2 = pDouble + 4;
S(string.Format("byte:{0}。old - 3 * 1", (uint) pByte));
S(string.Format("uint:{0}。old + 4 * 1", (uint) pUint));
S(string.Format("double2:{0}。pDouble + 4 * 8", (uint) pDouble2));
}
#endregion
#region 结构指针
unsafe static void Demo5()
{
MyStruct ms = new MyStruct();
MyStruct* pms = &ms;
(*pms).X = 5;//传统方式
pms->Y = 10;//与C++雷同方式
S(ms.X);
S(ms.Y);
int* X = &(pms->X);
S(*X);
*X = 15;
S(*X);
S(ms.X);
}
struct MyStruct
{
public int X;
public int Y;
}
#endregion
#region 类指针
unsafe static void Demo6()
{
MyClass mc = new MyClass();
//MyClass* pmc = &mc;//error,无法获取托管类型,因为它们嵌入一个对象。(结构是值类型)
fixed (int* X = &(mc.X))
fixed (int* Y = &(mc.Y))//*X和*Y固定或者fixed (int* X = &(mc.X), Q = &(mc.Y))。唯一限制,数据类型必须都是int*。
{
*X = 5;
*Y = 6;
S(mc.X + "<-X Y-> " + mc.Y);
fixed (int* Z = &(mc.Z))//*Z固定在X中。生命周期在于X、Y之间。
{
*Z = 7;
*X = 8;
*Y = 9;
S(mc.X + "<-X Y-> " + mc.Y + " Z->" + mc.Z);
}
*X = 10;
*Y = 11;
S(mc.X + "<-X Y-> " + mc.Y);
}
}
class MyClass
{
public int X;
public int Y;
public int Z;
}
#endregion
}
/// 指针,存储的是一个地址的整数。
/// </summary>
class Program
{
delegate void Methods();
static void Main(string[] args)
{
Demo6();
Console.ReadKey();
}
static void S(object o)
{
Console.WriteLine(o.ToString());
}
static void S(params object[] o)
{
foreach (object obj in o)
S(obj);
}
#region 指针简单示例
unsafe static void Demo1()
{
int x = 10;//整数类型
int* pX, pY;//指针坐标X、Y
pX = &x;//取pX地址
pY = pX;//将pX赋值给pY
x = 15;
*pX = 16;
*pY = 17;
S(x);
S(*pX);//取得地址的内容
S(*pY);
}
#endregion
#region 指针的类型转换
unsafe static void Demo2()
{
//NOTE:类型转换后,uint获得的是地址的十进制格式,并非获取地址的内容。
int x = 10;//整数类型
int* pX, pY;//指针坐标X、Y
pX = &x;//取pX地址
pY = pX;//将pX的地址赋值给pY
uint ux = (uint) pX;//类型转换,获取地址的十进制
int* pZ = (int*) ux;//类型转换,将地址赋值给pZ
S(x);
S(((uint) pX).ToString("x"));//十六进制内存地址
S((uint) pY);//十进制内存地址
S(ux);
S((uint) pZ);//十进制内存地址
S(*pZ);//输出地址的内容
}
#endregion
#region DWORD内存块
unsafe static void Demo3()
{
decimal m2 = 4.00m;
byte b = 100;
short s = 200;
int i = 300;
long l = 400;
float f = 1.00f;
double d = 2.00;
decimal m = 3.00m;
S(string.Format("byte:{0}", sizeof(byte)));
S(string.Format("short:{0}", sizeof(short)));
S(string.Format("int:{0}", sizeof(int)));
S(string.Format("long:{0}", sizeof(long)));
S(string.Format("float:{0}", sizeof(float)));
S(string.Format("double:{0}", sizeof(double)));
S(string.Format("decimal:{0}", sizeof(decimal)));
S("");
S("从高到矮……");
S(string.Format("decimal:{0} +16 ↑", (uint) &m2));
S(string.Format("byte:{0} +1 ↑", (uint) &b));
S(string.Format("short:{0} +2 ↑", (uint) &s));
S(string.Format("int:{0} +4 ↑", (uint) &i));
S(string.Format("long:{0} +8 ↑", (uint) &l));
S(string.Format("float:{0} +4 ↑", (uint) &f));
S(string.Format("double:{0} +8 ↑", (uint) &d));
S(string.Format("decimal:{0} +16 ↑", (uint) &m));
S("有没有发现byte和short也是4个字节的内存块?因为.NET约定,最少要占用4个字节。");
}
#endregion
#region 指针的运算
unsafe static void Demo4()
{
byte b = 8;
uint u = 3;
double d = 10.0;
byte* pByte = &b;
uint* pUint = &u;
double* pDouble = &d;
S(string.Format("byte:{0}", (uint) pByte));
S(string.Format("uint:{0}", (uint) pUint));
S(string.Format("double:{0}", (uint) pDouble));
pByte -= 3;
++pUint;
S("\n");
double* pDouble2 = pDouble + 4;
S(string.Format("byte:{0}。old - 3 * 1", (uint) pByte));
S(string.Format("uint:{0}。old + 4 * 1", (uint) pUint));
S(string.Format("double2:{0}。pDouble + 4 * 8", (uint) pDouble2));
}
#endregion
#region 结构指针
unsafe static void Demo5()
{
MyStruct ms = new MyStruct();
MyStruct* pms = &ms;
(*pms).X = 5;//传统方式
pms->Y = 10;//与C++雷同方式
S(ms.X);
S(ms.Y);
int* X = &(pms->X);
S(*X);
*X = 15;
S(*X);
S(ms.X);
}
struct MyStruct
{
public int X;
public int Y;
}
#endregion
#region 类指针
unsafe static void Demo6()
{
MyClass mc = new MyClass();
//MyClass* pmc = &mc;//error,无法获取托管类型,因为它们嵌入一个对象。(结构是值类型)
fixed (int* X = &(mc.X))
fixed (int* Y = &(mc.Y))//*X和*Y固定或者fixed (int* X = &(mc.X), Q = &(mc.Y))。唯一限制,数据类型必须都是int*。
{
*X = 5;
*Y = 6;
S(mc.X + "<-X Y-> " + mc.Y);
fixed (int* Z = &(mc.Z))//*Z固定在X中。生命周期在于X、Y之间。
{
*Z = 7;
*X = 8;
*Y = 9;
S(mc.X + "<-X Y-> " + mc.Y + " Z->" + mc.Z);
}
*X = 10;
*Y = 11;
S(mc.X + "<-X Y-> " + mc.Y);
}
}
class MyClass
{
public int X;
public int Y;
public int Z;
}
#endregion
}
实现字符串倒置:
public static string RevertString2(string src)
{
char tmp;
unsafe
{
fixed (char* ps = src)
{
for (int i = 0; i < src.Length / 2; i++)
{
tmp = ps[i];
ps[i] = ps[src.Length - i - 1];
ps[src.Length - i - 1] = tmp;
}
}
}
return src;
}
{
char tmp;
unsafe
{
fixed (char* ps = src)
{
for (int i = 0; i < src.Length / 2; i++)
{
tmp = ps[i];
ps[i] = ps[src.Length - i - 1];
ps[src.Length - i - 1] = tmp;
}
}
}
return src;
}