Kazekage Gaara
Kazekage Gaara

Reputation: 15052

struct as wrapper for value types in C#

I have just begun with C#. I noticed that all the value types are wrapped into respective structs. I did look up for the reason behind it, couldn't find much though(maybe I didn't search properly). I just wanted to ask - what is the reason that value types are wrapped as structs in C# as opposed to wrapper classes in Java?

EDIT:

When I hover onto int, I guess it shows me this : http://msdn.microsoft.com/en-us/library/system.int32%28v=vs.71%29 . From the comments, I understand that I might be understanding something wrong here. If int isn't wrapped into that struct, then why does it show information about that?

Upvotes: 1

Views: 1207

Answers (1)

Marc Gravell
Marc Gravell

Reputation: 1063013

I believe this is just a bit of confusion over what the IDE is telling you. Firstly, unlike Java there is absolutely no difference between int and Int32. It isn't the "primitive" and "boxed" versions of the same thing; simply, int is the c# name for convenience; the full name (since the CLI supports other languages etc) is global::System.Int32; but that is just an alias; they are identical and interchangeable.

Secondly, struct is a synonym for "value-type"; again, same meaning (mostly).

There is no wrapping here; when the IDE talks about "struct Int32" it is also talking about "primitive int". They are the same. The main point is: int is treated as a value not a reference, which means it is a "struct"; that is all that is trying to tell you, i.e. "not a reference".

Upvotes: 10

Related Questions