⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 chap19.lst

📁 Csharp2完全参考手册源代码 详细的说明可以在书里看到 该书是08年刚出炉很新鲜
💻 LST
字号:
listing 1
// Demonstrate pointers and unsafe. 
 
using System; 
 
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 10 to count via p 
     
    Console.WriteLine("New value of count is " + *p); 
  } 
}

listing 2
// Demonstrate fixed. 
 
using System; 
 
class Test { 
  public int num; 
  public Test(int i) { num = i; } 
} 
 
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); 
    } 
  } 
}

listing 3
// Demonstrate the effects of pointer arithmethic. 
 
using System; 
 
class PtrArithDemo { 
  unsafe public static void Main() { 
    int x; 
    int i;  
    double d; 
 
    int* ip = &i; 
    double* fp = &d; 
 
    Console.WriteLine("int     double\n"); 
 
    for(x=0; x < 10; x++) { 
       Console.WriteLine((uint) (ip) + " " + 
                         (uint) (fp)); 
       ip++; 
       fp++; 
    } 
  } 
}

listing 4
// Demonstrate pointer comparison. 
 
using System; 
 
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); 
  } 
}

listing 5
/* An array name with an index yields a pointer to the 
   start of the array. */ 
 
using System; 
 
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."); 
    } 
  } 
}

listing 6
// Index a pointer as if it were an array. 
 
using System; 
 
class PtrIndexDemo { 
  unsafe public static void Main() { 
    int[] nums = new int[10]; 
 
    // index pointer 
    Console.WriteLine("Index pointer like array."); 
    fixed (int* p = nums) { 
      for(int i=0; i < 10; i++)  
        p[i] = i; // index pointer like array 
 
      for(int i=0; i < 10; i++)  
        Console.WriteLine("p[{0}]: {1} ", i, p[i]); 
    } 
 
    // use pointer arithmetic 
    Console.WriteLine("\nUse pointer arithmetic."); 
    fixed (int* p = nums) { 
      for(int i=0; i < 10; i++)  
        *(p+i) = i; // use pointer arithmetic 
 
      for(int i=0; i < 10; i++)  
        Console.WriteLine("*(p+{0}): {1} ", i, *(p+i)); 
    } 
  } 
}

listing 7
// Use fixed to get a pointer to the start of a string. 
 
using System; 
 
class FixedString { 
  unsafe public static void Main() { 
    string str = "this is a test"; 
 
    // Point p to start of str. 
    fixed(char* p = str) { 
 
      // Display the contents of str via p. 
      for(int i=0; p[i] != 0; i++) 
        Console.Write(p[i]); 
    } 
 
    Console.WriteLine(); 
     
  } 
}

listing 8
using System; 
 
class MultipleIndirect { 
  unsafe public static void Main() { 
    int x;    // holds an int value  
    int* p;  // holds an int pointer 
    int** q; // holds an pointer to an int pointer 
 
    x = 10; 
    p = &x; // put address of x into p 
    q = &p; // put address of p into q 
 
    Console.WriteLine(**q); // display the value of x  
  } 
}

listing 9
// Demonstrate stackalloc. 
 
using System; 
 
class UseStackAlloc { 
  unsafe public static void Main() { 
    int* ptrs = stackalloc int[3]; 
 
    ptrs[0] = 1; 
    ptrs[1] = 2; 
    ptrs[2] = 3; 
 
    for(int i=0; i < 3; i++) 
      Console.WriteLine(ptrs[i]); 
  } 
}

listing 10
// Demonstrate a fixed-size buffer. 
  
using System;  
 
// Create a fixed size buffer. 
unsafe struct FixedBankRecord { 
  public fixed byte name[80]; // create a fixed-size buffer 
  public double balance; 
  public long ID; 
} 
  
class FixedSizeBuffer {  
  // mark Main as unsafe  
  unsafe public static void Main() {  
    Console.WriteLine("Size of FixedBankRecord is " +  
                       sizeof(FixedBankRecord)); 
  }  
}

listing 11
// Demonstrate a nullable type. 
 
using System; 
 
class NullableDemo { 
  public static void Main() { 
    int? count = null; 
 
    if(count.HasValue) 
      Console.WriteLine("count has this value: " + count.Value); 
    else  
      Console.WriteLine("count has no value"); 
 
    count = 100; 
 
    if(count.HasValue) 
      Console.WriteLine("count has this value: " + count.Value); 
    else  
      Console.WriteLine("count has no value");     
  } 
}

