
C# (C Sharp) Unified Type System: Value TypesAs already known, value types, reference types and pointer types jointly form the unified type system of C#. Value types are simplest and most commonly used. They are derived from System.ValueType.
Value type
variables are directly assigned with the data and they are allocated in
stack. Value type variables that are out of scope are automatically removed/destroyed
from the stack. Hence they do not require garbage collection to happen.
Moreover, you can easily copy values by assigning one value type variable
to another. Value types are further categorized into: Simple
Types This article
will give you an overview on each of these data types. Simple
Types: Simple types
are the integral types supported by C#. Each simple type is mapped to
a system type of .NET framework. Given below is a list of simple types
along with the system type to which they are mapped, their data representation
and examples: The examples
shown in the table above includes the simple way of declaration and initialization.
Consider the specific example: int intVar
=10; This means
that you have declared an integer variable and initialized it with the
value zero. There is an alternative way of declaring it. As already known,
all these value types derive from System.Object. Hence you can instantiate
integral types in the same way as you instantiate reference types. The
statement mentioned above can also be represented as: This statement
will instantiate the integer variable and initializes it to zero by default.
Not just integer, all the integral types shown in the table above can
be declared in the same way. Enumerations:
Enumerations
are defined using the keyword enum. Each enumeration is used to represent
a collection of constants of same type. You can explicitly define values
to these constants, if not they will be implicitly defined. Consider the
following example: enum sampleEnum
{Const1, Const2, Const3, Const4}; Each enum
should have a type associated with it. The type can be one among byte,
short, int or long. If you dont explicitly specify the type, it
will be assigned as int by default. The example above has no type mentioned.
Hence it is assumed as int by default. Here sampleEnum has four integer
constants but their values are not mentioned. Hence implicit values will
be assigned to them. Implicit value starts from 0 and increments by 1.
Hence Const1 will be assigned as 0, Const2 as 1, Const3 as 2 and Const4
as 3. What if you
are comfortable with the incremental sequence but you want the constant
values to start with 10 instead of 0? Then you have to define the enumeration
as: Assume that
you are convenient with these values but you want to explicitly mention
Const4 as 53. You can very well do it as shown below: enum sampleEnum
{ Const1=10, Const2, Const3, Const4=53}; In these
examples, type of enum is not mentioned. What if you want to mention the
type? Following example defines an enum of type long. enum sampleEnum:long
{ Const1=1111111111L, Const2, Const3, Const4}; You can access
enum constants using dot notation. Ensure that you explicitly cast the
enum constant values when you are assigning it to variables. Also remember
that enum name cannot include any blank space in between. A complete example
is provided below: Structures:
Structures are represented using the keyword struct. Enumerations discussed
above can contain only constants that too of same type. But structures
can contain not just constants but also constructors, methods, fields,
properties, indexer, operators, nested types and events. Moreover, all
these members can be of different types. Here is an example of structures: { Structures
might look very similar to classes but structures possess the characteristics
and limitations of value type and classes have the characteristics of
reference type. Moreover, structures cannot define no-argument constructor
or destructor. And modifier for a struct is public by default whereas
modifier for a class is private by default.
Tweet
_______________________________________________________________________ _______________________________________________________________________
FREE
Subscription
Subscribe to our mailing list and receive new articles Note
: We never rent, trade, or sell my email lists to Visit
.NET Programming Tutorial Homepage ______________________________________________________ |