Type Casting in C#

EDN Admin

Well-known member
Joined
Aug 7, 2010
Messages
12,794
Location
In the Machine
<font style="background-color:#ffffff" face="Verdana, Geneva, Arial, Sans-serif" size=4>Hi,</font>
<font style="background-color:#ffffff" face="Verdana, Geneva, Arial, Sans-serif" size=4> </font>
<font face=Verdana size=4>I am Using VS2005 and framework 2.0</font>
 
<font style="background-color:#ffffff" face="Verdana, Geneva, Arial, Sans-serif" size=4>I have read about boxing and unboxing [ ref: </font><a title="http://msdn2.microsoft.com/en-us/library/yz2be5wk.aspx http://msdn2.microsoft.com/en-us/library/yz2be5wk.aspx <font style="background-color:#ffffff" face="Verdana, Geneva, Arial, Sans-serif" size=4>http://msdn2.microsoft.com/en-us/library/yz2be5wk.aspx</font> <font style="background-color:#ffffff" face="Verdana, Geneva, Arial, Sans-serif" size=4> ] at msdn and it is costing more in term of memory usage on the heap, and anybody tell me do i use typecasting or System.Convert.ToInt32 ot ToDateTime etc.</font>
<font style="background-color:#ffffff" face="Verdana, Geneva, Arial, Sans-serif" size=4>e.g: </font>
<font style="background-color:#ffffff" face="Verdana, Geneva, Arial, Sans-serif" size=4> </font>
<font style="background-color:#ffffff" face="Verdana, Geneva, Arial, Sans-serif" size=4>TypeCasting: </font>
<font style="background-color:#ffffff" face="Verdana, Geneva, Arial, Sans-serif" size=4>int intABC;</font>
<font style="background-color:#ffffff" face="Verdana, Geneva, Arial, Sans-serif" size=4>intABC = (int)DropDownList.SelectedValue;</font>
<font style="background-color:#ffffff" face="Verdana, Geneva, Arial, Sans-serif" size=4> </font>
<font style="background-color:#ffffff" face="Verdana, Geneva, Arial, Sans-serif" size=4>Converting:</font>
<font style="background-color:#ffffff" face="Verdana, Geneva, Arial, Sans-serif" size=4>int intABC;</font>
<font style="background-color:#ffffff" face="Verdana, Geneva, Arial, Sans-serif" size=4>intABC = System.Convert.ToInt32(DropDownList.SelectedValue);</font>
<font style="background-color:#ffffff" face="Verdana, Geneva, Arial, Sans-serif" size=4> </font>
<font style="background-color:#ffffff" face="Verdana, Geneva, Arial, Sans-serif" size=4>From above example which result to a good and optimized performance in terms of memory usage on the heap for objects</font>
<font style="background-color:#ffffff" face="Verdana, Geneva, Arial, Sans-serif" size=4></font> 
<font style="background-color:#ffffff" face="Verdana, Geneva, Arial, Sans-serif" size=4> Above was just an example for which selectedvalue returns string, but what if and return type is just an object and i want to convert to int/ string/ etc..</font>
<font face=Verdana size=4></font> 
<font style="background-color:#ffffff" face="Verdana, Geneva, Arial, Sans-serif" size=4>This is required if i am using any type which returns an object type value</font>
<font style="background-color:#ffffff" face="Verdana, Geneva, Arial, Sans-serif" size=4> </font>
<font style="background-color:#ffffff" face="Verdana, Geneva, Arial, Sans-serif" size=4>Is type conversion is equally costing in vb.net though programmer is not doing any casting, but i assume the CLR is doing it return.</font>
<font style="background-color:#ffffff" face="Verdana, Geneva, Arial, Sans-serif" size=4></font> 
<font style="background-color:#ffffff" face="Verdana, Geneva, Arial, Sans-serif" size=4>My concern from this post is is casting or converting really consume response time to display output to user in ASP.NET and / or the memory of the server is used a lot as boxing creates objects on heap.</font>
<font style="background-color:#ffffff" face="Verdana, Geneva, Arial, Sans-serif" size=4> </font>
<font style="background-color:#ffffff" face="Verdana, Geneva, Arial, Sans-serif" size=4>Regards,</font>
<font style="background-color:#ffffff" face="Verdana, Geneva, Arial, Sans-serif" size=4>Hardik I. Shah</font>

View the full article
 
Back
Top