listing 12
// Use nullable objects in expressions. 
 
using System; 
 
class NullableDemo { 
  public static void Main() { 
    int? count = null; 
    int? result = null; 
 
    int incr = 10; // notice that incr is a non-nullable type 
 
    // result contains null, because count is null. 
    result = count + incr; 
 
    if(result.HasValue) 
      Console.WriteLine("result has this value: " + result.Value); 
    else  
      Console.WriteLine("result has no value");     
 
    // Now, count is given a value and result  
    // will contain a value. 
    count = 100; 
    result = count + incr; 
 
    if(result.HasValue) 
      Console.WriteLine("result has this value: " + result.Value); 
    else  
      Console.WriteLine("result has no value");     
             
  } 
}

listing 13
// Using ?? 
 
using System; 
 
class NullableDemo2 { 
  // Return a zero balance. 
  static double getZeroBal() { 
    Console.WriteLine("In getZeroBal()."); 
      return 0.0; 
  } 
 
  public static void Main() { 
    double? balance = 123.75; 
    double currentBalance; 
 
    // Here, getZeroBal( ) is not called because balance 
    // contains a value. 
    currentBalance = balance ?? getZeroBal(); 
 
    Console.WriteLine(currentBalance); 
  } 
}

listing 14
partial class XY { 
  public XY(int a, int b) { 
    x = a; 
    y = b; 
  } 
}

listing 15
partial class XY { 
  int x; 
 
  public int X { 
    get { return x; } 
    set { x = value; } 
  } 
}

listing 16
partial class XY { 
  int y; 
 
  public int Y { 
    get { return y; } 
    set { y = value; } 
  } 
}

listing 17
// Demonstrate partial class definitions. 
using System; 
 
class Test { 
  public static void Main() { 
    XY xy = new XY(1, 2); 
 
 
    Console.WriteLine(xy.X + "," + xy.Y); 
  } 
}

listing 18
// Demonstrate readonly. 
 
using System; 
 
class MyClass { 
  public static readonly int SIZE = 10; 
} 
 
class DemoReadOnly { 
  public static void Main() { 
    int[] nums = new int[MyClass.SIZE]; 
 
    for(int i=0; i<MyClass.SIZE; i++) 
      nums[i] = i; 
 
    foreach(int i in nums) 
      Console.Write(i + " "); 
 
    // MyClass.SIZE = 100; // Error!!! can't change 
  } 
}

listing 19
// Demonstrate using statement. 
 
using System; 
using System.IO; 
 
class UsingDemo { 
  public static void Main() { 
    StreamReader sr = new StreamReader("test.txt"); 
 
    // Use object inside using statement. 
    using(sr) { 
      Console.WriteLine(sr.ReadLine()); 
      sr.Close(); 
    } 
 
    // Create StreamReader inside the using statement. 
    using(StreamReader sr2 = new StreamReader("test.txt")) { 
      Console.WriteLine(sr2.ReadLine()); 
      sr2.Close(); 
    } 
  } 
}

listing 20
#include <stdlib.h> 
 
int __declspec(dllexport) absMax(int a, int b) { 
  return abs(a) < abs(b) ? abs(b) : abs(a); 
}

listing 21
using System; 
using System.Runtime.InteropServices; 
 
class ExternMeth { 
 
  // Here an extern method is declared. 
  [DllImport("ExtMeth.dll")] 
  public extern static int absMax(int a, int b); 
 
  public static void Main() { 
 
    // Use the extern method. 
    int max = absMax(-10, -20); 
    Console.WriteLine(max); 
 
  } 
}

listing 22
using System; 
 
namespace MyNS { 
 
  public class MyClass { 
    public MyClass() { 
      Console.WriteLine("Constructing from MyClass1.dll."); 
    } 
  } 
}

listing 23
using System; 
 
namespace MyNS { 
  public class MyClass { 
    public MyClass() { 
      Console.WriteLine("Constructing from MyClass2.dll."); 
    } 
  } 
}

listing 24
// extern alias statements must be at the top of the file. 
extern alias Asm1; 
extern alias Asm2; 
 
using System; 
 
class Demo { 
  public static void Main() { 
    Asm1::MyNS.MyClass t = new Asm1::MyNS.MyClass(); 
    Asm2::MyNS.MyClass t2 = new Asm2::MyNS.MyClass(); 
  } 
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -