/*
C#: The Complete Reference
by Herbert Schildt
Publisher: Osborne/McGraw-Hill (March 8, 2002)
ISBN: 0072134852
*/
/* An array name with an index yields a pointer to the
start of the array. */
using System;
public class PtrArray {
unsafe public static void Main() {
int[] nums = new int[10];
fixed(int* p = &nums[0], p2 = nums) {
if(p == p2)
Console.WriteLine("p and p2 point to same address.");
}
}
}
Demonstrate pointer comparison
/*
C#: The Complete Reference
by Herbert Schildt
Publisher: Osborne/McGraw-Hill (March 8, 2002)
ISBN: 0072134852
*/
// Demonstrate pointer comparison.
using System;
public class PtrCompDemo {
unsafe public static void Main() {
int[] nums = new int[11];
int x;
// find the middle
fixed (int* start = &nums[0]) {
fixed(int* end = &nums[nums.Length-1]) {
for(x=0; start+x <= end-x; x++) ;
}
}
Console.WriteLine("Middle element is " + x);
}
}
[/csharp]
Demonstrate the effects of pointer arithmethic
/*
C#: The Complete Reference
by Herbert Schildt
Publisher: Osborne/McGraw-Hill (March 8, 2002)
ISBN: 0072134852
*/
// Demonstrate the effects of pointer arithmethic.
using System;
public class PtrArithDemo {
unsafe public static void Main() {
int x;
int i;
double d;
int* ip = &i;
double* fp = &d;
Console.WriteLine(“int double
“);
for(x=0; x < 10; x++) { Console.WriteLine((uint) (ip) + " " + (uint) (fp)); ip++; fp++; } } } [/csharp]
Demonstrate fixed
/*
C#: The Complete Reference
by Herbert Schildt
Publisher: Osborne/McGraw-Hill (March 8, 2002)
ISBN: 0072134852
*/
// Demonstrate fixed.
using System;
class Test {
public int num;
public Test(int i) { num = i; }
}
public class FixedCode {
// mark Main as unsafe
unsafe public static void Main() {
Test o = new Test(19);
fixed (int* p = &o.num) { // use fixed to put address of o.num into p
Console.WriteLine("Initial value of o.num is " + *p);
*p = 10; // assign the to count via p
Console.WriteLine("New value of o.num is " + *p);
}
}
}
Demonstrate pointers and unsafe
/*
C#: The Complete Reference
by Herbert Schildt
Publisher: Osborne/McGraw-Hill (March 8, 2002)
ISBN: 0072134852
*/
// Demonstrate pointers and unsafe.
using System;
public class UnsafeCode {
// mark Main as unsafe
unsafe public static void Main() {
int count = 99;
int* p; // create an int pointer
p = &count; // put address of count into p
Console.WriteLine("Initial value of count is " + *p);
*p = 10; // assign the to count via p
Console.WriteLine("New value of count is " + *p);
}
}
Use params to mark parameter
using System;
class ParamsSample {
static void PrintStrings(params String[] StringArray) {
for (int i = 0; i < StringArray.Length; i++)
Console.Write("{0} ", StringArray[i]);
Console.WriteLine();
}
static void Main(String[] args) {
String names = "K";
PrintStrings("A");
PrintStrings(names, "R", "S");
PrintStrings("R", "E", "C", "S");
}
}
[/csharp]
use the params feature to write functions which accept a variable number of arguments
using System;
public class MainClass {
public static void Main(string[] args) {
int nSum;
Console.WriteLine("{0}", SumArgs(out nSum, 1, 2, 3));
int[] nArray = { 4, 5, 6 };
Console.WriteLine("{0}", SumArgs(out nSum, nArray));
}
public static int SumArgs(out int nSum, params int[] list) {
nSum = 0;
foreach (int n in list) {
nSum += n;
}
return nSum;
}
}

