Programming Forums
User Name Password Register
 

RSS Feed
FORUM INDEX | TODAY'S POSTS | UNANSWERED THREADS | ADVANCED SEARCH

Reply
 
Thread Tools Display Modes
Old Jan 17th, 2007, 7:28 AM   #1
jonyzz
Programmer
 
jonyzz's Avatar
 
Join Date: Aug 2005
Location: null
Posts: 40
Rep Power: 0 jonyzz is on a distinguished road
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
jonyzz is offline   Reply With Quote
Old Jan 17th, 2007, 9:54 AM   #2
kruptof
Professional Programmer
 
kruptof's Avatar
 
Join Date: May 2006
Location: UK - London
Posts: 330
Rep Power: 3 kruptof is on a distinguished road
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
__________________
Quote:
When I was young it seemed that life was so wonderful,a miracle, oh it was beautiful, magical.
Now watch what you say or they'll be calling you a radical,a liberal, oh fanatical, criminal. Oh won't you sign up your name,we'd like to feel you're acceptable, respectable, oh presentable, a vegetable
kruptof is offline   Reply With Quote
Old Jan 17th, 2007, 9:59 AM   #3
melbolt
Hobbyist Programmer
 
melbolt's Avatar
 
Join Date: Feb 2005
Location: PA, USA
Posts: 242
Rep Power: 4 melbolt is on a distinguished road
Send a message via AIM to melbolt Send a message via Yahoo to melbolt
http://dotnetjunkies.com/WebLog/chri.../17/11646.aspx
__________________
I have never let my schooling interfere with my education. -Mark Twain-

Xbox live gamertag: melbolt
melbolt is offline   Reply With Quote
Old Jan 17th, 2007, 10:25 AM   #4
Tim
Unverified User
 
Join Date: Dec 2006
Location: Bristol UK
Posts: 19
Rep Power: 0 Tim is on a distinguished road
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.
Tim is offline   Reply With Quote
Old Jan 17th, 2007, 10:29 AM   #5
pegasus001
Hobbyist Programmer
 
pegasus001's Avatar
 
Join Date: Nov 2006
Location: 163H
Posts: 213
Rep Power: 2 pegasus001 is on a distinguished road
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"
__________________
You never test the depth of a river with both feet.
The believer is happy. The doubter is wise.
Free speech carries with it some freedom to listen.
The next generation will always surpass the previous one. It`s one of the never ending cycles of life.
pegasus001 is offline   Reply With Quote
Old Jan 17th, 2007, 10:39 AM   #6
pegasus001
Hobbyist Programmer
 
pegasus001's Avatar
 
Join Date: Nov 2006
Location: 163H
Posts: 213
Rep Power: 2 pegasus001 is on a distinguished road
Quote:
Originally Posted by Tim View Post
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 View Post
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.
__________________
You never test the depth of a river with both feet.
The believer is happy. The doubter is wise.
Free speech carries with it some freedom to listen.
The next generation will always surpass the previous one. It`s one of the never ending cycles of life.
pegasus001 is offline   Reply With Quote
Old Jan 17th, 2007, 11:08 AM   #7
Tim
Unverified User
 
Join Date: Dec 2006
Location: Bristol UK
Posts: 19
Rep Power: 0 Tim is on a distinguished road
Quote:
Originally Posted by pegasus001 View Post
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.
Tim is offline   Reply With Quote
Old Jan 17th, 2007, 11:18 AM   #8
pegasus001
Hobbyist Programmer
 
pegasus001's Avatar
 
Join Date: Nov 2006
Location: 163H
Posts: 213
Rep Power: 2 pegasus001 is on a distinguished road
Im soooo sorry we are speaking english and yet not understanding each-other.

You said :
Quote:
Originally Posted by Tim View Post
Boxing referes to a value type that has been put on the heap inside an Object.
I said :
Quote:
Originally Posted by pegasus001 View Post
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.
__________________
You never test the depth of a river with both feet.
The believer is happy. The doubter is wise.
Free speech carries with it some freedom to listen.
The next generation will always surpass the previous one. It`s one of the never ending cycles of life.
pegasus001 is offline   Reply With Quote
Old Jan 17th, 2007, 11:26 AM   #9
pegasus001
Hobbyist Programmer
 
pegasus001's Avatar
 
Join Date: Nov 2006
Location: 163H
Posts: 213
Rep Power: 2 pegasus001 is on a distinguished road
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.
__________________
You never test the depth of a river with both feet.
The believer is happy. The doubter is wise.
Free speech carries with it some freedom to listen.
The next generation will always surpass the previous one. It`s one of the never ending cycles of life.
pegasus001 is offline   Reply With Quote
Old Jan 17th, 2007, 11:28 AM   #10
Tim
Unverified User
 
Join Date: Dec 2006
Location: Bristol UK
Posts: 19
Rep Power: 0 Tim is on a distinguished road
Quote:
Originally Posted by pegasus001 View Post
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.
Tim is offline   Reply With Quote
Reply

Bookmarks

« Previous Thread in Forum | Next Thread in Forum »

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
How do I take a square root? Fall Back Son C 26 Oct 23rd, 2006 12:24 PM
HELP I dont know y this has an error PLEASEHELP C++ 9 Aug 4th, 2005 5:12 PM
C at College, advice if you please. Ramlag C 10 Apr 29th, 2005 9:53 PM
help out beginner with some errors (solved) monka Java 8 Mar 3rd, 2005 8:50 PM
Y for yes and N for no problem sobank C++ 4 Mar 1st, 2005 10:33 PM




DaniWeb IT Discussion Community
All times are GMT -5. The time now is 3:07 PM.

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