C# Value Types and Reference Types

  Рет қаралды 4,698

Coding Tutorials

Coding Tutorials

3 жыл бұрын

Coding Tutorial: In .NET, there is a fundamental division between value types and reference types. All is explained here.
(Yes, I know I wrote 'CreateOjects' not 'CreateObjects'.)
Source code available at github.com/JasperKent/Val-and...

Пікірлер: 37
@CodingTutorialsAreGo
@CodingTutorialsAreGo 3 жыл бұрын
Anything other fundamental features of C# you're interested in? Let me know. Source code at: github.com/JasperKent/Val-and-Ref-Types Don't forget to subscribe at kzfaq.info/love/qWQzlUDdllnLmtgfSgYTCA And if you liked it, click the 👍.
@finwwwfinwww4669
@finwwwfinwww4669 4 ай бұрын
If we have a structure with a string type (or class ) as one of its property, how are they handled, are they still placed on Stack , or just string property is explicitly placed on heap ?
@bioanu
@bioanu 3 жыл бұрын
Excellent excellent excellent! Learning programming becomes simple when you are lucky enough to discover such a superb explanation!! Thank you very much!!
@alexpajp123
@alexpajp123 2 жыл бұрын
This is the best explanation i've heard on this topic.
@dolusdirectu
@dolusdirectu 3 жыл бұрын
Wow, the best explanation I have seen so far. Thx for this!
@Fernando-mh7st
@Fernando-mh7st Жыл бұрын
Great explanation!
@zachgh6111
@zachgh6111 3 жыл бұрын
These are brilliant - keep up the great work and thanks for your time!
@CodingTutorialsAreGo
@CodingTutorialsAreGo 3 жыл бұрын
Glad you like them!
@yegorkatrechko1736
@yegorkatrechko1736 Жыл бұрын
Thanks a lot! That is really helpful, your explanation is clear and easy to understand!
@user-yi4zd6gh5h
@user-yi4zd6gh5h 3 жыл бұрын
Great video. The best explanation i have ever seen on KZfaq
@CodingTutorialsAreGo
@CodingTutorialsAreGo 3 жыл бұрын
Glad it was helpful!
@jonlicht4514
@jonlicht4514 Жыл бұрын
Well explained, thank you.
@davidwhite2011
@davidwhite2011 3 жыл бұрын
There is a good article that you can find on real world performance that you can google. "Stack allocation vs heap allocation - performance benchmark". But any way on 1 million loops with 10 allocation+init+free attempts, we see that difference is about 8% at 60KB total processing size. Which also also not a big show stopper.
@Pluvo2for1
@Pluvo2for1 2 ай бұрын
I like to think of passing by reference as passing by value. Instead of passing a copy of a normal value type, we pass a copy of a memory address on the heap.
@John_Macaroni
@John_Macaroni Жыл бұрын
awesome video, thanks!
@davidmoshkowitz4043
@davidmoshkowitz4043 2 жыл бұрын
Amazing!!
@CuriousCyclist
@CuriousCyclist 2 жыл бұрын
Love your videos. You should do a video about correctly implementing the Clone method/interface.
@CodingTutorialsAreGo
@CodingTutorialsAreGo 2 жыл бұрын
I'll put it on the list.
@CuriousCyclist
@CuriousCyclist 2 жыл бұрын
@@CodingTutorialsAreGo Thanks. I look forward to it.
@kiPROBRos
@kiPROBRos 3 жыл бұрын
Excelent tutorial, thanks.
@zanagi
@zanagi 2 ай бұрын
lul c# is really funny when one comes from c++ background... I was so lost.
@DedicatedManagers
@DedicatedManagers 3 жыл бұрын
Another great explanation! Thank you! Also... what program did you use to create your sack pointer animation on the right side at the first half of the video?
@CodingTutorialsAreGo
@CodingTutorialsAreGo 3 жыл бұрын
All done in Adobe Premiere Pro.
@DedicatedManagers
@DedicatedManagers 3 жыл бұрын
@@CodingTutorialsAreGo I bet That took a fair amount of extra time. Well worth it though! It really made things clear. Thank you!
@CodingTutorialsAreGo
@CodingTutorialsAreGo 3 жыл бұрын
Yeah, it takes a while. When I'm teaching live, I just use a virtual whiteboard, which does it in real time. But it's such a mess, I don't want it recorded for posterity.
@nononnomonohjghdgdshrsrhsjgd
@nononnomonohjghdgdshrsrhsjgd Жыл бұрын
Very good! And how are the p1 p2 fred (names of objects) stored? In which part of the memory?
@CodingTutorialsAreGo
@CodingTutorialsAreGo Жыл бұрын
On the stack, as shown.
@nononnomonohjghdgdshrsrhsjgd
@nononnomonohjghdgdshrsrhsjgd Жыл бұрын
@@CodingTutorialsAreGo I meant the names itself. For example address 2 in stack memory contains a reference/address of where an object on the heap is stored. Where is the name p1 stored?
@CodingTutorialsAreGo
@CodingTutorialsAreGo Жыл бұрын
@@nononnomonohjghdgdshrsrhsjgd It's not. Local variable names are removed during compilation leaving only the positions on the stack.
@kiPROBRos
@kiPROBRos 3 жыл бұрын
12:56
@hristoiliev7767
@hristoiliev7767 2 жыл бұрын
What will happen in Stack if we have: var a = 5; a = 6; Will the compiler overrite the value of the existing variable "a" (5 to 6) OR It will create new object "a" with value of 6? Same question for string? var a = "aa"; a = "bb"; Will the compiler override the reference of "a" in the Stack OR Will create new object "a" with the newer reference?
@CodingTutorialsAreGo
@CodingTutorialsAreGo 2 жыл бұрын
In the case of ints, a is an int and therefore a value type, so only one block of stack storage is created with a value 5 which is then overwritten to 6. (Actually, in this simple case the compiler might spot the optimisation and just set a directly to 6, but in general it overwrites.) In the string case, there will be one reference block allocated on the stack for a and two blocks on the heap, for "aa" and "bb". a will initially contain the reference to "aa" but will then be overwritten with the reference to "bb". (Again, assuming no compiler optimisations.)
@hristoiliev7767
@hristoiliev7767 2 жыл бұрын
@@CodingTutorialsAreGo Perfect! Thank you very much! :)
@mert.pinarbasi
@mert.pinarbasi 2 жыл бұрын
In Java we have Wrapper Classes to make primitive types to reference types.However in C# int is inherited from Object class.How can it is possible to both inherited and being stored in stack.What happens if I call toString() method on a int variable is it will be allocated to the heap?.I dont understand how it is possible to inherited from object if it is stored in stack it is the opposite of java.
@CodingTutorialsAreGo
@CodingTutorialsAreGo 2 жыл бұрын
The fundamental thing that is required to allocate an object on the stack is that the compiler knows its size. It doesn't matter if it's inherited as long as the compiler knows the size of the overall object. In C++, for example, you can store anything on the stack, whatever the depth of the inheritance. What you can't do in C# is have a base class reference to a derived class object stored on the stack. C# cheats this using boxing. A C# box is the same concept as the Java wrapper, except that it is automatically created by the compiler, whereas in Java they are predefined (or they were last time I used Java - it may have moved on). See my video on boxing: kzfaq.info/get/bejne/iJdllMiHvr3KeHk.html
@mert.pinarbasi
@mert.pinarbasi 2 жыл бұрын
@@CodingTutorialsAreGo Thanks for high quality videos and explanation.As I understand , when we call toString() method for example in a value type still it is stored in stack which is a great advantage if we compare the design preference with Java.Because there is no need boxing nor wrapper classes to use methods like toString if we want to use them.So, when I call an toString method in int value is c# compiler does autoboxing ?
@CodingTutorialsAreGo
@CodingTutorialsAreGo 2 жыл бұрын
@@mert.pinarbasi When you call ToString on a value type (e.g. int) then the value type remains on the stack. There is no boxing. The int remains on the stack. The returned string, obviously, must be on the heap.
.NET Attributes
24:49
Coding Tutorials
Рет қаралды 10 М.
C# Equality and Hashcodes
27:05
Coding Tutorials
Рет қаралды 8 М.
Неприятная Встреча На Мосту - Полярная звезда #shorts
00:59
Полярная звезда - Kuzey Yıldızı
Рет қаралды 7 МЛН
small vs big hoop #tiktok
00:12
Анастасия Тарасова
Рет қаралды 24 МЛН
WHO DO I LOVE MOST?
00:22
dednahype
Рет қаралды 77 МЛН
He sees meat everywhere 😄🥩
00:11
AngLova
Рет қаралды 9 МЛН
.NET Core Garbage Collection
14:54
Coding Tutorials
Рет қаралды 22 М.
Arrays vs Lists
12:49
Coding Tutorials
Рет қаралды 6 М.
Where are types allocated in .NET and why people get it so wrong
14:35
C# 10 Part 2 - Records, Structs and Record Structs
19:32
Coding Tutorials
Рет қаралды 6 М.
Implementing IEnumerable
17:35
Coding Tutorials
Рет қаралды 10 М.
Value & Reference types in C#. Write Better Code!
15:09
Tarodev
Рет қаралды 32 М.
Stackalloc and Spans
30:17
Coding Tutorials
Рет қаралды 9 М.
Advanced C#: Lesson 3 - Value types vs Reference types
28:11
Jesse Dietrichson
Рет қаралды 83 М.
Zig for Impatient Devs
9:48
Isaac Harris-Holt
Рет қаралды 69 М.
C# async, await and Task
16:26
Coding Tutorials
Рет қаралды 6 М.
Samsung S24 Ultra professional shooting kit #shorts
0:12
Photographer Army
Рет қаралды 35 МЛН
Hisense Official Flagship Store Hisense is the champion What is going on?
0:11
Special Effects Funny 44
Рет қаралды 2,4 МЛН
В России ускорили интернет в 1000 раз
0:18
Короче, новости
Рет қаралды 676 М.