C# (C Sharp) Unified Type System: Value Types

As 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
• Enumerations
• Structures

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:

Unified Type System Chart C#

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:
int intVar = new int( );

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 don’t 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:
enum sampleEnum { Const1=10, Const2, Const3, Const4};
Now the values for the constants will be: Const1 = 10, Const2 = 11, Const3=12, Const4=13

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:
public class sampleEnumeration {
enum sampleEnum:long { Const1=1111111111L, Const2};
public static void Main( ) {
long constVar1 = (long) sampleEnum.Const1;
long constVar2 = (long) sampleEnum.Const2;
Console.WriteLine(“{0},{1}”, constVar1, constVar2);
}
}

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:
public struct Employee

{
public int empNo;
public string empName;
public Employee(int no, string name) {
empNo = no;
empName = name;
}
}
class testClass {
static void Main() {
Employee emp1 = new Employee();
Employee emp2 = new Employee(123, “XYZ”);
emp1.empNo = 122;
emp1.empName = “ABC”;
Console.WriteLine("emp1 data: empNo = {0}, empName = {1}", emp1.empNo,
emp1.empName);
Console.WriteLine("emp2 data: empNo = {0}, empName = {1}", emp2.empNo,
emp2.empName);
}
}

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.

| All about Conceptual Analysis on .NET Remoting | Building desktop applications in .Net | Building Distributed Applications Efficiently Using .Net Remoting | C# (C Sharp) Unified Type System: Reference Types | C# (C Sharp) Unified Type System: Value Types | Data access in .Net | Delegates Vs Interfaces in C# (C Sharp) | How is Integration between Java and .NET Achieved | How is Versioning Achieved in C# (C Sharp) | Implementing Authorization and authentication in ASP.net | Implementing design patterns in .Net | List of Query Keywords in C# (C Sharp) |


“Amazon and the Amazon logo are trademarks of Amazon.com, Inc. or its affiliates.”

| Privacy Policy for www.dotnet-guide.com | Disclosure | Contact |

Copyright - © 2004 - 2024 - All Rights Reserved.