Wednesday, 1 August 2012

Using Value Types in .Net

The simplest types in the .NET framework, primarily numeric and Boolean types,are value types. Value types are variables that contain their data directly instead of containing a reference to the data stored elsewhere in memory. Instances of value types are stored in an area of memory called the stack, where the runtime can create, read, update, and an be remove them quickly with minimal overhead. There are general value types:
  • Built-in types
  • User-defined types
  • Enumeration
These types can be explained as follows:

  • Built-in Value Types:  These are base types provided with the .NET Framework, with which other types are built. All built-in numeric types are value types. You choose a numeric type based on the size of the values we expect to workwith and the level of precision we require. These numeric types are used so frequently that Visual Basic and C# define aliases for them. To declare a value type we must first declare a symbol as an instance of that type. Value types have an implicit constructor, so declaring them instantiates the type automatically; we don't have to i nclude the New Keyword as we do with the classes.
  • User-Defined types:   These types are also called structures or simply structs, after the language keyword used to create them. As with other value types, instances of user-defined types are stored on the stackand they contain their data directly.
  • Enumerations:  These are related symbols that have fixed values. Use enumerations to provide a list of choices for developers a list of choices for developers using class. If we create an instance of the Titles type, Visual Studio displays a list of the available values when we assign a value to the variable. The purpose of numerations is to simply coding and improve code readability by enabling to use meaningul symbols instead of simple numeric values. 

No comments:

Post a Comment