So wcf defines some normal properties and methods to

Info icon This preview shows pages 329–331. Sign up to view the full content.

ogies, each of which may have its own unique characteristics. So WCF defines some normal properties and methods to deal with aspects of communication that are com- mon to most scenarios, but also provides a dictionary of dynamic properties to handle transport-specific scenarios. For example, if you are using WCF with HTTP-based communication, you might want your client code to be able to modify the User-Agent header. This header is specific to HTTP, and so WCF doesn’t provide a property for this as part of its programming model, because it wouldn’t do anything for most network protocols. Instead, you con- trol this with a dynamic property, added via the WCF Message type’s Properties dic- tionary, as Example 9-6 shows. Example 9-6. Setting a dynamic property on a WCF message Message wcfMessage = CreateMessageSomehow(); HttpRequestMessageProperty reqProps = new HttpRequestMessageProperty(); reqProps.Headers.Add(HttpRequestHeader.UserAgent, "my user agent"); wcfMessage.Properties[HttpRequestMessageProperty.Name] = reqProps; Dictionaries | 305
Image of page 329

Info icon This preview has intentionally blurred sections. Sign up to view the full version.

C# 4.0 introduces an alternative way to support dynamic properties, through the dynamic keyword (which we will describe in Chapter 18 ). This makes it possible to use normal C# property access syntax with properties whose availability is determined at runtime. So you might think dynamic makes dictionaries redundant. In practice, dynamic is nor- mally used only when interacting with dynamic programming systems such as scripting languages, so it’s not based on .NET’s dictionary mechanisms. Sparse arrays The final common scenario we’ll look at for dictionaries is to provide efficient storage for a sparse array . A sparse array is indexed by an integer, like a normal array, but only a tiny fraction of its elements contain anything other than the default value. For a numeric element type that would mean the array is mostly zeros, while for a reference type it would be mostly nulls. As an example of where this might be useful, consider a spreadsheet. When you create a new spreadsheet, it appears to be a large expanse of cells. But it’s not really storing information for every cell. I just ran Microsoft Excel, pressed Ctrl-G to go to a particular cell and typed in $XFD$1000000 , and then entered a value for that cell. This goes to the 16,384th column (which is as wide as Excel 2007 can go), and the 1 millionth row. Yet despite spanning more than 16 billion cells, the file is only 8 KB. And that’s because it doesn’t really contain all the cells—it only stores information for the cells that contain something. The spreadsheet is sparse—it is mostly empty. And it uses a representation that makes efficient use of space when the data is sparse. If you try to create a rectangular array with 16,384 columns and 1 million rows, you’ll get an exception as such an array would go over the .NET 4 upper size limit for any single array of 2 GB. A newly created array always contains default values for all of its elements, so the information it contains is always sparse to start with—sparseness is a
Image of page 330
Image of page 331
This is the end of the preview. Sign up to access the rest of the document.
  • Spring '15

{[ snackBarMessage ]}

What students are saying

  • Left Quote Icon

    As a current student on this bumpy collegiate pathway, I stumbled upon Course Hero, where I can find study resources for nearly all my courses, get online help from tutors 24/7, and even share my old projects, papers, and lecture notes with other students.

    Student Picture

    Kiran Temple University Fox School of Business ‘17, Course Hero Intern

  • Left Quote Icon

    I cannot even describe how much Course Hero helped me this summer. It’s truly become something I can always rely on and help me. In the end, I was not only able to survive summer classes, but I was able to thrive thanks to Course Hero.

    Student Picture

    Dana University of Pennsylvania ‘17, Course Hero Intern

  • Left Quote Icon

    The ability to access any university’s resources through Course Hero proved invaluable in my case. I was behind on Tulane coursework and actually used UCLA’s materials to help me move forward and get everything together on time.

    Student Picture

    Jill Tulane University ‘16, Course Hero Intern