Search code examples
c#.netreference-type

Why do we need reference types in .NET


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?


Solution

  • 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.