ch4_13.cs
来自「《c#技术内幕代码》」· CS 代码 · 共 52 行
CS
52 行
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 + =
减小字号Ctrl + -
显示快捷键?