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

📄 chap10.lst

📁 Csharp2完全参考手册源代码 详细的说明可以在书里看到 该书是08年刚出炉很新鲜
💻 LST
📖 第 1 页 / 共 2 页
字号:
listing 1
// Use an indexer to create a fail-soft array. 
 
using System; 
 
class FailSoftArray {  
  int[] a;    // reference to underlying array  
 
  public int Length; // Length is public 
 
  public bool errflag; // indicates outcome of last operation 
   
  // Construct array given its size. 
  public FailSoftArray(int size) { 
    a = new int[size]; 
    Length = size;  
  } 
 
  // This is the indexer for FailSoftArray. 
  public int this[int index] { 
    // This is the get accessor. 
    get { 
      if(ok(index)) { 
        errflag = false; 
        return a[index]; 
      } else { 
        errflag = true; 
        return 0; 
      } 
    } 
 
    // This is the set accessor 
    set { 
      if(ok(index)) { 
        a[index] = value; 
        errflag = false; 
      } 
      else errflag = true; 
    } 
  } 
 
  // Return true if index is within bounds. 
  private bool ok(int index) { 
   if(index >= 0 & index < Length) return true; 
   return false; 
  } 
}  
  
// Demonstrate the fail-soft array. 
class FSDemo {  
  public static void Main() {  
    FailSoftArray fs = new FailSoftArray(5); 
    int x; 
 
    // show quiet failures 
    Console.WriteLine("Fail quietly."); 
    for(int i=0; i < (fs.Length * 2); i++) 
      fs[i] = i*10; 
 
    for(int i=0; i < (fs.Length * 2); i++) { 
      x = fs[i]; 
      if(x != -1) Console.Write(x + " "); 
    } 
    Console.WriteLine(); 
 
    // now, generate failures 
    Console.WriteLine("\nFail with error reports."); 
    for(int i=0; i < (fs.Length * 2); i++) { 
      fs[i] = i*10; 
      if(fs.errflag) 
        Console.WriteLine("fs[" + i + "] out-of-bounds"); 
    } 
 
    for(int i=0; i < (fs.Length * 2); i++) { 
      x = fs[i]; 
      if(!fs.errflag) Console.Write(x + " "); 
      else 
        Console.WriteLine("fs[" + i + "] out-of-bounds"); 
    } 
  } 
}

listing 2
// Overload the FailSoftArray indexer. 
  
using System;  
  
class FailSoftArray {   
  int[] a;    // reference to underlying array   
  
  public int Length; // Length is public  
  
  public bool errflag; // indicates outcome of last operation  
    
  // Construct array given its size.  
  public FailSoftArray(int size) {  
    a = new int[size];  
    Length = size;   
  }  
  
  // This is the int indexer for FailSoftArray.  
  public int this[int index] {  
    // This is the get accessor.  
    get {  
      if(ok(index)) {  
        errflag = false;  
        return a[index];  
      } else {  
        errflag = true;  
        return 0;  
      }  
    }  
  
    // This is the set accessor  
    set {  
      if(ok(index)) {  
        a[index] = value;  
        errflag = false;  
      }  
      else errflag = true;  
    }  
  }  
  
  /* This is another indexer for FailSoftArray. 
     This index takes a double argument.  It then 
     rounds that argument to the nearest integer 
     index. */  
  public int this[double idx] {  
    // This is the get accessor.  
    get {  
      int index; 
 
      // round to nearest int 
      if( (idx - (int) idx) < 0.5) index = (int) idx; 
      else index = (int) idx + 1; 
 
      if(ok(index)) {  
        errflag = false;  
        return a[index];  
      } else {  
        errflag = true;  
        return 0;  
      }  
    }  
  
    // This is the set accessor  
    set {  
      int index; 
 
      // round to nearest int 
      if( (idx - (int) idx) < 0.5) index = (int) idx; 
      else index = (int) idx + 1; 
 
      if(ok(index)) {  
        a[index] = value;  
        errflag = false;  
      }  
      else errflag = true;  
    }  
  }  
  
  // Return true if index is within bounds.  
  private bool ok(int index) {  
   if(index >= 0 & index < Length) return true;  
   return false;  
  }  
}   
   
