Did you know the differences between the types? How many times have they confused you? 🎮 Play 7 Awesome Games (Action, Strategy, Management) and Help Support the Channel! ✅ Get the Game Bundle 67% off unitycodemonkey.com/gameBundle.php
Excellent video, which I wish I had when I started C #. Thank you for all these videos! (I bought Clean Code. I did not keep very good memories of my last big book in English, but this kind of book is rarely translated into French).
Value types are bytes of data that contain values in memory, reference types are addresses to bytes of data in memory. Reference types are pointers (C/C++) to an address, and variables/fields store data.
Great vid, thank you. One improvement could be a little diagram which pops up which shows what's going on 'under the hood'. in relation to the reference pointers/value copies.
So is it always recommended to use classes over structs? Very informative video nevertheless, but it can be good to give examples of where the misuse or best use of the methods you describe ( beginner here ).
Classes are easier to understand so for the most part that's what you should use. Structs are beneficial when performance is an issue even when not using Unity DOTS. However when working with Unity DOTS you don't have a choice, in order to benefit from all the massive performance boosts you have to use Structs.
This is really interesting. Could someone tell me a scenario where knowing this is useful? It would be interesting to hear it in real world context to help me understand it better. Any help's appreciated, thank you!
The fact that structs/classes exist in different locations in memory (stack/heap) can be a huge difference in terms of performance. I remember a long time ago when I was working on Survivor Squad and I had the Field of View code and it was taking a massive amount of time every frame, once I switched it to structs instead of classes the performance increased massively.
Hey Code Monkey...How the Data Stores of Variable and Reference??How the Code Run,Excute and Compiler in Unity ?? for example It get Convert into C++ and so on..So What will the Best Practises ??
For anybody: What's an Intuitive example of when one would choose Structs over a Class? It woudnt ever occur to me to use a struct for anything that wasn't DOTS since I've never needed one in the past. Judging by the way they behave, I also wouldn't necessarily want to use one for fear of getting confused.
The one main benefit is performance due to how structs are stored in the Stack rather than the Heap. For example if you have a massive array that you want to cycle through, it will be faster with Structs rather than Classes because of where they are stored in memory. This benefit exists even in non-DOTS code. For example in my game, Survivor Squad, I had a massive array to control the Fog of War, just a simple boolean saying hidden or visible. First I made it with classes and it was painfully slow, switched it to structs and everything worked perfect. But yes, in 95% of cases you should use Classes since they will be easier to work with and only use Structs if you absolutely need it.
I had an issue in my project where the Garbage Collector was being called too often due to a routine running in the Update. When I changed a parameter's type from class to struct the issue practically disappeared since the struct was handled by the Stack. This might not give a massive performance boost, but many different platforms and PC specs handle GC differently which could create some issues, so it's good practice to avoid overworking the GC whenever possible. What I learned from this was: If you have some data that is short lived (inside a method) and sent around the code very often as parameters (like inside an Update) it could be better to use a value type to avoid GC overwork. Always Profile the project, if you see too many GC calls then changing the way you handle data from classes to structs or other value types might be a solution.
I think your describe the information of Value Type and references but I wouldn't say this is a good video because I feel like your not teaching me "How" Value Types work. I've looked over this video a good couple times, like 6-ish and I'm certain you can clarify the relationships better for a beginner to make this more digestiable for someone to learn from. - Constructive critism, your welcome
What do you mean by "How" they work? Are you asking how the C# compiler ends up storing them in memory? Like what Assembly code is run and what CPU Registers are used? Those are extremely low level details which this tutorial is definitely not intended to teach. The goal with this tutorial is teach you how they are different in how they are used and how they behave, the super low level implementation is way beyond the scope of this tutorial. If you want you can get an assembly inspector program and inspect every low level instruction that happens when a struct is created.
please sir Can you make a video on how to make a walk animation using code (your animation system) Thanks alot. your videos are really awsome and verry helpfull
I don't think there's any built in container for strings, chances are whatever problem you're trying to solve doesn't really need strings. If you absolutely needed to use strings I guess you could use a NativeList and handle it character by character.
Question: You said which ones are value types and which ones are reference types. You demonstrated int to show that its a value type. I tried doing the same thing on string types which you said is a reference type so i did this. string A = "HELLO"; string B = A; B = "WORLD"; Debug. Log(A); //outputs HELLO If string is a reference type, the output should be WORLD, right?
String isn't a pure reference type, it's a very specific data type with a lot of exceptions. In your example yes it works as a value type, but then you can also do string A = null; Essentially a string is just a char[] which is stored in memory as a reference to that char[] and not stored as the string contents directly. So yes strings sometimes behave as value types and sometimes as reference types.
From architectural memory point, I want to understand this correctly. Struct goes into Stack, while Class are allocated dynamically into heap? Also what if I apply a pointer to to a struct in unsafe code? what will happen then?
@@anonymoussloth6687 if you define a class, like public class MyClass { } And you make a field of that type, it will construct an instance of that class
@@CodeMonkeyUnity oh I see. So if there is a script on a gameonject that has the field of Myclass, and I drag the script Myclass.cs into it in the inspector, it will create a new instance of the class? And if it's a transform or mono behaviour, then it will have a reference to it? But what if there is a mono behaviour that isn't instantiated? In other words, I just have the script but it hasn't been put on a game object yet
what a coincidence, i had this question yesterday, i pass an List as a parameter, but the items added to the list don't go to the original list, and i always believed a list was a reference type
List is a reference type and that should be working correctly so it sounds like you have a different issue. Maybe you're actually passing a copy? DoSomething(myList.ToList()); This would pass a copy instead of the original list
Hey bortha, this might not be in your road map, but can you make a tutorial on how to make a 2d sprite into pieces on trigger. (we can later add point effector to blow it ). Thanks for all these tutorials man.
Hi! Great job with these tutorials! Could you please make a video on Rendering with DOTS, like the new RenderMesh, RenderBounds and so on? I am trying to make a grid of nodes using a prefab and when the mouse clicks on one of the nodes i want to change its color but because it's shared it changes all of them. Any ideas?
I covered rendering a single sprite and then rendering 100,000 animated sprites. The trick is to use the same material and a different MaterialPropertyBlock ruclips.net/p/PLzDRvYVwl53s40yP5RQXitbT--IRcHqba
@@CodeMonkeyUnity Oh...the console showed the value of a, that's what I missed and couldn't understand why the answer was 5. You were talking about b so I thought that's what the console was showing the value of.
I know that this is a fairly old video, but I do have a question: I have a class, which holds some nested structs within in. And sometimes I do want to pass down copies of the structs, but other times I wanna pass down a reference (as read only) for faster computation times. How can I do that? I mean... the example shown in the end doesn't really work for me, because what I'd wanna do would look something like this: MyStruct asd; public void SetUp(ref MyStruct dsa) { asd = dsa; } And this would just make a copy of dsa, wouldn't it? And writing "ref" before asd just gives me computational errors.
What are you trying to do? If you need an object to behave like a class why not make it a class instead of trying to make a struct behave like a class?
@@CodeMonkeyUnity I was considering that, but wanted to hear if there is a way to do it with a struct. However, how would this behave: public struct MyStruct { public float[] values; public MyClass cls; } public MyClass { public float[] moreValues; } Would passing by the struct around make copies of the class within it, or would it be a reference? As for what I'm trying to do: I'm making a game with selectable heroes/classes. And I am keeping the hero stats and abilities within a struct. And when a hero is selected, I copy the desired struct onto them, with the idea that changes can be made in the future, with talents and such, without affecting the other heroes who use the same class. And when casting a spell, I pass on the nested struct, with the spell data within it (such as damage, range, speed, etc) to the spell.
It works by passing a reference, similar to the ref keyword The difference is you HAVE to set a value before you exit the function whereas with ref you don't have to change it
It's the opposite, objects use more memory because you need to store the object pointer in the Stack and the object data in the Heap Whereas a struct is all entirely stored in the Stack which is much faster.
The example for value type at 3:25 doesn't look right to me. Shouldn't it be like: int a = 7; int b = a; a = 5; Debug.Log(b); Like this? (When you run it it says 7, so your point is proven.) Similar with the struct example. When they're both referencing one same instance, the test on both directions are equivalent; But when they're two individual copies, I think the right way to test is to modify the one pointed at, not the one pointing.
It doesn't matter which one you test, with structs they are individual copies completely separated changing a or b would only change that copy, with classes they would be references pointing to the same object, changing a or b would change the same reference.
@@CodeMonkeyUnity Yeah I get the idea, I just think that it can't be logically proven when the test is done in reverse like this. But anyway, your point is explained clear enough, thanks for replying :)
C# Arrays are reference types, so int[] intArray; holds a reference However NativeArray is the special DOTS array which is a value type and can be used inside Burst compiled jobs.
String is actually a value type that lives on the heap because of its byte value can be large and undetermined before initialization and is a combination of char values, where a standard char will be under 2 bytes with exception of some emoji char that will throw an overflow if 3-4 bytes and must be converted as a string as char is allocated 2 bytes. The compiler will actually allocate 12-32 bytes for a string depending on operating system at declaration and double the allocation if the string exceeds it by splitting off the string and then combining it within extra registers. If you take your example for reference types string A is still equal to A even if string B = A. chars 'c' + 'a' + 't' have decimal values of c(3)+ a(1) + t(20) and in binary c(01100011) + a(01100001) + t(01110100) = string "cat" = decimal 24 or binary 011000110110000101110100 if using UTF-8bit with 16 bit adding 00000000 before each letters binary. The information that String is a reference type is incorrect.
Not sure exactly what you're talking about but straight from Microsofts website string is a reference type docs.microsoft.com/en-us/dotnet/csharp/language-reference/builtin-types/reference-types#the-string-type
@@CodeMonkeyUnity Though Microsoft has it listed as a reference String is a special case it is sometimes a value type and at other times a reference. example you can convert.Int32(string "3") but not "d' though it has a decimal value. Using your example you used in the video to distinguish types string will act as a value not a reference stringA = "bird" , StringB = string A, StringB = "fish", Debug.log(StringA) = log = "bird". where a normal reference would update stringA. Even if you did string B = new string(A) A will still store A and B will still store B and will not update A. When a string is under 20 characters it is assigned 32 bytes and will be assigned to the stack like a struct. But above 20 characters it will be assigned 64 bytes and moved to the heap to be combined acting like a reference calculation. Likely the main reason why a string is classified by Microsoft as a reference is because it can be iterated through like a char[ ] array though it is not technically an array as it does not act like char[ ] arrayChar = new char [ ] would in regards to reference.
@@CodeMonkeyUnity Nm you are correct I realized its because a new instance is created every time a string is declared as if string S = new string("some value") every time, what makes it a reference is because a reference for the variable is stored on the stack but the actual data is being stored on the heap.
Hey dude you can do a MOBA game like lol offline or online, you know if you see this please do that video if you want :D bye (yhea i copy this form the other video you uploaded XD)
Making an entire MOBA is a bit too complex for a single video but I have thought about doing some elements from it like doing the Enemy Spawning and AI
@@CodeMonkeyUnity yhea i know it's complex but you can make a serie of videos like a video of "minions spawning" and the other is "character selection" you know step by step only if you want :D
No way did he like me comment! I would just like to say you inspire me! You are talented and are just the best at these types of videos. Thank you so much 😊
am I crazy... or would it be better if there were to operators for assignments? an '=c' operator for when a copy is assigned... and an '=r' or something when you are assigning a reference. something like that. I decided to spend all this free time i've had this year from being locked indoors to learn Unity and GameDev. Now... I'm finding I need to learn C# and it's infuriating how illogical it all is. Like... I watch ten thousand tutorials on object oriented programming and classes and I think I understand it.... then I'm writing code and I'm like... wtf is vector.forward? I'm using it all the time but I never instantiate it. And now I'm learning that Vector3 is a static struct and NOTHING like a class....and now I know nothing all over again. sigh...
So.. the reason there is a difference is because of.. Minimizing memory consumption. It is all based on optimization within C. A cool feature of a language like RustLang is that every type is a value type. (If I am correct). And you can do what you said. basically, make something mutable/copyable, etc. But then again. RustLang doesn't use objects. I wouldn't look too much into tutorials if I were you. And instead, do the Try and Error method. Together with reading up on Game Design Patterns. As this would prob help you a lot more. Something which helped me with learning how to program well is to make game systems. Not a game. Just a system you could use in one. And then see where is falls short and how to do it better next time.
Did you know the differences between the types? How many times have they confused you?
🎮 Play 7 Awesome Games (Action, Strategy, Management) and Help Support the Channel!
✅ Get the Game Bundle 67% off unitycodemonkey.com/gameBundle.php
NOTE: Although string is a Reference Type, it behaves like a Value Type (immutable) When you assign a variable it receives a copy and not a pointer.
Countless hours wasted failing to debug and understand why my data was being changed, seemingly for no reason. Thank you so much.
Definetely the most underrated programming channel on the platform. Stunning content
Thanks!
This video should be the first on the playlist. Just an advice.
Thanks, I've been struggling to figure out how c# was structured, and how to manage instances, this helped a TON!
This is exactly what I needed! This problem had been vexing me for a while, thanks a bunch
Excellent video, which I wish I had when I started C #.
Thank you for all these videos!
(I bought Clean Code. I did not keep very good memories of my last big book in English, but this kind of book is rarely translated into French).
THANK YOU!! I never understood why I had to keep declaring new whatever type until now.
This clarified the difference between value types and reference types really nicely, thank you.
I'm glad you found the video helpful! Thanks!
Value types are bytes of data that contain values in memory, reference types are addresses to bytes of data in memory. Reference types are pointers (C/C++) to an address, and variables/fields store data.
I just found this video useful as a reference for a college forum discussion. Thanks CodeMonkey!
This is extremely useful! I was encountered this problem before and it is so frustrating
Great vid, thank you. One improvement could be a little diagram which pops up which shows what's going on 'under the hood'. in relation to the reference pointers/value copies.
You just taught me something today.
awesome explanation CM love your videos . keep making more Awesome videos on C# unity like this
Just want to say thank you. Great tutorial!
I learn many thing by this😀😀😀😀
Thank you for the explanation! You have some really high quality content.
Thanks, I'm glad you like the videos!
awesome tut ;) please make tuts for design pattern in unity
Thank you so much for making these tutorials :)
So is it always recommended to use classes over structs? Very informative video nevertheless, but it can be good to give examples of where the misuse or best use of the methods you describe ( beginner here ).
Classes are easier to understand so for the most part that's what you should use. Structs are beneficial when performance is an issue even when not using Unity DOTS.
However when working with Unity DOTS you don't have a choice, in order to benefit from all the massive performance boosts you have to use Structs.
perfectooo----trying to add some italian accent, but God knows i am from middle East.
Tahnks very helpful video!
wonderful, so beginner friendly
love your videos you're great! keep making more!
Thanks! I'm glad you like them!
well done description, and statements congratulations
This is really interesting. Could someone tell me a scenario where knowing this is useful? It would be interesting to hear it in real world context to help me understand it better. Any help's appreciated, thank you!
The fact that structs/classes exist in different locations in memory (stack/heap) can be a huge difference in terms of performance.
I remember a long time ago when I was working on Survivor Squad and I had the Field of View code and it was taking a massive amount of time every frame, once I switched it to structs instead of classes the performance increased massively.
Oh fair, thank you code monkey!!!!
Thank you Jacob ❤
at last i understand 😁Thanks
I'm glad the video helped! Thanks!
Nullable enums are baaaaaaad practice, normaly you use "None" as the default value.
Yeah I agree that's normally what I do but I wanted to show that there is a way to make a value type null.
@@CodeMonkeyUnity The only good use case i can think of would be database in models like nullable DateTime.
Hey Code Monkey...How the Data Stores of Variable and Reference??How the Code Run,Excute and Compiler in Unity ?? for example It get Convert into C++ and so on..So What will the Best Practises ??
For anybody: What's an Intuitive example of when one would choose Structs over a Class? It woudnt ever occur to me to use a struct for anything that wasn't DOTS since I've never needed one in the past. Judging by the way they behave, I also wouldn't necessarily want to use one for fear of getting confused.
The one main benefit is performance due to how structs are stored in the Stack rather than the Heap.
For example if you have a massive array that you want to cycle through, it will be faster with Structs rather than Classes because of where they are stored in memory.
This benefit exists even in non-DOTS code.
For example in my game, Survivor Squad, I had a massive array to control the Fog of War, just a simple boolean saying hidden or visible. First I made it with classes and it was painfully slow, switched it to structs and everything worked perfect.
But yes, in 95% of cases you should use Classes since they will be easier to work with and only use Structs if you absolutely need it.
@@CodeMonkeyUnity Fair enough, thank you so much for your reply! I'll definitely keep that in mind about the array size.
I had an issue in my project where the Garbage Collector was being called too often due to a routine running in the Update. When I changed a parameter's type from class to struct the issue practically disappeared since the struct was handled by the Stack.
This might not give a massive performance boost, but many different platforms and PC specs handle GC differently which could create some issues, so it's good practice to avoid overworking the GC whenever possible.
What I learned from this was: If you have some data that is short lived (inside a method) and sent around the code very often as parameters (like inside an Update) it could be better to use a value type to avoid GC overwork.
Always Profile the project, if you see too many GC calls then changing the way you handle data from classes to structs or other value types might be a solution.
Thanks.
I think your describe the information of Value Type and references but I wouldn't say this is a good video because I feel like your not teaching me "How" Value Types work. I've looked over this video a good couple times, like 6-ish and I'm certain you can clarify the relationships better for a beginner to make this more digestiable for someone to learn from. - Constructive critism, your welcome
What do you mean by "How" they work? Are you asking how the C# compiler ends up storing them in memory? Like what Assembly code is run and what CPU Registers are used?
Those are extremely low level details which this tutorial is definitely not intended to teach. The goal with this tutorial is teach you how they are different in how they are used and how they behave, the super low level implementation is way beyond the scope of this tutorial.
If you want you can get an assembly inspector program and inspect every low level instruction that happens when a struct is created.
You are a legend bro
please sir Can you make a video on how to make a walk animation using code (your animation system) Thanks alot. your videos are really awsome and verry helpfull
Nice video!
So, string is not usable in DOTS component, is it? Is there any built-in container for storing text in Unity?
I don't think there's any built in container for strings, chances are whatever problem you're trying to solve doesn't really need strings.
If you absolutely needed to use strings I guess you could use a NativeList and handle it character by character.
@@CodeMonkeyUnity How about NativeStringXX structs?
docs.unity3d.com/Packages/com.unity.entities@0.0/api/Unity.Entities.NativeString64.html
This is a good channel😊
I'm glad you found the video helpful!
@@CodeMonkeyUnity yeah you bet good stuff
Cool video!
thank you very much
Question: You said which ones are value types and which ones are reference types. You demonstrated int to show that its a value type. I tried doing the same thing on string types which you said is a reference type so i did this.
string A = "HELLO";
string B = A;
B = "WORLD";
Debug. Log(A); //outputs HELLO
If string is a reference type, the output should be WORLD, right?
String isn't a pure reference type, it's a very specific data type with a lot of exceptions.
In your example yes it works as a value type, but then you can also do string A = null;
Essentially a string is just a char[] which is stored in memory as a reference to that char[] and not stored as the string contents directly.
So yes strings sometimes behave as value types and sometimes as reference types.
From architectural memory point, I want to understand this correctly. Struct goes into Stack, while Class are allocated dynamically into heap? Also what if I apply a pointer to to a struct in unsafe code? what will happen then?
Pointers go to the stack so in that case you would have a pointer on the stack pointing to a struct also on the stack
When we assign things to variables in the inspector, we are assigning the reference for reference types? And copy of the value for the value types?
If it's a transform or MonoBehaviour? Yes reference.
But if you make a Class field then it will simply construct a class instance
@@CodeMonkeyUnity I got the first part. But what do u mean by class field?
@@anonymoussloth6687 if you define a class, like public class MyClass { }
And you make a field of that type, it will construct an instance of that class
@@CodeMonkeyUnity oh I see. So if there is a script on a gameonject that has the field of Myclass, and I drag the script Myclass.cs into it in the inspector, it will create a new instance of the class? And if it's a transform or mono behaviour, then it will have a reference to it? But what if there is a mono behaviour that isn't instantiated? In other words, I just have the script but it hasn't been put on a game object yet
what a coincidence, i had this question yesterday, i pass an List as a parameter, but the items added to the list don't go to the original list, and i always believed a list was a reference type
List is a reference type and that should be working correctly so it sounds like you have a different issue. Maybe you're actually passing a copy?
DoSomething(myList.ToList());
This would pass a copy instead of the original list
thank you for this tutorial
Just amazing
Make these kinda video.cool. I want to know static and heap memory management. Anyhow video is nice
Could you add this video to c# playlist, please?
nice
For beginner its better you show them why or when to use it
Great.
Thanks alot. ❤️
Hey bortha, this might not be in your road map, but can you make a tutorial on how to make a 2d sprite into pieces on trigger. (we can later add point effector to blow it ). Thanks for all these tutorials man.
You mean cut up the mesh into a bunch of separate parts?
@@CodeMonkeyUnity Yeah but something in your way.
Hi! Great job with these tutorials! Could you please make a video on Rendering with DOTS, like the new RenderMesh, RenderBounds and so on? I am trying to make a grid of nodes using a prefab and when the mouse clicks on one of the nodes i want to change its color but because it's shared it changes all of them. Any ideas?
I covered rendering a single sprite and then rendering 100,000 animated sprites. The trick is to use the same material and a different MaterialPropertyBlock
ruclips.net/p/PLzDRvYVwl53s40yP5RQXitbT--IRcHqba
@3:57 you changed the value of b to 5, so why does it equal 7 ? Does b=5 not make the variable equal 5 ?
It sets 'b' to 5 but ints are value types so the value stored in 'a' is still 7
@@CodeMonkeyUnity Oh...the console showed the value of a, that's what I missed and couldn't understand why the answer was 5. You were talking about b so I thought that's what the console was showing the value of.
I know that this is a fairly old video, but I do have a question:
I have a class, which holds some nested structs within in. And sometimes I do want to pass down copies of the structs, but other times I wanna pass down a reference (as read only) for faster computation times. How can I do that?
I mean... the example shown in the end doesn't really work for me, because what I'd wanna do would look something like this:
MyStruct asd;
public void SetUp(ref MyStruct dsa)
{
asd = dsa;
}
And this would just make a copy of dsa, wouldn't it? And writing "ref" before asd just gives me computational errors.
What are you trying to do? If you need an object to behave like a class why not make it a class instead of trying to make a struct behave like a class?
@@CodeMonkeyUnity I was considering that, but wanted to hear if there is a way to do it with a struct.
However, how would this behave:
public struct MyStruct
{
public float[] values;
public MyClass cls;
}
public MyClass
{
public float[] moreValues;
}
Would passing by the struct around make copies of the class within it, or would it be a reference?
As for what I'm trying to do: I'm making a game with selectable heroes/classes. And I am keeping the hero stats and abilities within a struct. And when a hero is selected, I copy the desired struct onto them, with the idea that changes can be made in the future, with talents and such, without affecting the other heroes who use the same class. And when casting a spell, I pass on the nested struct, with the spell data within it (such as damage, range, speed, etc) to the spell.
excellent !
LMAO why is your ad on your own video XD!!
Which one?
vector3 is a struct
The nullable structs are not shown in unity inspector. Is there a way to fix that?
what about "out" parament
It works by passing a reference, similar to the ref keyword
The difference is you HAVE to set a value before you exit the function whereas with ref you don't have to change it
Lvalue = Rvalue, but not always there are rare situation
If objects are reference types and structs are value types, does this mean that objects are more memory-efficient?
It's the opposite, objects use more memory because you need to store the object pointer in the Stack and the object data in the Heap
Whereas a struct is all entirely stored in the Stack which is much faster.
@@CodeMonkeyUnity No wonder Unity focuses more on Structs. What other engines do this?
The example for value type at 3:25 doesn't look right to me.
Shouldn't it be like:
int a = 7;
int b = a;
a = 5;
Debug.Log(b);
Like this?
(When you run it it says 7, so your point is proven.)
Similar with the struct example. When they're both referencing one same instance, the test on both directions are equivalent; But when they're two individual copies, I think the right way to test is to modify the one pointed at, not the one pointing.
It doesn't matter which one you test, with structs they are individual copies completely separated changing a or b would only change that copy, with classes they would be references pointing to the same object, changing a or b would change the same reference.
@@CodeMonkeyUnity Yeah I get the idea, I just think that it can't be logically proven when the test is done in reverse like this. But anyway, your point is explained clear enough, thanks for replying :)
200k face reveal
Are native array reference type or value type ?
C# Arrays are reference types, so int[] intArray; holds a reference
However NativeArray is the special DOTS array which is a value type and can be used inside Burst compiled jobs.
Thanks !
String is actually a value type that lives on the heap because of its byte value can be large and undetermined before initialization and is a combination of char values, where a standard char will be under 2 bytes with exception of some emoji char that will throw an overflow if 3-4 bytes and must be converted as a string as char is allocated 2 bytes. The compiler will actually allocate 12-32 bytes for a string depending on operating system at declaration and double the allocation if the string exceeds it by splitting off the string and then combining it within extra registers.
If you take your example for reference types string A is still equal to A even if string B = A. chars 'c' + 'a' + 't' have decimal values of c(3)+ a(1) + t(20) and in binary c(01100011) + a(01100001) + t(01110100) = string "cat" = decimal 24 or binary 011000110110000101110100 if using UTF-8bit with 16 bit adding 00000000 before each letters binary. The information that String is a reference type is incorrect.
Not sure exactly what you're talking about but straight from Microsofts website string is a reference type
docs.microsoft.com/en-us/dotnet/csharp/language-reference/builtin-types/reference-types#the-string-type
@@CodeMonkeyUnity Though Microsoft has it listed as a reference String is a special case it is sometimes a value type and at other times a reference. example you can convert.Int32(string "3") but not "d' though it has a decimal value.
Using your example you used in the video to distinguish types string will act as a value not a reference stringA = "bird" , StringB = string A, StringB = "fish", Debug.log(StringA) = log = "bird". where a normal reference would update stringA. Even if you did string B = new string(A) A will still store A and B will still store B and will not update A.
When a string is under 20 characters it is assigned 32 bytes and will be assigned to the stack like a struct. But above 20 characters it will be assigned 64 bytes and moved to the heap to be combined acting like a reference calculation.
Likely the main reason why a string is classified by Microsoft as a reference is because it can be iterated through like a char[ ] array though it is not technically an array as it does not act like char[ ] arrayChar = new char [ ] would in regards to reference.
@@CodeMonkeyUnity Nm you are correct I realized its because a new instance is created every time a string is declared as if string S = new string("some value") every time, what makes it a reference is because a reference for the variable is stored on the stack but the actual data is being stored on the heap.
I don't understand this part: new TestJob { testNativeArray = testNativeArray } ;
Why testNativeArray = testNativeArray?
You're assigning the testNativeArray field inside the Job with the one made before. There are 2 fields named testNativeArray
❤
Hey dude you can do a MOBA game like lol offline or online, you know if you see this please do that video if you want :D bye
(yhea i copy this form the other video you uploaded XD)
Making an entire MOBA is a bit too complex for a single video but I have thought about doing some elements from it like doing the Enemy Spawning and AI
@@CodeMonkeyUnity yhea i know it's complex but you can make a serie of videos like a video of "minions spawning" and the other is "character selection" you know step by step only if you want :D
2:21
5 SECONDS AGO
We're so EARLY!
No way did he like me comment! I would just like to say you inspire me! You are talented and are just the best at these types of videos. Thank you so much 😊
Glad to hear it!
Early!
sir explaination is very fast i cant catch up.
WOW IM EARLY
He lost me with the DOTS implementation
am I crazy... or would it be better if there were to operators for assignments? an '=c' operator for when a copy is assigned... and an '=r' or something when you are assigning a reference. something like that. I decided to spend all this free time i've had this year from being locked indoors to learn Unity and GameDev. Now... I'm finding I need to learn C# and it's infuriating how illogical it all is. Like... I watch ten thousand tutorials on object oriented programming and classes and I think I understand it.... then I'm writing code and I'm like... wtf is vector.forward? I'm using it all the time but I never instantiate it. And now I'm learning that Vector3 is a static struct and NOTHING like a class....and now I know nothing all over again. sigh...
So.. the reason there is a difference is because of.. Minimizing memory consumption. It is all based on optimization within C. A cool feature of a language like RustLang is that every type is a value type. (If I am correct). And you can do what you said. basically, make something mutable/copyable, etc. But then again. RustLang doesn't use objects.
I wouldn't look too much into tutorials if I were you. And instead, do the Try and Error method. Together with reading up on Game Design Patterns. As this would prob help you a lot more. Something which helped me with learning how to program well is to make game systems. Not a game. Just a system you could use in one. And then see where is falls short and how to do it better next time.