這個東西,沒理解前感覺很難,理解了覺得簡單。個人感覺就1點:*內容和&地址。&獲取一個十進位制的uint地址,而*獲得地址的內容。
Code
<!--
Code highlighting produced by Actipro CodeHighlighter (freeware)
http://www.CodeHighlighter.com/
--> ///
/// 指標,儲存的是一個地址的整數。
///
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 + " " + mc.Y);
fixed (int* Z = &(mc.Z))//*Z固定在X中。生命週期在於X、Y之間。
{
*Z = 7;
*X = 8;
*Y = 9;
S(mc.X + " " + mc.Y + " Z->" + mc.Z);
}
*X = 10;
*Y = 11;
S(mc.X + " " + mc.Y);
}
}
class MyClass
{
public int X;
public int Y;
public int Z;
}
#endregion
<!--
Code highlighting produced by Actipro CodeHighlighter (freeware)
http://www.CodeHighlighter.com/
--> ///
/// 指標,儲存的是一個地址的整數。
///
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 + " " + mc.Y);
fixed (int* Z = &(mc.Z))//*Z固定在X中。生命週期在於X、Y之間。
{
*Z = 7;
*X = 8;
*Y = 9;
S(mc.X + " " + mc.Y + " Z->" + mc.Z);
}
*X = 10;
*Y = 11;
S(mc.X + " " + mc.Y);
}
}
class MyClass
{
public int X;
public int Y;
public int Z;
}
#endregion