GeekInterview.com
   Home |  Tech FAQ  |   Interview Questions |  Placement Papers |  Tech Articles |  Learn |  Freelance Projects |  Online Testing |  Geeks Talk |  Job Postings |  Knowledge Base | Site Search |  Add/Ask Question

  GeekInterview.com  >  Interview Questions  >  Microsoft  >  C#

 Print  |  
Question:  Variable Declaration and Assignment

Answer: What is difference between these two statement? if variable is declared and assign value in following two ways

String sValue = (String)GridView1.DataKeys[GridView1.SelectedIndex].Value;
String sValue = GridView1.DataKeys[GridView1.SelectedIndex].Value.ToString();


October 10, 2009 08:46:42 #2
 amackail   Member Since: October 2009    Total Comments: 1 

RE: Variable Declaration and Assignment
 

The only difference between the two is in the second example you are actually
doing the boxing of the value type into a reference type onto the stack. As
every type in C# is derived from the type 'object', they all have the .ToString
method. The .ToString method does this boxing (or copying) for you. If you were
to look into the IL code you would see that a copy of the initial value type is
created as a reference type when the toString method is used with a line that
looks something like this


IL_0004: box [mscorlib]System.Int32.


In short, these two actions are the same to the CLR.

     

 

Back To Question