// Demonstrate the fail-soft array.  
class FSDemo {   
  public static void Main() {   
    FailSoftArray fs = new FailSoftArray(5);  
  
    // put some values in fs 
    for(int i=0; i < fs.Length; i++) 
      fs[i] = i;  
 
    // now index with ints and doubles 
    Console.WriteLine("fs[1]: " + fs[1]); 
    Console.WriteLine("fs[2]: " + fs[2]); 
 
    Console.WriteLine("fs[1.1]: " + fs[1.1]); 
    Console.WriteLine("fs[1.6]: " + fs[1.6]); 
 
  }  
}

listing 3
// Indexers don't have to operate on actual arrays. 
 
using System; 
 
class PwrOfTwo {  
 
  /* Access a logical array that contains 
     the powers of 2 from 0 to 15. */ 
  public int this[int index] { 
    // Compute and return power of 2. 
    get { 
      if((index >= 0) && (index < 16)) return pwr(index); 
      else return -1; 
    } 
 
    // there is no set accessor 
  } 
 
  int pwr(int p) { 
    int result = 1; 
 
    for(int i=0; i < p; i++) 
      result *= 2; 
     
    return result; 
  } 
}  
  
class UsePwrOfTwo {  
  public static void Main() {  
    PwrOfTwo pwr = new PwrOfTwo(); 
 
    Console.Write("First 8 powers of 2: "); 
    for(int i=0; i < 8; i++) 
      Console.Write(pwr[i] + " "); 
    Console.WriteLine(); 
 
    Console.Write("Here are some errors: "); 
    Console.Write(pwr[-1] + " " + pwr[17]); 
 
    Console.WriteLine(); 
  } 
}

listing 4
// A two-dimensional fail-soft array. 
 
using System; 
 
class FailSoftArray2D {  
  int[,] a; // reference to underlying 2D array  
  int rows, cols; // dimensions 
  public int Length; // Length is public 
 
  public bool errflag; // indicates outcome of last operation 
   
  // Construct array given its dimensions. 
  public FailSoftArray2D(int r, int c) { 
    rows = r; 
    cols = c; 
    a = new int[rows, cols]; 
    Length = rows * cols;  
  } 
 
  // This is the indexer for FailSoftArray2D. 
  public int this[int index1, int index2] { 
    // This is the get accessor. 
    get { 
      if(ok(index1, index2)) { 
        errflag = false; 
        return a[index1, index2]; 
      } else { 
        errflag = true; 
        return 0; 
      } 
    } 
 
    // This is the set accessor. 
    set { 
      if(ok(index1, index2)) { 
        a[index1, index2] = value; 
        errflag = false; 
      } 
      else errflag = true; 
    } 
  } 
 
  // Return true if indexes are within bounds. 
  private bool ok(int index1, int index2) { 
   if(index1 >= 0 & index1 < rows & 
      index2 >= 0 & index2 < cols) 
         return true; 
 
   return false; 
  } 
}  
  
// Demonstrate a 2D indexer. 
class TwoDIndexerDemo {  
  public static void Main() {  
    FailSoftArray2D fs = new FailSoftArray2D(3, 5); 
    int x; 
 
    // show quiet failures 
    Console.WriteLine("Fail quietly."); 
    for(int i=0; i < 6; i++) 
      fs[i, i] = i*10; 
 
    for(int i=0; i < 6; i++) { 
      x = fs[i,i]; 
      if(x != -1) Console.Write(x + " "); 
    } 
    Console.WriteLine(); 
 
    // now, generate failures 
    Console.WriteLine("\nFail with error reports."); 
    for(int i=0; i < 6; i++) { 
      fs[i,i] = i*10; 
      if(fs.errflag) 
        Console.WriteLine("fs[" + i + ", " + i + "] out-of-bounds"); 
    } 
 
    for(int i=0; i < 6; i++) { 
      x = fs[i,i]; 
      if(!fs.errflag) Console.Write(x + " "); 
      else 
        Console.WriteLine("fs[" + i + ", " + i + "] out-of-bounds"); 
    } 
  } 
}

listing 5
// A simple property example. 
 
using System; 
 
class SimpProp {  
  int prop; // field being managed by MyProp 
 
  public SimpProp() { prop = 0; } 
 
  /* This is the property that supports access to 
     the private instance variable prop.  It 
     allows only positive values. */ 
  public int MyProp { 
    get { 
      return prop; 
    } 
    set { 
      if(value >= 0) prop = value; 
    }  
  } 
}  
  

⌨️ 快捷键说明

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