Showing posts with label .net 2.0 Framework Fundamental. Show all posts
Showing posts with label .net 2.0 Framework Fundamental. Show all posts

Monday, June 25, 2012

How to Declare a Value Type Variable

To use a type, you must first declare a symbol as an instance of that type. Value types
have an implicit constructor, so declaring them instantiates the type automatically; you
don’t have to include the New keyword as you do with classes. The constructor assigns
a default value (usually null or 0) to the new instance, but you should always explicitly
initialize the variable within the declaration, as shown in the following code block:


' VB
Dim b As Boolean = False

// C#
bool b = false;

Declare a variable as nullable if you want to be able to determine whether a value has
been assigned. For example, if you are storing data from a yes/no question on a form
and the user did not answer the question, you should store a null value. The following
code declares a boolean variable that can be true, false, or null:

' VB
Dim b As Nullable(Of Boolean) = Nothing

// C#
Nullable// b = null;

// Shorthand notation, only for C#
bool? b = null;

Declaring a variable as nullable enables the HasValue and Value members. Use HasValue
to detect whether a value has been set as follows:

' VB
If b.HasValue Then Console.WriteLine("b is {0}.", b.Value) _
Else Console.WriteLine("b is not set.")

// C#
if (b.HasValue) Console.WriteLine("b is {0}.", b.Value);
else Console.WriteLine("b is not set.");

Sunday, May 2, 2010

The Object base class

In the .NET Framework, all types are derived from System.Object. That relationship helps establish the common type system used throughout the .NET Framework

Using Value Types

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 remove them quickly with minimal overhead.

There are three general value types:
  • Built-in types
  • User-defined types
  • Enumerations
Each of these types is derived from the System.ValueType base type.