Game Development Reference
In-Depth Information
byte[] nameBytes = new byte[size];
Array.Copy(buffer, 0, nameBytes, 0, size);
Console.WriteLine(“Computer Name: “ + Encoding.ASCII.GetString(nameBytes));
}
Conclusion
Figure 35.1 shows all three examples after execution. Notice the speed difference
between the unsafe context and the unsafe + unchecked context.
Figure 35.1 Screenshot of all three examples after execution.
This chapter covered the usage of pointers and unsafe code within the C# lan-
guage. There are only a few situations where it should be used, but pointers can
solve a lot of problems when used appropriately.
It is important to keep in mind that unsafe code contexts require elevated securi-
ty permissions to execute, so do not use unsafe code within applications that need
to run under a least privilege account.
Lastly, only use pointers for performance gains if the gain itself is substantial. A
great deal of the .NET framework has been optimized to its fullest potential, and
while you may think your unsafe code implementation is faster than the built-in
functionality, you cannot be certain unless you profile. Even then, if the difference
between your code and the built-in functionality is pretty close, you are better off
using the built-in functionality instead.
Search Nedrilad ::




Custom Search