concept value type in category .net

appears as: value type, value types, value type
C# in Depth

This is an excerpt from Manning's book C# in Depth.

I won’t present much code this time, but I’m sure it’ll be a familiar problem to you, especially if you’ve done a lot of work with databases. Imagine your list of products contains not just products on sale right now, but ones that aren’t available yet. In some cases, you may not know the price. If decimal were a reference type, you could just use null to represent the unknown price, but since it’s a value type, you can’t. How would you represent this in C# 1?

This myth comes in a variety of forms. Some people believe that value types can’t or shouldn’t have methods or other significant behavior—they should be used as simple data transfer types, with just public fields or simple properties. The DateTime type is a good counterexample to this: it makes sense for it to be a value type, in terms of being a fundamental unit like a number or a character, and it also makes sense for it to be able to perform calculations based on its value. Looking at things from the other direction, data transfer types should often be reference types anyway—the decision should be based on the desired value or reference type semantics, not the simplicity of the type.

Other people believe that value types are “lighter” than reference types in terms of performance. The truth is that in some cases value types are more performant—they don’t require garbage collection unless they’re boxed, don’t have the type identification overhead, and don’t require dereferencing, for example. But in other ways, reference types are more performant—parameter passing, assigning values to variables, returning values, and similar operations only require 4 or 8 bytes to be copied (depending on whether you’re running the 32-bit or 64-bit CLR) rather than copying all the data. Imagine if ArrayList were somehow a “pure” value type, and passing an ArrayList expression to a method involved copying all its data! In almost all cases, performance isn’t really determined by this sort of decision anyway. Bottlenecks are almost never where you think they’ll be, and before you make a design decision based on performance, you should measure the different options.

Figure 3.3. Visual demonstration of why List<T> takes up a lot less space than ArrayList when storing value types
NHibernate in Action

This is an excerpt from Manning's book NHibernate in Action.

  • An object of value type has no database identity; it belongs to an entity, and its persistent state is embedded in the table row of the owning entity (except in the case of collections, which are also considered value types, as you’ll see in chapter 6). Value types don’t have identifiers or identifier properties. The lifespan of a value-type instance is bounded by the lifespan of the owning entity.
  • The most obvious value types are simple objects like Strings and Integers. NHibernate also lets you treat a user-defined class as a value type, as you’ll see next. (We also come back to this important concept in section 6.1.)

    We first distinguished between entity and value types back in section 3.6.1. In order to give you a better understanding of the NHibernate type system, we elaborate a little more.

    Entities are the coarse-grained classes in a system. You usually define the features of a system in terms of the entities involved: “The user places a bid for an item” is a typical feature definition that mentions three entities—user, bid, and item. In contrast, value types are the much more fine-grained classes in a system, such as strings, numbers, dates, and monetary amounts. These fine-grained classes can be used in many places and serve many purposes; the value-type string can store email address, user-names, and many other things. Strings are simple value types, but it’s possible (but less common) to create value types that are more complex. For example, a value type like an address can contain several fields.

    How do you differentiate between value types and entities? From a more formal standpoint, we can say an entity is any class whose instances have their own persistent identity, and a value type is a class whose instances don’t. The entity instances may therefore be in any of the three persistent lifecycle states: transient, detached, or persistent. But we don’t consider these lifecycle states to apply to the simpler value-type instances. Furthermore, because entities have their own lifecycle, the Save() and Delete() methods of the NHibernate ISession interface apply to them, but never to value-type instances. To illustrate, consider figure 6.1.

    6.1.1. Associations and value types

    As we said, not all value types are simple. It’s possible for value types to also define associations. For example, the Money value type may have a property called Currency that is an association to a Currency entity, as shown in figure 6.2

    Figure 6.2. The Money value type with an association to a Currency entity

    If your value types have associations, they must always point to entities. The reason is that, if those associations could point from entities to value types, a value type could potentially belong to several entities, which isn’t desirable. This is one of the great things about value types; if you update a value-type instance, you know that it affects only the entity that owns it. For example, changing the TotalAmount of one Order can’t accidentally affect others.

    So far, we’ve talked about value types and entities from an object-oriented perspective. To build a more complete picture, we now look at how the relational model sees value types and entities, and how NHibernate bridges the gap.