Why do we need reference types in .NET
Asked Answered
A

4

5

Why do we need reference types in .NET?

I can think of only 1 cases, that it support sharing data between different functions and hence gives storage optimization.

Other than that I could not enumerate any reason, why reference types are needed?

Amhara answered 10/2, 2012 at 17:36 Comment(5)
How would you implement a linked list or binary tree without reference types?Eldoraeldorado
Seems like it could get a bit slow if you had to copy massive data structures across the heap every time you passed them to another function..Sublingual
You don't need reference types, you could just use a pointer, passed by value. But then we've come full circle.Shaven
@Tilak: Why do you need pointers in C++?Hellbox
Because if you had to use raw pointers instead, .NET wouldn't be "managed" anymore and lose one of its primary advantages.Hyperbaric
C
28

Why do we need reference types in .NET? I can think of only one reason: that it support sharing of data and hence gives storage optimization.

You've answered your own question. Do you need a better reason than that?

Suppose every time you wanted to refer to the book The Hobbit, you had to instead make a copy of the entire text. That is, instead of saying "When I was reading The Hobbit the other day...", you'd have to say "When I was reading In a hole in the ground there lived a hobbit... [all the text] ... Well thank goodness for that, said Bilbo, handing him the tobacco jar. the other day..."

Now suppose every time you used a database in a program, instead of referring to the database, you simply made a full copy of the entire database, every single time you used any of it in any way. How fast do you think such a program would be?

References allow you to write sentences that talk about books by use of their titles instead of their contents. Reference types allow you to write programs that manipulate objects by using small references rather that enormous quantities of data.

Clo answered 10/2, 2012 at 17:46 Comment(6)
Did you quote that from memory, or do you have a copy of The Hobbit in your office?Rochelrochell
+1 - It would not surprise me in the least if @Eric had much more than this of The Hobbit committed to memory.Sublingual
@phoog, you may find this interestingRoentgenology
@phoog: From memory, and of course I got it wrong. The real last line is "Thank goodness!" said Bilbo laughing, and handed him the tobacco-jar.Clo
(tries hard not to mention that outstanding compiler question thing about optional parameters)Burlingame
@MarcGravell Would that comment itself not constitute failure? :PCountrified
B
10
class Node {
    Node parent;
}

Try implementing that without a reference type. How big would it be? How big would a string be? An array? How much space would you need to reserve on the stack for:

string s = GetSomeString();

How would any data be used in a method that wasn't specific to one call-path? Multi-threaded code, for example.

Burlingame answered 10/2, 2012 at 17:42 Comment(0)
H
5

Three reasons that I can think of off the top of my head.

  1. You don't want to continually copy objects every time you need to pass them to a Method or Collection Type.

  2. When iterating through collections, you may want to modify the original object with new values.

  3. Limited Stack Space.

Haldes answered 10/2, 2012 at 17:42 Comment(0)
E
0

If you look at value types like int, long, float you can see that the biggest type store 8 bytes or 64 bits.

However, think about a list or an array of long values, in that case, if we have a list of 1000 values then the worst case will take 8000 bytes.

Now, to pass by value 8000 bytes will make our program to run super slow, because the function that took the list as a parameter will now have to copy all these values into a new list and by that we loose time and space.

That's why we have reference types, because if we pass that list then we don't lose time and space to copy that list because we pass the address of the list in the memory.

The reference type in the function will work on the same address as the list you passed, and if you want to copy that list you can do that manually.

By using reference types we save time and space for our program because we don't bother to copy and save the argument we passed.

Eberhard answered 14/2, 2021 at 17:38 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.