![]() |
|
![]() |
|
|
Thread Tools | Display Modes |
|
|
#1 |
|
Programmer
Join Date: Aug 2005
Location: null
Posts: 40
Rep Power: 0
![]() |
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 ![]() |
|
|
|
|
|
#2 | |
|
Professional Programmer
Join Date: May 2006
Location: UK - London
Posts: 329
Rep Power: 3
![]() |
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:
|
|
|
|
|
|
|
#3 |
|
Hobbyist Programmer
|
__________________
I have never let my schooling interfere with my education. -Mark Twain- Xbox live gamertag: melbolt |
|
|
|
|
|
#4 |
|
Unverified User
Join Date: Dec 2006
Location: Bristol UK
Posts: 19
Rep Power: 0
![]() |
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. |
|
|
|
|
|
#5 |
|
Hobbyist Programmer
Join Date: Nov 2006
Location: 163H
Posts: 213
Rep Power: 2
![]() |
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. |
|
|
|
|
|
#6 | |
|
Hobbyist Programmer
Join Date: Nov 2006
Location: 163H
Posts: 213
Rep Power: 2
![]() |
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:
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. |
|
|
|
|
|
|
#7 | |
|
Unverified User
Join Date: Dec 2006
Location: Bristol UK
Posts: 19
Rep Power: 0
![]() |
Quote:
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. |
|
|
|
|
|
|
#8 | |
|
Hobbyist Programmer
Join Date: Nov 2006
Location: 163H
Posts: 213
Rep Power: 2
![]() |
Im soooo sorry we are speaking english and yet not understanding each-other.
You said : Quote:
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. |
|
|
|
|
|
|
#9 |
|
Hobbyist Programmer
Join Date: Nov 2006
Location: 163H
Posts: 213
Rep Power: 2
![]() |
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. |
|
|
|
|
|
#10 | |
|
Unverified User
Join Date: Dec 2006
Location: Bristol UK
Posts: 19
Rep Power: 0
![]() |
Quote:
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. |
|
|
|
|
![]() |
| Bookmarks |
| Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | |
| Thread Tools | |
| Display Modes | |
|
|
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 |