C# Value Types and Reference Types

  Рет қаралды 5,372

Coding Tutorials

Coding Tutorials

Күн бұрын

Пікірлер: 38
@CodingTutorialsAreGo
@CodingTutorialsAreGo 4 жыл бұрын
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 kzbin.info/door/qWQzlUDdllnLmtgfSgYTCA And if you liked it, click the 👍.
@finwwwfinwww4669
@finwwwfinwww4669 11 ай бұрын
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.
@ЕдвардГригорян-н6к
@ЕдвардГригорян-н6к 3 жыл бұрын
Great video. The best explanation i have ever seen on KZbin
@CodingTutorialsAreGo
@CodingTutorialsAreGo 3 жыл бұрын
Glad it was helpful!
@dolusdirectu
@dolusdirectu 3 жыл бұрын
Wow, the best explanation I have seen so far. Thx for this!
@yegorkatrechko1736
@yegorkatrechko1736 Жыл бұрын
Thanks a lot! That is really helpful, your explanation is clear and easy to understand!
@John_Macaroni
@John_Macaroni 2 жыл бұрын
awesome video, thanks!
@Fernando-mh7st
@Fernando-mh7st 2 жыл бұрын
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!
@jonlicht4514
@jonlicht4514 2 жыл бұрын
Well explained, thank you.
@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.
@davidwhite2011
@davidwhite2011 4 жыл бұрын
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.
@CuriousCyclist
@CuriousCyclist 3 жыл бұрын
Love your videos. You should do a video about correctly implementing the Clone method/interface.
@CodingTutorialsAreGo
@CodingTutorialsAreGo 3 жыл бұрын
I'll put it on the list.
@CuriousCyclist
@CuriousCyclist 3 жыл бұрын
@@CodingTutorialsAreGo Thanks. I look forward to it.
@DedicatedManagers
@DedicatedManagers 4 жыл бұрын
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 4 жыл бұрын
All done in Adobe Premiere Pro.
@DedicatedManagers
@DedicatedManagers 4 жыл бұрын
@@CodingTutorialsAreGo I bet That took a fair amount of extra time. Well worth it though! It really made things clear. Thank you!
@CodingTutorialsAreGo
@CodingTutorialsAreGo 4 жыл бұрын
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.
@kiPROBRos
@kiPROBRos 3 жыл бұрын
Excelent tutorial, thanks.
@davidmoshkowitz4043
@davidmoshkowitz4043 2 жыл бұрын
Amazing!!
@hristoiliev7767
@hristoiliev7767 3 жыл бұрын
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 3 жыл бұрын
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 3 жыл бұрын
@@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: kzbin.info/www/bejne/iGKYkpiMjryZfqc
@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.
@Pluvo2for1
@Pluvo2for1 9 ай бұрын
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.
@kiPROBRos
@kiPROBRos 3 жыл бұрын
12:56
@jonjondi3845
@jonjondi3845 5 ай бұрын
thank you sir
@zanagi
@zanagi 9 ай бұрын
lul c# is really funny when one comes from c++ background... I was so lost.
C# Equality and Hashcodes
27:05
Coding Tutorials
Рет қаралды 9 М.
Stackalloc and Spans
30:17
Coding Tutorials
Рет қаралды 12 М.
Who is More Stupid? #tiktok #sigmagirl #funny
0:27
CRAZY GREAPA
Рет қаралды 10 МЛН
Война Семей - ВСЕ СЕРИИ, 1 сезон (серии 1-20)
7:40:31
Семейные Сериалы
Рет қаралды 1,6 МЛН
Value & Reference types in C#. Write Better Code!
15:09
Tarodev
Рет қаралды 33 М.
.NET Core Garbage Collection
14:54
Coding Tutorials
Рет қаралды 26 М.
C# Delegates and Lambdas
19:07
Coding Tutorials
Рет қаралды 4,1 М.
Where are types allocated in .NET and why people get it so wrong
14:35
Dependency injection fundamentals in C# - DI vs IoC vs DIP
13:30
Amichai Mantinband
Рет қаралды 45 М.
Reference Vs Value In JavaScript
15:12
Web Dev Simplified
Рет қаралды 187 М.
C# 12 Collection Expressions and the Spread Operator
25:27
Coding Tutorials
Рет қаралды 2,2 М.
The Dispose Pattern
16:28
Coding Tutorials
Рет қаралды 11 М.
C# Value vs Reference Types Explained
6:40
Mark Inman
Рет қаралды 6 М.
Who is More Stupid? #tiktok #sigmagirl #funny
0:27
CRAZY GREAPA
Рет қаралды 10 МЛН