Programming Forums

Programming Forums (http://www.programmingforums.org/forumindex.php)
-   C# (http://www.programmingforums.org/forum16.html)
-   -   Difference between double and Double in C# (http://www.programmingforums.org/showthread.php?t=12391)

jonyzz Jan 17th, 2007 8:28 AM

Difference between double and Double in C#
 
Hi,
I was wondering if there is a difference between double and Double, between string and String, int and Int32 .. etc. in C#. I know that they all are primitive types, and that "int" for example is a synonym for Int32 in C#. But I was wondering - if int is a value type, does it mean that Int32 is a value type too? (it is a class as far as I know). And one more. Suggest we have the following code:

:


int i = 5;

Int32 i1 = 5;

i.ToString(); // i is boxed as far as i know

i1.ToString(); // is i1 boxed too ?


So i am wondering if Int32 is also being boxed.
Best regards :)

kruptof Jan 17th, 2007 10:54 AM

what do you mean by boxed?
if i can remember correclty i think the highest value that can be stored in an 32 bit integer is 2^32 - 1(i think) so in base 10 the highest number would be 4,294,967,295 and thats for unsigned (i think). For the value type thing i think this site found through google might explain a bit about it. http://www1.cs.columbia.edu/~lok/csh...pes/Int32.html

melbolt Jan 17th, 2007 10:59 AM

http://dotnetjunkies.com/WebLog/chri.../17/11646.aspx

Tim Jan 17th, 2007 11:25 AM

Neither values are Boxed. Both i and i1 are value types on the stack. Int32 and all the other .net primatives are implemented as structs. This means they have methods and properties etc and are created on the stack. If you come from a c/c++ or java background it can be a little weird that the primatives have methods. It is one of the reason people claim that C# is a truly object oriented language because its primatives have methods.

<randomrant>
On a side note C# structs are dangerious as people make them mutable and then all sorts of funny things happen as they then treat them like they are heap allocated and referenced. The only thing they are useful for is short lived "objects" as there is less overhead in creating them as they are stack allocated.
</randomrant>

To Box an int you have to put it into a object of type Object. This will box it and put it on the heap. C# autoboxes/unboxes this so it can cause problems to novices as they dont have a clue. This is one of the reasons Java held back on autoboxing/unboxing for years because they thought it would lead to confusion and I tend to agree. Putting a cast in is not hard and it make code easier to understand but slightly more cluttered.

pegasus001 Jan 17th, 2007 11:29 AM

It has been a while since i have programmed in C#. But microsoft claims C# to be fully oop. By this I mean that there is no radical difference between them, only Double is an encapsulates (i think) the System.double thing. You must also know that everything in C#(aslo applies to Java too) inherits from the object class that is predefined by the language. So have a look "here"

pegasus001 Jan 17th, 2007 11:39 AM

Quote:

Originally Posted by Tim (Post 122734)
Neither values are Boxed.

I don`t quite agree. Because every type in c# inherits the Object class, so how ca you say that they aren`t boxed. They have methods or you forgot :
Quote:

Originally Posted by Tim (Post 122734)
It is one of the reason people claim that C# is a truly object oriented language because its primatives have methods.

So this means that they are inside(boxed into) something.
And also C# has no primitive types, because it is oop, it only has predefined types.

Tim Jan 17th, 2007 12:08 PM

Quote:

Originally Posted by pegasus001 (Post 122736)
I don`t quite agree. Because every type in c# inherits the Object class, so how ca you say that they aren`t boxed. They have methods or you forgot :


So this means that they are inside(boxed into) something.
And also C# has no primitive types, because it is oop, it only has predefined types.


No not really. Structs inherit from System.ValueType (which does inherit from System.Object but thats irrelevent). There is a huge difference between stucts and classes in C# (it has nothing to do with default member visability like C++). When you create an instance of a struct it is stack allocated and the variable holding it is actually the value. An instance of a class is heap allocated and the variable references(or has the address of) the instance of it. Struct types allow you to have methods like ToString or whatever but that has nothing to do with the fact thats its heap or stack allocated.

If you box a struct (or a primative -same thing) then it gets put on the heap and you have a variable that holds the address of it. This is what boxing really is as you are putting your value type and shoving it into an Object that is on the heap.

What you imply is that when you call a method a something which is a struct type it gets boxed into an object on the heap and then called. This would be a huge waste of resources and is not the case. Boxing does not mean that it is inside something at all. Boxing referes to a value type that has been put on the heap inside an Object.

pegasus001 Jan 17th, 2007 12:18 PM

Im soooo sorry we are speaking english and yet not understanding each-other.

You said :
Quote:

Originally Posted by Tim (Post 122741)
Boxing referes to a value type that has been put on the heap inside an Object.

I said :
Quote:

Originally Posted by pegasus001 (Post 122736)
I don`t quite agree. Because every type in c# inherits the Object class.....

.....So this means that they are inside(boxed into) something.

Practically the same. Dont you think. But now that i read it it is true that they are structs. I learned sth today.

pegasus001 Jan 17th, 2007 12:26 PM

And also i dont care if they are put on the heap or they remain in the stack. All i want is that when i pass it in a function i want the reference to be passed as expected. I dont care if they do it by putting it on the heap or wherever.

Tim Jan 17th, 2007 12:28 PM

Quote:

Originally Posted by pegasus001 (Post 122742)
Practically the same. Dont you think. But now that i read it it is true that they are structs.

No boxing refers to a value type put inside an object on the heap. Its only that. Int32, Double etc are not inside anything. They are just there on the stack. Example

:

    int i = 90; //Not Boxed on stack
    Object o = i; //Boxed on heap
    int j = (int) u //Unboxed and back on heap.


This is a very important thing to learn. The difference between stucts and classes is huge. People who use a struct(value type) type like it is a reference type(class) will run into problems at some point, believe me I have.


All times are GMT -5. The time now is 1:57 AM.

Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC