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

Labels

.net .Net Instrumentation logging .net localization Agile amazon amazon elasticache amazon services AppDomain Application Domain architecture asp ASP.Net authentication authentication mechanisms Byte order mark c# cache canvas app cdata certifications class classic mode cloud cloud computing cluster code-behind Combobox compilation Configuration providers configurations connection connectionString constructors control controls contructor CSV CTS .net types conversion database DataGridView DataSource DataTable DataType DBML delegates design pattern dispose double encoding Entity framework Events exception handling expiry fault contracts fault exceptions function pointers functions generics help HostingEnvironmentException IIS inner join instance management integrated mode javascript join left outer join LINQ LINQ join LINQ to SQL memory leak methods microsoft model driven app modes in IIS MSIL multiple catch blocks no primary key Nullable Osmos Osmotic Osmotic communication Osmotic communications page events page life cycle partial class PMI powerapps preserve precision points private contructor ProcessExit Project management properties property protect connectionString providerName providers query regular expression repository Responsive Web Design return type run-time RWD Saas self join session session expiry sessions singelton singleton pattern software as a service source control system SQLMetal string time management time-boxing toolstrip ToolStrip controls ToolStripControlHost tortoise SVN ToString() try catch finally update wcf web application web design web site web.config where-clause xml

Pages