0

In case of Java when we write something like

Integer i = new Integer(20);

In the above case the obj is allocated on the heap

and in case of C#

Int32 i = new Int32();
i=10;

In this case the object is allocated on the stack

Are these just the difference in implementation wise or there are more differences too?

Correction: changed Integer to Int32 for C#

Akash
  • 4,956
  • 11
  • 42
  • 70
  • How you define `Integer` for C#? (there is `System.Int32`/`int`, but no `Integer`)... – Alexei Levenkov Jul 03 '12 at 04:36
  • C# doesn't have explicit types representing boxed primitives. It's just an object that could be cast back to the appropriate type. It's nowhere near as complicated as it is in Java. – Jeff Mercado Jul 03 '12 at 04:38

2 Answers2

3

There is no Integer in C#, its either int or int32 and both are same. With respect to C# saying "Value types go to stack" is some what not correct. You need to see this article from Eric Lippert: The Truth About Value Types

Edit: based on comment:

Int32 and int are same, the two are synonymous.

Habib
  • 219,104
  • 29
  • 407
  • 436
3

Many of the reasons you may wish to wrap a primitive in Java don't apply to C# because C# has structs instead of primitives. The following are some of the things structs can be used for that can't be done with prmitives:

When they are needed on the heap they can be boxed. This link explains it http://msdn.microsoft.com/en-us/library/yz2be5wk.aspx.

Christian Maslen
  • 1,100
  • 9
  • 13