Aliases doesn’t work always

.Net CTS i.e. Common Type System have defined aliases for primitive data types. This facility is made available for the programmers who come from the programming languages prior to .Net like C++, VB6 etc.C3 makes use of int and long for 32 bit and 64 bit unassigned integer whereas VB makes use of Integer and long for the same.
Lets take a look at CTS memory allocation table for C# and C++;

CTS type
Size
C# alias
Type equivalent in C++
System.Int32
4 bytes
int
int or long
System.Int64
8 bytes
long
_ _int64
System.Char
2 bytes
char
WCHAR
System.Double
8 bytes
double
double

Now from the table above it is clear that C# makes use of int as an alias for 32 bit unassigned integer and C++ makes use of int or long. But long in case of C# is 64 bit unassigned integer.

Now you might ask what does that make difference. Let's have a look at it by an example.
We will call beep() function from an unmanaged code with its required arguments.
VB6 prototype is;
Beep(dwFreq as Long, dwDuration as Long) as Boolean
Now if we write it in .net, we might pass arguments as ;
Beep(long dwFreq,long dwDuration)
Now in this case long for VB is 32 bit unassigned integer and long for C# is 64 bit unassigned integer. The function will never return any output because of expected type mismatch.
If you are having any doubts about types in your mind, it is always a good practice to make use qualified names from CTS.

Ref: .Net Gotchas

No comments:

Post a Comment