What are the limitations of unsafe code, in C#? For example, can I do virtually arbitrary pointer casts and arithmetic as if I were using C or C++?
Share
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
Yes. All bets are off when unsafe is in play.
This is the idea behind “unsafe” – that the “safety” of verifiable types is removed, and you can cast from a pointer of one type to a pointer of another type without the runtime keeping you from shooting yourself in the foot, if you so desire – much like C or C++.
Here’s an example of using different pointer types in C#:
Note the type of the array is
Byte[], but after I get aByte*I can cast it toInt64*and work with 8 bytes at a time.