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 = &amp;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 = &amp;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;
    }
}

    


Check the array length for params parameters

   
 

using System;
public class Employee {

    public Employee(params string[] _name) {
        switch (_name.Length) {

            case 1:
                name = _name[0];
                break;
            case 2:
                name = _name[0] + " " + _name[1];
                break;
            default:
                break;
        }
    }
    public void GetName() {
        Console.WriteLine(name);
    }
    private string name = "";
}
public class Employeenel {

    public static void Main() {
        Employee bob = new Employee("Bob", "Wilson");
        bob.GetName();
    }
}

    


Normal parameter and params parameters

   
 

using System;

public class Starter {
    public static void Main() {
        Names("F", "F", "B", "A");
        Names("F", 1234, 5678, 9876, 4561);
        Names("F", "C", "D");
    }

    public static void Names(string company,params string[] employees) {
        Console.WriteLine("{0} employees: ",company);
        foreach (string employee in employees) {
            Console.WriteLine(" {0}", employee);
        }
    }

    public static void Names(string company,params int[] emplid) {
        Console.WriteLine("{0} employees: ",company);
        foreach (int employee in emplid) {
            Console.WriteLine(" {0}", employee);
        }
    }

    public static void Names(string company,string empl1, string empl2) {
        Console.WriteLine("{0} employees: ",company);
        Console.WriteLine("  {0}", empl1);
        Console.WriteLine("  {0}", empl2);
    }
}