首頁  >  文章  >  後端開發  >  C# 中的指針

C# 中的指針

王林
王林原創
2024-09-03 15:06:41419瀏覽

指標被定義為包含另一個變數的記憶體位址的變數。 C# 中的指標在有不安全且由 unsafe 關鍵字標記的語句時使用。這些類型的語句不受垃圾收集器的控制並使用指標變數。

語法: 指標可以宣告為

type  *var name;
int* a;

這裡 * 稱為解引用運算符,a 是包含 int 型別位址的變數。

範例

int *p = & x;    // where &x is the memory address of x
Console.WriteLine((int)p) // displaying memory address
Console.WriteLine(*p) // displaying value at memory address

指標在 C# 中如何運作?

下面的範例展示了它在 C# 中的工作原理。

C# 中的指標 – 範例#1

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace Pointers
{
class Demo
{
public void Method()
{
unsafe
{
int a = 40;
int b = 20;
int* ptr1 = &a;
int* ptr2 = &b;
Console.WriteLine(*ptr1);    // displaying the value
Console.WriteLine(*ptr2);    // displaying the value
Console.WriteLine((int)ptr1); // displaying the address
Console.WriteLine((int)ptr2); // displaying the address
}
}
}
class Example
{
// main method
public static void Main()
{
Demo d = new Demo();
d.Method();
}
}
}

有不同的方法可以執行不安全的語句,如修飾符、建構子等。在上面的範例中,一組語句被標記為不安全。在上面的程式碼中,有兩個變數a和b,其值分別為40和20,指標包含它們的位址。 Console.WriteLine() 用來顯示變數的值和位址。

輸出:

C# 中的指針

C# 中的指標 – 範例#2

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace Pointers
{
class Demo
{
public unsafe void Method()
{
int a = 50;
int b = 20;
int* ptr1 = &a;
int* ptr2 = &b;
Console.WriteLine(*ptr1);       // displaying the value
Console.WriteLine(*ptr2);       // displaying the value
Console.WriteLine((int)ptr1);   // displaying the address
Console.WriteLine((int)ptr2);   // displaying the address
}
}
class Example
{
// main method
public static void Main()
{
Demo d = new Demo();
d.Method();
}
}
}

在上面的範例中,unsafe 與有兩個變數 a 和 b 的方法一起使用,其值分別為 50 和 20。指標 *ptr1 和 *ptr2 指向它們的記憶體位址。

輸出:

C# 中的指針

C# 中的指標 – 範例#3

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace Pointers
{
class Demo
{
public unsafe static void Main()
{
int[] array = { 10, 20, 30, 40, 50 };    // declaring array
fixed (int* ptr = array)                 // fixed for pinning the object
/* let us have array address in pointer */
for (int i = 0; i < 5; i++)
{
Console.WriteLine("Value of array[{0}]={1}", i, *(ptr + i));
Console.WriteLine("Address of array[{0}]={1}", i, (int)(ptr + i));
Console.ReadKey();
}
}
}
}

在上面的程式碼中,定義了一個由五個元素組成的數組,並使用 Console.WriteLine() 顯示數組元素的值和數組元素的位址。 C# 中有一個概念,稱為物件的固定。上面的程式碼中,使用了一個fixed語句來固定對象,這樣垃圾收集器就不會讓物件移動並「固定」它。可能會影響運作效率。

輸出:

C# 中的指針

C# 中的指標 – 範例 #4

using System;
namespace Pointers
{
// Struct employee
struct Employee
{
// members
// employee id and salary
public int empid;
public double salary;
// Constructor to initialize values
public Employee(int e, double s)
{
empid = e;
salary = s;
}
}; // end of struct
class Program
{
// Main Method
static void Main(string[] args)
{
// unsafe so as to use pointers
unsafe
{
// Declaring two employee Variables
Employee E1 = new Employee(798, 30000);
Employee E2 = new Employee(799, 31000);
// Declaring two employee pointers
// and initializing them with addresses
// of E1 and E2
Employee* E1_ptr = &E1;
Employee* E2_ptr = &E2;
// Displaying details of employees using pointers
// Using  the arrow ( -> ) operator
Console.WriteLine("Details of Employee 1");
Console.WriteLine("Employee Id: {0} Salary: {1}",
E1_ptr->empid, E1_ptr->salary);
Console.WriteLine("Details of Employee 2");
Console.WriteLine("Employee Id: {0} Salary: {1}",
E2_ptr->empid, E2_ptr->salary);
} // end unsafe
} // end main
} // end class
}

在上面的範例中,employee 結構體包含成員員工 ID 和薪水,並參數化建構子來初始化值。指標指向包含原始值類型的結構,而不是包含參考類型的結構。在main方法中,有兩個員工變數和員工指針,分別用位址E1和E2進行初始化。 Console.WriteLine() 用於使用指標顯示員工的詳細資料。

輸出:

C# 中的指針

C# 中的指標 – 範例 #5

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace Pointers
{
class Demo
{
public static void Main()
{
unsafe
{
int* arr = stackalloc int[6];   // declaring array
arr[0] = 10;
arr[1] = 20;
arr[2] = 30;
arr[3] = 40;
arr[4] = 50;
arr[5] = 60;
for (int i = 0; i < 6; i++)
{
Console.WriteLine($"Value at {i}: {arr[i]}");
Console.ReadKey();
}
}
}
}
}

上面的程式碼中使用了stackalloc關鍵字,即在堆疊上分配記憶體。堆疊區塊上執行的記憶體是在方法執行期間建立的。 stackalloc性能更好,而且不需要pin數組。它比堆分配的數組更好,因為不需要釋放它,因為它會在方法返回時自動釋放。

輸出:

C# 中的指針

在指標中,轉換有隱式和顯式型別。隱式類型轉換就像任何指標類型到 void* 類型以及 null 到任何指標類型。在明確類型中,轉換是從 byte、sbyte、ushort、short、uint、int、ulong、long 到任何指標類型,反之亦然,以及一個指標到另一個指標。

結論 – C# 中的指標

因此指標用於指向記憶體位址並使用不安全的語句程式碼執行它們。它僅在非託管環境中使用,並且不會被垃圾收集器追蹤。指標用於堆疊、佇列等

以上是C# 中的指針的詳細內容。更多資訊請關注PHP中文網其他相關文章!

陳述:
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn
上一篇:C# 物件到字典下一篇:C# 物件到字典