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

📄 ch4_13.cs

📁 《c#技术内幕代码》
💻 CS
字号:
using System;

class CH4_13
{
   public static unsafe String UnsafeCodeExample( String s )
   {
      int  len = s.Length;
      char[] str = new char[len+1];
      string stemp = "";
      int nPos = 0;
      
      fixed(char* sptr = str)
      {
         // Copy the string in backwards
         for ( int i=len-1; i>=0; --i )
         {
            sptr[nPos++] = s[i];
            sptr[nPos] = (char)0;
         }
      
         // Now, copy it back
         for ( int i=0; i<len; ++i )
            stemp += sptr[i];
      }
      return stemp;
   }
   public static unsafe void BadIdea()
   {
      string s = "Another test";
      char[] str = new char[s.Length+1];
      for ( int i=0; i<s.Length; ++i )
      {
         str[i] = s[i];
	 str[i+1] = (char)0;
      }
      
      fixed(char* sptr = str)
      {
         for ( int I=0; I<20; ++I )
             Console.WriteLine("S[{0}] = {1}", I, sptr[I] );
      }
   }
   
   public static void Main()
   {
      String s = UnsafeCodeExample("This is a test");
      Console.WriteLine( "Reversed: {0}", s );
      BadIdea();
   }
}
 

⌨️ 快捷键说明

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