Csharp/CSharp Tutorial/struct/struct unsafe code — различия между версиями
Admin (обсуждение | вклад) м (1 версия) |
(нет различий)
|
Текущая версия на 15:17, 26 мая 2010
Access members via ->
When you access a member of a structure through a pointer, you must use the arrow operator, which is ->, rather than the dot (.) operator.
<source lang="csharp">using System;
using System.Globalization;
struct Point
{
public int x; public int y; public override string ToString() { return "(" + x + "," + y + ")"; }
}
public class MainClass{
static void Main(string[] args) { Console.WriteLine("Access members via ->"); unsafe { Point point; Point* p = &point; p->x = 100; p->y = 200; Console.WriteLine(p->ToString()); } }
}</source>
Access members via -> (100,200)
struct with unsafe code(pointer)
<source lang="csharp">using System; using System.Globalization; public struct Node {
public int Value; public unsafe Node* Left; public unsafe Node* Right;
} public class MainClass{
static void Main(string[] args) { Node n = new Node(); n.Value = 9; }
}</source>
Use pointer indirection and . operator
<source lang="csharp">using System; using System.Globalization; struct Point {
public int x; public int y; public override string ToString() { return "(" + x + "," + y + ")"; }
}
public class MainClass{
static void Main(string[] args) { Console.WriteLine("Access members via."); unsafe { Point point; Point* p = &point; (*p).x = 100; (*p).y = 200; Console.WriteLine((*p).ToString()); } }
}</source>
Access members via. (100,200)