why theres 2 different data types
Posted by Extra-Recording-7493@reddit | learnprogramming | View on Reddit | 28 comments
after a lot of confusion i found out storing data had 2 different types called value type and reference type (atleast in c#)
while i do understand them i have no idea why we need 2 different data types for storing data . like why not 1 or 3 or maybe 10 , whats so usefull about these 2 type that many programming languages use them consistently
justaguyonthebus@reddit
The type can either 1) contain the data or 2) tell me where the data is.
You could argue that a third type would be NULL, the not yet undefined. But I can't imagine any other types in this context.
But the reason for the two is there are two memory frameworks. You have the stack memory and heap memory. So if the data is in the heap, your type in the stack needs to be a reference type to say that your princess is in another castle.
coderemover@reddit
We don’t. It’s just stupid C# design. The problem is that originally every variable was implicitly a reference to an object, and C# (similarly to Java) didn’t make a syntactical distinction between references and values like C, C++, Rust or Go do. Then it turned out having all variables be references by default is not performant and often we really want to specify just the type of the value, without making it a reference. So they introduced a new concept „value types”, with different semantics.
In many programming languages there is no such distinction. There are no value types and no reference types, there are just types, and variables are passed always by value. References are just ordinary types as everything else and you can usually make a reference / pointer to a value by special syntax.
gurishtja@reddit
Maybe you need to learn some bassics of CS before going to c# ?
GreenRangerOfHyrule@reddit
This has taken me a while to understand as well. So here is a somewhat poor explantion.
Let's say we were talking and it came up that I know a good cookie recipe. There are 2 ways I can provide this recipe to you:
I can take out a piece of paper and write down the recipe and hand it to you. This would be a way of passing it by value. Because you hold in your hand the actual data.
Another ways is I can send you a link to a web page. For the sake of this discussion this link will take you to a wiki. This would be passing it by reference. Because the piece of paper doesn't contain the recipe. It contains a link (reference) too get the recipe.
Now, for how they differ. Let's say the recipe I gave you was for chocolate chip cookies. You decide that you don't like that and replace the chocolate chips with butter scotch chips. In the first example when you modify the recipe since you have the actual recipe, the changes are reflected solely on your copy. My copy is unaltered. In the second example if you edited the wiki page anyone who goes to that link will see a butterscotch chip recipe.
Another way to look at it is if you go and tell someone else and they want the recipe. You can take out a piece of paper and copy the recipe down and give that to them. Or you can write down the link to the wiki. With the first option both of you can make changes without either seeing it. In the second if one of you makes a change the other will see it.
I'm certain there are much better ways to explain it. But hopefully that makes some level of sense. Because now I kinda want a cookie...
xenomachina@reddit
OP isn't asking about passing by value and passing by reference. They are asking about value types and reference types, which are a distinct (but related, and similarly named) concept.
GreenRangerOfHyrule@reddit
As I mentioned it was an poor attempt to explain it.
I did read the comment. And I just wanted to make sure I understood it. I do see how it was incorrect and/or misleading to use pass-by. But assuming the physical paper represented the the value (either the full recipe or a link to a wiki to get the recipe) it still holds?
Because if not, then I apparently don't understand it all... And the above advice should be discarded
xenomachina@reddit
Your analogy is good for describing the difference between references and values, but because it's geared around "passing" the recipe, it's a better fit for describing pass by reference and pass by value, rather than reference types and value types.
GreenRangerOfHyrule@reddit
Fair enough. That is on me.
I do appreciate you point it out though. And hopefully it will be of value to someone
jay_thorn@reddit
Here's your cookie. 🍪😋
mxldevs@reddit
Based on your understanding, what third type would you propose?
DTux5249@reddit
They're not different data types, they're two fundamental pieces of information about data.
Whenever you declare a variable in a program, that variable points to a particular place in your computer's storage. Your computer is just an electric abacus; a bunch of cubbies that can hold numbers. Each cubby has an address so your computer can find it.
Value = whatever number you put in the cubby.
Reference = the cubby itself; its address.
If I pass a variable into a function by its value, that function creates its own copy of that variable to use (same data, stored somewhere else). Changing it in that function will do nothing outside of that function, because once the function finishes, that address is free to be overwritten by your computer.
If I pass the variable by its reference into a function though, any changes I make to that variable will go straight to that address - changing it for every other part of the program currently using that variable.
Look up the concept of pointers. This distinction is fundamental to understanding what a computer is, and how it works.
xenomachina@reddit
OP isn't asking about passing by value and passing by reference. They are asking about value types and reference types, which are a distinct (but related, and similarly named) concept.
mikiencolor@reddit
There not two types of data. They're two different concepts. A reference is a pointer to data in memory. A value is the actual data. If you pass data by value, you're copying the data in your local context. If you pass data by reference, you are only copying the memory address of the original data.
1 --> 1
refA --> 1 <-- refB
Passing by reference is useful when you want to call some function to change the state of your data. Especially if your data is big or complex. Generally speaking, usually primitives, like integers, and other types that fit neatly into CPU registers, are passed by value, and complex types like classes are passed by reference. References are memory addreses, which are int64 values that also fit neatly into CPU registers.
xenomachina@reddit
Value types vs reference type and pass-by-value vs pass-by-reference sound like similar distinctions, and are related concepts, so it's easy to conflate them, but they are not the same thing.
In C# (like Java) there are both value types and reference types.
In C# (like C++) you can pass by value or pass by reference.
And you can intermix these, so you can even pass values by reference or pass references by value:
Pass value by value:
Pass reference by value:
Passing by value is the default. To enable passing by reference for a parameter, you use the
refkeyword.Pass value by reference:
Pass reference by reference:
The distinction between value types and reference types is whether a variable of that type contains the value itself or just a reference (effectively a pointer) to it.
The distinction between pass by value and pass by reference is whether a variable passed as an argument to a function can be modified in the function.
Many people get confused by passing references by value and think that it is pass by reference, because a function can mutate the value that the variable references, but pass by reference means the variable itself can be changed, not just a value that it references.
Most modern languages only support pass by value, while many modern languages have either both reference types and value types (Java, Swift, etc.) and some (like Python and Ruby) only have reference types.
BeauloTSM@reddit
Value type = the data
Reference type = where the data is
These are the two fundamental ways to represent data. You can theoretically make as many data types as you want, but they would just end up being variations of those.
Informal-Chance-6067@reddit
I was just going to make a (likely incorrect) joke about Java and integers but realized OP mentioned C#
sixtyhurtz@reddit
In order to get your head around this, I really recommend you learn about values, pointers, and references in C++.
C# references are a mish-mash between pointers and references. They can be null, but you can't do pointer math. Behind the scenes they are the same thing though - they are a value that points to the actual object you want.
This is not a pipe. It's a representation of a pipe. You can use it to refer to a pipe, but it is a different thing.
RecursiveServitor@reddit
Just to add to the confusion, C# has actual pointers as well.
binarycow@reddit
Then there are pointer like things that aren't pointers.
reffields and variables, spans, etc.SillyBrilliant4922@reddit
Where do you think that value is stored?
Extra-Recording-7493@reddit (OP)
computar
marvinvis@reddit
As a suggestion watch Rich Hickey the value of values.
https://youtu.be/-6BsiVyC1kM?is=4prGqItPdI91F75h
yopla@reddit
The value type is the sandwich. The reference type is the location of the sandwich.
I don't know where I was going with that analogy... 😂
NeinJuanJuan@reddit
This is a very simplified explanation:
If you want a box (Box A), I could give it to you. But the box might be very heavy, so instead of giving it to you, I might tell you to go and find it yourself. So I give you a small Box B that contains the location of large Box A)
Box A is the value.
Box B is the reference.
The details are much more complicated than this. But the above lies should serve you well until you're ready to learn new, more advanced lies.
jay_thorn@reddit
Pass-by-value and pass-by-reference are not data types, they're parameter-passing strategies.
In some programming languages, like C#, data types are classified as either value types (C# structs) or reference types (C# classes).
Here's a Wikipedia article talking about value types and reference types: https://en.wikipedia.org/wiki/Value_type_and_reference_type
RecursiveServitor@reddit
There are managed types and inline types. Managed types are tracked by the garbage collector and cleaned up when not in use. Inline types are stored inline in whatever context they're allocated in. Typically either the callstack or inside a managed type. They're cleaned up with their context.
There are also pointers to native memory. In C# you can represent these as inline types. They're cleaned up (freed) by the programmer. If the programmer forgets the application will have a memory leak.
The above is a gross simplification. Handling memory is complex and millions of dollars have been spent on making sure you don't really have to worry about it most of the time.
johnpeters42@reddit
Because they represent a yes/no option, and there are situations where you want one option, and there are others where you want the other option. Also, one of them is faster, which may be significant if that code runs often enough.
grantrules@reddit
I can't describe it any better than this: https://www.microsoftpressstore.com/articles/article.aspx?p=3131577