VB.NET to C# – 02
Working with data types

VB.NET to C# – 02
Working with data types

The Common Type System (CTS) ensures that components written in C# or VB.NET can interact with each other. The CTS supports two general categories of types: Value types and Reference types. Value types directly contain data. An instance of a value type is either allocated on the stack or allocated inline in a structure. These types can be natively built-in (native data types), user-defined or enumerations. A reference type, in contrast to a value type, stores a reference to the value’s memory address and is allocated on the heap. A reference type can be a self-describing type, a pointer type or an interface type. As said, the native .NET types are in general fully compatible, but naming and assigning is different between VB.NET and C#.

Value Types
Boolean bool
Byte, SByte byte, sbyte
Char char
Short, Integer, Long short, int, long
UShort, UInteger, ULong ushort, uint, ulong
Single, Double float, double
Decimal decimal
Date DateTime (not built-in in C#)

Reference Types
Objects objects
String string
Arrays arrays
Delegates delegates

Declaring a variable in C# is quite different from VB.NET. In C# you have to start with the type and than the name and assignment of the actual value. Some examples in VB.NET:

Dim correct As Boolean = True
Dim aByte As Byte = &H2A   
Dim customer As Object = Nothing
Dim lastName As String = "Obelink"
Dim aCharacter As Char = "A"c
Dim today As Date = #10/22/2016 09:45:00 PM#
Dim price As Decimal = 25.99@
Dim calculatedValue As Single = 2.6!
Dim pi As Double = 3.1415
Dim numberOfStudents As Short = 123S    ' Int16
Dim numberOfCars As Integer = 123456    ' Int32
Dim numberOfPeople As Long = 123456789L ' Int64

The same variables and assignments in C#:

bool correct = true;
byte aByte = 0x2A;   
object customer = null;
string lastName = "Obelink";
char aCharacter = 'A';
DateTime today = DateTime.Parse("10/22/2016 09:45:00 PM");
decimal price = 25.99m;
float calculatedValue = 2.6f;
double pi = 3.1415;   
short numberOfStudents = 123;   // int16
int numberOfCars = 123456;     // int32
long numberOfPeople = 123456L;  // int64

Local variables can be given an inferred “type” of var instead of an explicit type. It will derive the type from the assigned value. In the code below you can also see an example of the use of a nullable – and an anonymous type.


Dim aDouble = 1.23
Dim aString = "Andre"
Dim aDoubleArray = New Double() {1.1, 2.2, 3.3}

' A nullable Type
Dim dateShipped As Date? = Nothing

' An anonymous type
Dim person = New With {.FirstName = "Andre", .LastName = "Obelink"}


var aDouble = 1.23;
var aString = "Andre";
var aDoubleArray = new double[] {1.1, 2.2, 3.3 };

// A nullable Type
DateTime? dateShipped = null;

// An anonymous type
var person = new {FirstName = "Andre", LastName = "Obelink"}

Special data types or – structures are enumerations. To be honest, I really like the way you can declare them in C#. I give also an example how to extract the value and description in both languages. Please notice that the default behaviour of both languages is different.


Public Enum Status
End Enum

Public Enum Severity
    Info = 0
    Warning = 10
    [Error] = 20    ' Error is a reserved word
End Enum

' Extract the value or description
Debug.WriteLine(Severity.Warning)               ' 20
Debug.WriteLine(Severity.Warning.ToString())    ' Warning


public enum Status {Pending, InProgress, Finsihed}
public enum Severity {Info = 0, Warning = 10, Error = 20}

// Extract the value or description
Debug.WriteLine((int)Severity.Warning);         // 20
Debug.WriteLine(Severity.Warning);              // Warning

Leave a Reply