13.07.2015 Views

C# in Depth

C# in Depth

C# in Depth

SHOW MORE
SHOW LESS
  • No tags were found...

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

Fixed-size buffers <strong>in</strong> unsafe code199checksum pragma to tell the <strong>C#</strong> compiler to use that checksum <strong>in</strong>stead of calculat<strong>in</strong>gone from the generated page.The syntax of the checksum pragma is#pragma checksum "filename" "{guid}" "checksum bytes"The GUID <strong>in</strong>dicates which hash<strong>in</strong>g algorithm has been used to calculate the checksum.The documentation for the CodeChecksumPragma class gives GUIDs for SHA-1 andMD5, should you ever wish to implement your own dynamic compilation frameworkwith debugger support.It’s possible that future versions of the <strong>C#</strong> compiler will <strong>in</strong>clude more pragmadirectives, and other compilers (such as the Mono compiler, mcs) could have theirown support for different features. Consult your compiler documentation for themost up-to-date <strong>in</strong>formation.The next feature is another one that you may well never use—but at the same time,if you ever do, it’s likely to make your life somewhat simpler.7.6 Fixed-size buffers <strong>in</strong> unsafe codeWhen call<strong>in</strong>g <strong>in</strong>to native code with P/Invoke, it’s not particularly unusual to f<strong>in</strong>d yourselfdeal<strong>in</strong>g with a structure that is def<strong>in</strong>ed to have a buffer of a particular lengthwith<strong>in</strong> it. Prior to <strong>C#</strong> 2, such structures were difficult to handle directly, even withunsafe code. Now, you can declare a buffer of the right size to be embedded directlywith the rest of the data for the structure.This capability isn’t just available for call<strong>in</strong>g native code, although that is its primaryuse. You could use it to easily populate a data structure directly correspond<strong>in</strong>g toa file format, for <strong>in</strong>stance. The syntax is simple, and once aga<strong>in</strong> we’ll demonstrate itwith an example. To create a field that embeds an array of 20 bytes with<strong>in</strong> its enclos<strong>in</strong>gstructure, you would usefixed byte data[20];This would allow data to be used as if it were a byte* (a po<strong>in</strong>ter to byte data),although the implementation used by the <strong>C#</strong> compiler is to create a new nested typewith<strong>in</strong> the declar<strong>in</strong>g type and apply the new FixedBuffer attribute to the variableitself. The CLR then takes care of allocat<strong>in</strong>g the memory appropriately.One downside of this feature is that it’s only available with<strong>in</strong> unsafe code: theenclos<strong>in</strong>g structure has to be declared <strong>in</strong> an unsafe context, and you can only use thefixed-size buffer member with<strong>in</strong> an unsafe context too. This limits the situations <strong>in</strong>which it’s useful, but it can still be a nice trick to have up your sleeve at times. Also,fixed-size buffers are only applicable to primitive types, and can’t be members ofclasses (only structures).There are remarkably few W<strong>in</strong>dows APIs where this feature is directly useful. Thereare numerous situations where a fixed array of characters is called for—the TIME_ZONE_INFORMATION structure, for example—but unfortunately fixed-size buffers of charactersappear to be handled poorly by P/Invoke, with the marshaler gett<strong>in</strong>g <strong>in</strong> the way.Licensed to Rhona Hadida

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!