Latest / CyberSecurity Summary / C# 7.0 Pocket Reference: Instant Help for C# 7.0 Programmers
Transcript
- 0:00Speaker 1: Imagine moving into a house where the walls like literally
- 0:04Speaker 1: shift and reorganize themselves depending on what kind of furniture
- 0:07Speaker 1: you decide to bring inside.
- 0:08Speaker 2: Right, just totally dynamic yea yeah, Like.
- 0:10Speaker 1: You bring in a tiny chair in the room shrinks
- 0:12Speaker 1: to hold it perfectly. You bring in a massive sofa,
- 0:15Speaker 1: the floor plan just completely rewires itself to accommodate it.
- 0:18Speaker 2: And a team of invisible workers just stands by to
- 0:21Speaker 2: haul it away the second you stop using it.
- 0:23Speaker 1: Exactly that dynamic, invisible architecture is basically the reality of
- 0:28Speaker 1: modern software. And today we are jumping into a deep
- 0:31Speaker 1: dive on the c sharp seven point zero pocket reference
- 0:34Speaker 1: by Joseph Albahari and Ben Albahari.
- 0:37Speaker 2: It's a fantastic resource, it really is.
- 0:39Speaker 1: We're going to figure out exactly how this language pulls
- 0:42Speaker 1: off this impossible high wire act. I mean, giving developers raw,
- 0:47Speaker 1: bare metal computational power without making them manage the messy,
- 0:52Speaker 1: unforgiving physics of computer memory.
- 0:54Speaker 2: It is a really delicate balance. Sea sharp is well.
- 0:57Speaker 2: It's a language built from the ground up to offer
- 0:59Speaker 2: the performance of older low level languages while simultaneously providing
- 1:04Speaker 2: an incredible safety net for your productivity. It's platform neutral,
- 1:07Speaker 2: so it runs practically anywhere, but it was really engineered
- 1:10Speaker 2: to sing when paired with the dot net framework. And
- 1:13Speaker 2: the text we're looking at today is this dense, insightful
- 1:17Speaker 2: roadmap of how that safety net actually functions under the hood.
- 1:20Speaker 1: And our mission for this deep dive isn't to just
- 1:24Speaker 1: read off syntax or build a glossar. No, that would
- 1:26Speaker 1: be boring totally. We want to understand the underlying DNA
- 1:31Speaker 1: of this system. How does it physically store your data,
- 1:34Speaker 1: what happens when that data.
- 1:35Speaker 2: Misbehaves or when it doesn't even exist right?
- 1:38Speaker 1: And how does it let developers express dizzyingly complex logic
- 1:43Speaker 1: without losing their minds. And I want to emphasize right
- 1:46Speaker 1: up front, even if you listening right now have never
- 1:48Speaker 1: written a single line of code in your life. Understanding
- 1:51Speaker 1: how a system like this organizes information, it can fundamentally
- 1:55Speaker 1: change how you approach problem solving. It just trains you
- 1:58Speaker 1: to think about structure.
- 2:00Speaker 2: And structure is exactly where we have to start, because
- 2:03Speaker 2: before you can manipulate data or build an application, you
- 2:06Speaker 2: have to understand how the system physically stores its foundation.
- 2:09Speaker 1: Okay, let's unpack this. We're talking about types.
- 2:11Speaker 2: Right, Yes, types. You can think of a type as
- 2:14Speaker 2: a blueprint for a value. The language comes with pre
- 2:17Speaker 2: defined types like an integer, which is just a whole number,
- 2:20Speaker 2: or a booleon which is a simple true or false switch.
- 2:24Speaker 1: But you can build your own too, Exactly, you.
- 2:26Speaker 2: Can build custom types like a unit converter or a
- 2:28Speaker 2: panda class, which basically bundle related data and behaviors together
- 2:33Speaker 2: into one neat package.
- 2:35Speaker 1: So we have our blueprints. But the authors highlight this
- 2:38Speaker 1: massive architectural divide and how c sharp actually builds those
- 2:42Speaker 1: blueprints in memory.
- 2:43Speaker 2: The value types versus reference types divide.
- 2:46Speaker 1: Yeah, and this basically dictates the entire physical reality of
- 2:49Speaker 1: the program.
- 2:50Speaker 2: It is arguably the single most critical distinction in the language,
- 2:54Speaker 2: and it comes down to two very different physical locations
- 2:56Speaker 2: in your computer's memory, the stack and the heap.
- 2:59Speaker 1: The stack in the heap.
- 3:00Speaker 2: Right, value types live on the stack. Reference types live
- 3:03Speaker 2: on the heap. The text uses a custom type called point,
- 3:06Speaker 2: representing an X and y coordinate on a graph to
- 3:09Speaker 2: demonstrate this.
- 3:10Speaker 1: Okay, so how does that work.
- 3:11Speaker 2: Well, if you define your point blueprint as a struct
- 3:14Speaker 2: the language treats it as a value type. When you
- 3:17Speaker 2: create a variable called P one. The actual X and
- 3:21Speaker 2: Y numbers are physically stuffed right inside P one on
- 3:24Speaker 2: the stack.
- 3:25Speaker 1: Oh wow.
- 3:26Speaker 2: And if you then write a line of code that
- 3:27Speaker 2: says P two equals P one, the system physically duplicates
- 3:31Speaker 2: those numbers. It carves out a brand new independent chunk
- 3:35Speaker 2: of memory for P two.
- 3:36Speaker 1: Right, So I've then changed P one's ex coordinate to
- 3:39Speaker 1: be the number nine. P two's ex coordinate stays exactly.
- 3:42Speaker 2: What it was precisely.
- 3:43Speaker 1: They're completely isolated. It's like, okay, It's like having a
- 3:47Speaker 1: super secret chocolate chip cookie recipe written on a piece
- 3:50Speaker 1: of paper and I hand you a photocopy of it.
- 3:52Speaker 2: I like that analogy.
- 3:53Speaker 1: You can take a sharpie, cross out the chocolate chips
- 3:55Speaker 1: and write in raisins, which, by the way, would be
- 3:57Speaker 1: a total crime against baking. But my original recipe is
- 4:00Speaker 1: totally safe. You're Edits don't affect my paper.
- 4:02Speaker 2: What's fascinating here is that the beauty of that photocopy
- 4:05Speaker 2: approach is the system doesn't have to manage it long term.
- 4:08Speaker 2: Once the function you're running finishes, your photocopy is just
- 4:12Speaker 2: thrown in.
- 4:12Speaker 1: The trash, just tossed out.
- 4:14Speaker 2: Yeah, that is the nature of the stack. It operates
- 4:17Speaker 2: like a literal stack of cafeteria trays. Variables are piled
- 4:21Speaker 2: on top of each other as they are needed, and
- 4:23Speaker 2: as soon as a task is done, the top crazer
- 4:26Speaker 2: just popped off and instantly destroyed. So fast, lightning fast.
- 4:29Speaker 2: But it is strictly scoped and small. You can't put
- 4:32Speaker 2: everything on the stack, which.
- 4:34Speaker 1: Brings us to the other side of the divide, the heap.
- 4:37Speaker 1: What if we take that exact same point code, but
- 4:39Speaker 1: we change the blueprint from a struct to a class.
- 4:42Speaker 2: A class creates a reference type. Now when you create
- 4:45Speaker 2: P one, the actual heavy data that X and y
- 4:48Speaker 2: coordinates is created over in the heat, and the heap
- 4:51Speaker 2: is like it's like a sprawling, unstructured warehouse for complex
- 4:56Speaker 2: objects that need to live for a long time. The
- 4:58Speaker 2: variable P one sitting on the doesn't hold the numbers anymore.
- 5:02Speaker 2: It just holds a pointer, like a reference, telling the
- 5:05Speaker 2: computer where to find the data in the warehouse.
- 5:07Speaker 1: So, following my analogy, a reference type isn't a photocopy.
- 5:11Speaker 1: It's like I shared the link to a live Google
- 5:13Speaker 1: doc with you. We are both looking at the exact
- 5:15Speaker 1: same document.
- 5:16Speaker 2: Yes, and if you say P two equals P one.
- 5:18Speaker 2: Now the system doesn't copy the data, it just copies
- 5:21Speaker 2: the link. Oh wow, both variables point to the exact
- 5:24Speaker 2: same object in the warehouse. If you change P one's
- 5:28Speaker 2: ex coordinate to nine and then ask the computer what
- 5:31Speaker 2: P two's ex coordinate is, it will also be nine.
- 5:34Speaker 2: They are intrinsically linked.
- 5:36Speaker 1: But wait, if the stack just throws away its trays
- 5:38Speaker 1: when it's done, what happens to all the heavy data
- 5:41Speaker 1: sitting out in the heap warehouse? Doesn't it just pile up?
- 5:44Speaker 2: That is where c Sharp's invisible workers come in. The
- 5:47Speaker 2: heap requires a cleanup crew known as the garbage collector.
- 5:50Speaker 1: The famous garbage collector.
- 5:51Speaker 2: Exactly, It constantly runs in the background tracing all the
- 5:55Speaker 2: active links from the stack into the keep. The moment
- 5:57Speaker 2: it realizes that a piece of data in the warehouse
- 6:00Speaker 2: has absolutely no links pointing to it anymore, meaning no
- 6:03Speaker 2: part of your program can even reach it, the garbage
- 6:06Speaker 2: collector sweeps it up and frees the memory. That's incredibly convenient,
- 6:10Speaker 2: it is you get the raw power of complex, long
- 6:14Speaker 2: lived objects without having to manually command the computer to
- 6:17Speaker 2: delete them, which honestly was a massive source of bugs
- 6:20Speaker 2: in older languages.
- 6:21Speaker 1: So we've mapped out how C sharp physically stores its foundation,
- 6:26Speaker 1: balancing the fast stack with the garbage collected heap. But
- 6:29Speaker 1: a pile of data isn't always perfectly well behaved.
- 6:32Speaker 2: It's not at all.
- 6:33Speaker 1: Let's look at what happens when the actual information inside
- 6:36Speaker 1: these structures gets weird. I was reading the section on
- 6:39Speaker 1: numeric types, and the text gives this mathematical example that
- 6:42Speaker 1: completely broke my brain for a second. Floating point math.
- 6:46Speaker 2: Ah, Yes, the infamous divide between the double type and
- 6:50Speaker 2: the decimal type. It traps developers constantly.
- 6:53Speaker 1: It's wild. The authors show this specific equation. You take
- 6:56Speaker 1: zero point one multiplied by ten, which is exactly one
- 7:00Speaker 1: track that result from one one mius one. The answer
- 7:04Speaker 1: universally should.
- 7:05Speaker 2: Be zero, right basic math.
- 7:06Speaker 1: But in C sharp, if you use the standard double
- 7:09Speaker 1: type for this, the answer it spits out is negative
- 7:11Speaker 1: one point four to nine, followed by an e in
- 7:13Speaker 1: andh eight. It's this microscopic, weird negative fraction. Why does
- 7:17Speaker 1: a basic subtraction equation completely fail to equal zero?
- 7:20Speaker 2: It's entirely about how the physical processor of your computer
- 7:24Speaker 2: understands fractions. The double type represents numbers in base two,
- 7:28Speaker 2: it uses binary. Now think about our human base ten system.
- 7:32Speaker 2: You cannot perfectly represent the fraction one third in base ten.
- 7:35Speaker 2: It becomes zero point three to three to three, repeating
- 7:37Speaker 2: into infinity. You have to chop it off eventually. Right,
- 7:40Speaker 2: The exact same thing happens to the computer in base
- 7:43Speaker 2: two binary when it tries to represent the fraction zero
- 7:45Speaker 2: point one. It can't do it cleanly. It becomes a repeating,
- 7:49Speaker 2: imprecise binary fraction.
- 7:51Speaker 1: Oh so it's chopping off the end of the binary decimal,
- 7:54Speaker 1: and when you do math with it, those microscopic chopped
- 7:57Speaker 1: off in precisions compound, leaving you with that weird negative
- 8:01Speaker 1: exponential instead of a clean zero.
- 8:02Speaker 2: Precisely, the double type sacrifices absolute precision to fit into
- 8:07Speaker 2: a base two.
- 8:07Speaker 1: Format, which means if I am writing, say, banking software,
- 8:11Speaker 1: and I use double to calculate dally interest rates, over
- 8:14Speaker 1: a few years, I'm going to be losing or inventing
- 8:17Speaker 1: fractions of panties everywhere.
- 8:18Speaker 2: The accounting department would audit you into oblivion exactly. So
- 8:21Speaker 2: how do we fix it for financial calculations? The text
- 8:25Speaker 2: emphasizes you must use the decimal type. The decimal type
- 8:28Speaker 2: operates in base ten. It stores the number as a
- 8:30Speaker 2: massive integer and just remembers exactly where the decimal point goes.
- 8:34Speaker 2: So it's super precise.
- 8:35Speaker 1: Very it has twenty eight to twenty nine significant figures
- 8:38Speaker 1: of absolute precision. Zero point one is stored perfectly a
- 8:42Speaker 1: zero point one.
- 8:43Speaker 2: Wait a second. If decimal gives us perfect human math
- 8:46Speaker 2: without the compounding rounding errors, why do we even keep
- 8:49Speaker 2: double around. Aren't we just asking for accounting bugs by
- 8:52Speaker 2: leaving an imprecise math type in the language. Why not
- 8:55Speaker 2: just use decimal for absolutely everything?
- 8:57Speaker 1: If we connect this to the bigger picture, it's because
- 9:00Speaker 1: of the raw hardware physics. Your computer's processor has a
- 9:03Speaker 1: dedicated floating point unit physically wired to crunch base two
- 9:07Speaker 1: numbers at blinding speed.
- 9:09Speaker 2: Oh so with the speed thing exactly.
- 9:11Speaker 1: The double type runs natively on that hardware. That decimal
- 9:14Speaker 1: type is non native. It's a software construct. The processor
- 9:17Speaker 1: has to do a massive amount of extra simulated work
- 9:19Speaker 1: to process base ten math. Because of that physical reality,
- 9:23Speaker 1: double is roughly ten times faster to compute than decimal. Okay,
- 9:26Speaker 1: that makes perfect sense. If I'm programming a three D
- 9:29Speaker 1: physics engine and calculating the spatial coordinates of fifty thousand
- 9:33Speaker 1: colliding particles sixty times a second. I really don't care
- 9:37Speaker 1: if a particle's position is off by a billionth of
- 9:39Speaker 1: a millimeter. I just needed to render instantly exactly.
- 9:43Speaker 2: Double is for science graphics, and raw speed decimal is
- 9:46Speaker 2: for human made values like currency, where precision is non negotiable.
- 9:51Speaker 1: So that's the quirk of imprecise data. But what about
- 9:54Speaker 1: when data just doesn't exist at all? The concept of.
- 9:58Speaker 2: Null, that's a huge one.
- 10:00Speaker 1: The book explains that value types the photocopies on the
- 10:02Speaker 1: sack can't ordinarily be null. They have to hold a value,
- 10:06Speaker 1: even if it's just a zero. But reference types, those
- 10:09Speaker 1: links to the Google doc can absolutely be null, meaning
- 10:12Speaker 1: the pointer is pointing at empty.
- 10:14Speaker 2: Space, and this leads to the infamous null reference exception.
- 10:17Speaker 2: To a developer, those three words are terrifying. It is
- 10:20Speaker 2: arguably the most common way software crashes.
- 10:23Speaker 1: Let's visualize the mechanism of that crash for a second.
- 10:26Speaker 1: To a non programmer, why does a pointer pointing to
- 10:29Speaker 1: nothing cause the whole system to explode? It's like if
- 10:33Speaker 1: you tell the computer to go read the Google doc
- 10:36Speaker 1: and it follows the link only to find an empty void.
- 10:39Speaker 1: It just panics, right.
- 10:41Speaker 2: It doesn't know what to do next, so.
- 10:42Speaker 1: The program simply drops dead.
- 10:44Speaker 2: That panic is exactly what happens. This system attempts an
- 10:47Speaker 2: operation on memory that doesn't exist, and the only safe
- 10:50Speaker 2: response is to halt execution entirely. But to combat this,
- 10:54Speaker 2: c Sharp introduced incredibly elegant syntax to safely check for
- 10:59Speaker 2: that void with without crashing.
- 11:00Speaker 1: The null conditional operator, or, as it's affectionately known, the
- 11:04Speaker 1: Elvis operator.
- 11:05Speaker 2: Yes because the question mark and the dot next to
- 11:07Speaker 2: each other looks slightly like Elvis Presley's swooping hair in
- 11:10Speaker 2: an eye.
- 11:11Speaker 1: I know, you literally can't unsee it once you notice it,
- 11:14Speaker 1: But functionally, how does elvis save the program from panicking.
- 11:19Speaker 2: Let's say you want to extract a text string from
- 11:20Speaker 2: a variable, so you write the command to get that string.
- 11:23Speaker 2: If the variable is null, boom, the program crashes. But
- 11:26Speaker 2: if you insert the Elvis operator the question mark and
- 11:29Speaker 2: the dot right before the command, it short circuits the panic.
- 11:33Speaker 2: It essentially whispers to the compiler, hey, if this variable
- 11:36Speaker 2: happens to be a void, just stop right here. Don't
- 11:39Speaker 2: even try to read the text, just hand me back
- 11:41Speaker 2: a null and let's quietly move.
- 11:43Speaker 1: On, no crash, and you can seamlessly pair it with
- 11:46Speaker 1: another tool. Right. Yeah, the null coalescing operator, which is
- 11:50Speaker 1: just two question marks in a row exactly.
- 11:52Speaker 2: It acts like a backup generator.
- 11:53Speaker 1: So you try to read the text with Elvis and
- 11:55Speaker 1: then use the two question marks to say if you
- 11:58Speaker 1: hit a void, just substance too the word unknown instead.
- 12:02Speaker 1: It condenses what used to be a clunky multiline error
- 12:05Speaker 1: checking routine into one sleek, panic free line of code.
- 12:09Speaker 2: It removes the friction of defensive programming, and C sharp
- 12:12Speaker 2: seven took that friction removal further with tuples, which solved
- 12:15Speaker 2: the opposite problem returning too much data.
- 12:17Speaker 1: Oh tuples right. Historically, if a single function needed to
- 12:21Speaker 1: calculate and hand back two distinct things, say a person's
- 12:24Speaker 1: name and their age, you had to jump through ridiculous hoops.
- 12:27Speaker 2: It was a meth You either had to.
- 12:28Speaker 1: Build a completely separate, dummy blueprint class just to hold
- 12:32Speaker 1: those two variables together, or you had to use these
- 12:35Speaker 1: clunky out parameters that made the code super difficult to read.
- 12:39Speaker 2: Tuples bypass all of that. Backed by a lightweight value
- 12:42Speaker 2: type called value tuple. They let you elegantly bundle values
- 12:45Speaker 2: together on the fly. You literally just put the name
- 12:48Speaker 2: Bob and his age twenty three in parentheses and assign
- 12:52Speaker 2: it to a variable. The compiler handles the underlying structure automatically, and.
- 12:56Speaker 1: The deconstruction syntax is even better. You can call that
- 12:59Speaker 1: function instantly split the tupple back into individual usable variable. Yes,
- 13:03Speaker 1: the deconstructor you just write parentheses the word string and
- 13:06Speaker 1: the variable name, followed by the word end in the
- 13:08Speaker 1: variable age. Boom, you have two local variables ready to
- 13:12Speaker 1: go without ever defining a custom class. It keeps the
- 13:15Speaker 1: works based incredibly clean.
- 13:16Speaker 2: So we have our structural foundation. We've navigated the mathematical quirks,
- 13:20Speaker 2: and we've tamed the voids with graceful operators. But a
- 13:24Speaker 2: program isn't just a static warehouse of data. We need action.
- 13:28Speaker 1: We need logic which moves us right into how c
- 13:31Speaker 1: sharp passes behaviors around. We know how to put the
- 13:34Speaker 1: number five into a variable, but how do we put
- 13:36Speaker 1: an actual action like a math calculation into a variable.
- 13:40Speaker 2: That is the domain of delegates. A delegate is fundamentally
- 13:44Speaker 2: a type that represents a reference to a method. Think
- 13:46Speaker 2: of it as a pluggable protocol.
- 13:48Speaker 1: Yeah, a protocol, right.
- 13:50Speaker 2: It defines exactly what a method's inputs and outputs should
- 13:53Speaker 2: look like. The authors use a transformer delegate as an example.
- 13:57Speaker 2: If you define a protocol that takes an integer, all
- 14:00Speaker 2: alters it, and returns an integer, you can assign any
- 14:03Speaker 2: actual method that matches that fingerprint to a delegate variable.
- 14:06Speaker 1: So what does this all mean? The immediate question for
- 14:09Speaker 1: anyone looking at this is why not just call the
- 14:11Speaker 1: method directly? Why bother creating a middleman?
- 14:15Speaker 2: Because the middleman allows for brilliant decoupling. If you write
- 14:19Speaker 2: a generic engine that loops through a massive list of
- 14:21Speaker 2: numbers and applies some transformation to each one, that engine
- 14:25Speaker 2: shouldn't need to know what the transformation actually is. Oh,
- 14:28Speaker 2: it just does the work, right, It just needs a
- 14:30Speaker 2: delegate at runtime. You can plug in a method that
- 14:33Speaker 2: squares the numbers, or a completely different method that has them.
- 14:37Speaker 2: The delegate makes your architecture modular.
- 14:41Speaker 1: And the language evolved a wonderfully concise way to write
- 14:43Speaker 1: those plug in methods lambda expressions.
- 14:46Speaker 2: They are everywhere now.
- 14:47Speaker 1: Instead of writing out a whole separate named method just
- 14:50Speaker 1: to square number, you write it in line. You literally
- 14:53Speaker 1: just write the variable say x an arrow and then
- 14:56Speaker 1: x times x.
- 14:57Speaker 2: The compiler reads the arrow as goes.
- 14:59Speaker 1: To baraily minimalist. But this minimalism hides what I found
- 15:04Speaker 1: to be the most fascinating trap in the entire book,
- 15:07Speaker 1: the closure trap.
- 15:08Speaker 2: Oh yes, this is a spectacular aha moment for understanding
- 15:12Speaker 2: compiler architecture.
- 15:13Speaker 1: Let's lay out the scenario from the text. We build
- 15:16Speaker 1: a simple four loop. The loop uses an integer variable
- 15:18Speaker 1: eye that starts at zero, goes to one, then goes
- 15:21Speaker 1: to two, and finishes a standard loop right and inside
- 15:25Speaker 1: that loop, we create a tiny lamba expression that just
- 15:28Speaker 1: prints the value of ee. We stash those three lambdas
- 15:31Speaker 1: in a array to be executed later on. Common sense
- 15:34Speaker 1: dictates that the first lamb to capture the zero, the
- 15:37Speaker 1: second capture of the one, and the third capture of
- 15:39Speaker 1: the two. That's what you would expect, So when we
- 15:41Speaker 1: run them later, they should print zero, one, two, But
- 15:45Speaker 1: they don't. When you run them, they print three, three three.
- 15:48Speaker 1: Why is the computer ignoring the past?
- 15:51Speaker 2: Because of the physical reality of what a lamba actually captures.
- 15:54Speaker 2: It doesn't take a snapshot of the value of the
- 15:56Speaker 2: variable at the exact millisecond. The lambda is created.
- 15:59Speaker 1: Wait it doesn't.
- 16:00Speaker 2: No, it captures a reference to the variable itself. It
- 16:03Speaker 2: locks onto the memory location. By the time the four
- 16:06Speaker 2: loop finishes spinning, that single shared variable Eye has been
- 16:10Speaker 2: incremented up to three.
- 16:11Speaker 1: Oh wow.
- 16:12Speaker 2: So later, when those three lamb does finally wake up
- 16:15Speaker 2: and execute, they all look at that single shared memory
- 16:18Speaker 2: location and they all see the number three.
- 16:21Speaker 1: The book points out, the fix is to create a
- 16:23Speaker 1: brand new local variable inside the loops brackets, copy the
- 16:26Speaker 1: value of eye into it, and have the lamb to
- 16:28Speaker 1: capture that new inner variable exactly.
- 16:31Speaker 2: Since a brand new physical variable is created on the stack,
- 16:34Speaker 2: for every single spin of the loop, each lambda gets
- 16:37Speaker 2: its own isolated box to look at. That establishes true
- 16:41Speaker 2: isolation for each closure.
- 16:42Speaker 1: Well, wait, I have to puzzle through the physics of this.
- 16:45Speaker 1: If a lambda captures an outer variable and we stash
- 16:48Speaker 1: that lambda away to be run ten minutes later, doesn't
- 16:51Speaker 1: that artificially extend the life of that variable.
- 16:54Speaker 2: Yes, it does.
- 16:55Speaker 1: Like our loop variable was a value type on the stack,
- 16:58Speaker 1: it was supposed to be thrown in the trash the
- 17:01Speaker 1: millisecond the loop finished. If the lamb is still referencing
- 17:04Speaker 1: it ten minutes later, how does it survive? Isn't this
- 17:07Speaker 1: just a massive memory leak waiting to happen?
- 17:10Speaker 2: This raises an important question, and your intuition is pointing
- 17:12Speaker 2: directly at the compiler's most complex magic trick. How does
- 17:16Speaker 2: a short lived stack variable survive? It survives because the
- 17:19Speaker 2: compiler secretly rewrites your code behind your back. Wait, really, yes,
- 17:24Speaker 2: When it detects that a lambda is capturing a local variable,
- 17:26Speaker 2: it intercepts that variable. It takes it off the fast
- 17:29Speaker 2: temporary stack and hoists it into a hidden auto generated
- 17:33Speaker 2: class over in the heap warehouse.
- 17:35Speaker 1: That is wild. It quietly transforms a temporary value type
- 17:40Speaker 1: into a long lived reference type just to keep it
- 17:42Speaker 1: alive for the lambda.
- 17:43Speaker 2: It forcibly extends the variable's lifetime to match the life
- 17:47Speaker 2: span of the delegate. It will eventually be cleaned up
- 17:50Speaker 2: by the garbage collector. But it perfectly illustrates the hitting
- 17:53Speaker 2: cost of syntactic.
- 17:54Speaker 1: Sugar because it looks so simple.
- 17:56Speaker 2: Lambdas look wonderfully simple, just an arrow and some math,
- 17:59Speaker 2: but they can trigger complex heap allocations and garbage collection pressure.
- 18:03Speaker 2: This is precisely why c sharp seven introduce local methods.
- 18:07Speaker 2: The ability to define a fully named method and nested
- 18:10Speaker 2: inside another method.
- 18:11Speaker 1: So that's the alternative.
- 18:13Speaker 2: Right. Local methods can access those outer variables without forcing
- 18:16Speaker 2: the compiler to generate hidden heap classes, giving you a
- 18:19Speaker 2: highly efficient alternative when you don't actually need the pluggable
- 18:22Speaker 2: nature of a delegate.
- 18:23Speaker 1: The languid is literally reorganizing your memory management to accommodate
- 18:27Speaker 1: your logic, but you have to understand the trick so
- 18:29Speaker 1: you don't abuse it, which brings us to the ultimate
- 18:34Speaker 1: synthesis of everything we've discussed today, taking these floating behaviors
- 18:38Speaker 1: and applying them to massive data sets. Section four iterators
- 18:42Speaker 1: and LANQ.
- 18:43Speaker 2: This is where c Sharp's design philosophy achieves its greatest leverage.
- 18:48Speaker 2: It begins with iterators. If you need to generate a
- 18:51Speaker 2: massive sequence of numbers, say the Fibonacci sequence, you don't
- 18:55Speaker 2: have to calculate a million numbers, load them all into
- 18:58Speaker 2: a giant array in memory, and hand the heavier ray back.
- 19:02Speaker 2: You can write an iterator method using the yield return statement.
- 19:06Speaker 1: And the yield return statement is like another compiler magic trick.
- 19:10Speaker 1: The book explains that it transforms your method into a
- 19:12Speaker 1: state machine.
- 19:13Speaker 2: Exactly a state machine.
- 19:14Speaker 1: The code doesn't just run top to bottom and finish.
- 19:17Speaker 1: It runs until it hits yield, return hands the calculated
- 19:20Speaker 1: value back to the caller, and then it just freezes.
- 19:22Speaker 1: It pauses its entire state in memory. It doesn't wake
- 19:26Speaker 1: up or calculate the next number until the caller specifically
- 19:28Speaker 1: sends a command.
- 19:29Speaker 2: Called move next, and that precise, paused state machine is
- 19:33Speaker 2: the foundation of LANQ language Integrated Query. L and Q
- 19:37Speaker 2: allows you to query collections of data in memory using
- 19:40Speaker 2: a syntax that reads very much like database SQL.
- 19:43Speaker 1: Use standard operators, right yeah.
- 19:45Speaker 2: Like where to filter out bad data, select to transform
- 19:48Speaker 2: what's left, and order by to sort the final results.
- 19:51Speaker 1: Here's where it gets really interesting, though. The absolute superpower
- 19:54Speaker 1: of l and Q is deferred execution or lazy evaluations.
- 19:59Speaker 2: It's brilliant.
- 20:00Speaker 1: The authors provide this amazing example. You have a list
- 20:02Speaker 1: of numbers. You write a complex LNQ query to filter
- 20:06Speaker 1: and sort that list, but you haven't actually asked to
- 20:09Speaker 1: see the results yet. You just define the instructions. Then
- 20:12Speaker 1: you sneak an extra element into the original list. You
- 20:14Speaker 1: add the number two. After that you run a forged
- 20:17Speaker 1: loop to actually look at the results of your query,
- 20:20Speaker 1: and that brand new number two is fully processed and
- 20:24Speaker 1: included in the output.
- 20:25Speaker 2: Because writing the query and executing the query are two
- 20:28Speaker 2: fundamentally disconnected events, the query is just a blueprint of intent.
- 20:32Speaker 2: It refuses to actually touch the data until the absolute
- 20:36Speaker 2: last possible millisecond when you force it to enumerate.
- 20:39Speaker 1: I love visualizing deferred execution like a conveyor belt at
- 20:42Speaker 1: a highly efficient custom sandwich shop.
- 20:44Speaker 2: Oh I like this.
- 20:45Speaker 1: Yeah. You walk in and you tell the first worker,
- 20:48Speaker 1: I only want the wheat bread. That's your wear filter.
- 20:51Speaker 1: You tell the next worker toast it. That's your select transformation.
- 20:55Speaker 1: You tell the last worker put the biggest sandwiches first.
- 20:58Speaker 1: That's your order buy. You have built this intricate logic pipeline,
- 21:03Speaker 1: but the workers haven't sliced a single piece of bread yet.
- 21:07Speaker 1: They are just standing there frozen. They don't consume any ingredients,
- 21:10Speaker 1: They don't expand any energy until the customer at the
- 21:13Speaker 1: very end of the line finally opens their mouth and says,
- 21:16Speaker 1: I'll take the next sandwich. That is the forge loop,
- 21:19Speaker 1: and then it starts exactly Only then does the conveyor
- 21:22Speaker 1: belt turn on and it processes exactly one sandwich through
- 21:25Speaker 1: the entire pipeline.
- 21:26Speaker 2: The efficiency of that decoupling is staggering. If you construct
- 21:30Speaker 2: an l in Q query to process a database of
- 21:33Speaker 2: ten million records, but your user interface only ever asks
- 21:36Speaker 2: to display the first three results. The system only processes
- 21:40Speaker 2: three records.
- 21:40Speaker 1: It doesn't do the rest.
- 21:41Speaker 2: No, it doesn't waste precious CPU cycles and heap memory,
- 21:45Speaker 2: rendering a massive ten million item list that you will
- 21:47Speaker 2: never look at. It pulls data through the pipeline purely
- 21:51Speaker 2: on demand.
- 21:51Speaker 1: It is incredibly elegant, and it really brings this entire
- 21:55Speaker 1: exploration full circle. I mean, we started at the bedrock
- 21:59Speaker 1: physical reality of memory, the strict cafeteria trays of the stack,
- 22:03Speaker 1: and the sprawling warehouse of the heat.
- 22:05Speaker 2: We covered a lot of ground.
- 22:06Speaker 1: We really did. We saw how the physics of base
- 22:08Speaker 1: two processors create math quirks that we have to actively
- 22:11Speaker 1: manage with types like decimal. We untangled the logic traps
- 22:15Speaker 1: of closures, revealing how the compiler secretly hoists variables to
- 22:19Speaker 1: keep our lambdas alive. And finally we arrived at the lazy,
- 22:23Speaker 1: highly efficient conveyor belts of LNQ, which let us express
- 22:27Speaker 1: massive data operations with just a few intuitive lines of code,
- 22:31Speaker 1: completely abstracted away from the raw memory underneath.
- 22:34Speaker 2: It is a profound journey from the bare metal to
- 22:36Speaker 2: the high level abstract and to.
- 22:38Speaker 1: You listening right now, Whether you spend your day's writing
- 22:41Speaker 1: code or you're just deeply curious about the digital infrastructure
- 22:44Speaker 1: of our world. Mastering these concepts is powerful. Knowing exactly
- 22:49Speaker 1: why a simple subtraction equation fails to equal zero, or
- 22:52Speaker 1: knowing the mechanism of how a system can freeze its
- 22:55Speaker 1: state and delay doing work until the exact second it's needed.
- 22:59Speaker 1: It sharpens your critical thinking. It reminds you that underneath
- 23:02Speaker 1: every sleek app, every seamless interface, there is a shifting
- 23:05Speaker 1: architecture making microscopic, highly consequential decisions every single nanosecond.
- 23:11Speaker 2: I want to leave you with one final, slightly provocative
- 23:14Speaker 2: thought to chew on. We have spent this entire deep
- 23:18Speaker 2: dive examining how s sharp goes to extraordinary lengths to
- 23:21Speaker 2: protect you right. It offers strict type safety, It manages
- 23:25Speaker 2: your memory with the garbage collector, It diffuses null reference
- 23:28Speaker 2: panics with elvis operators, and it saves resources with lazy execution.
- 23:32Speaker 2: But tucked away in the text is a brief mention
- 23:34Speaker 2: of an advanced topic unsafe code and pointer.
- 23:37Speaker 1: Oh yeah, the unsafe zone, yes.
- 23:40Speaker 2: Se sharp actually allows you, if you explicitly choose to
- 23:42Speaker 2: do so, to write a block of code marked is unsafe.
- 23:46Speaker 2: Inside that block, the safety nets are ripped away, the
- 23:49Speaker 2: garbage collector steps back. You are permitted to use raw
- 23:53Speaker 2: direct memory pointers exactly as you would in an unforgiving
- 23:57Speaker 2: lower level language like C plus plus. So the question
- 24:00Speaker 2: is if language architects spend decades designing these brilliant safeguards
- 24:04Speaker 2: to maximize our productivity, what happens when you actively choose
- 24:08Speaker 2: to step outside those boundaries. When does the relentless quest
- 24:12Speaker 2: for absolute raw performance force us to abandon the very
- 24:15Speaker 2: tools that keep our system stable? And are you prepared
- 24:18Speaker 2: to build your own walls when the invisible workers leave?
- 24:21Speaker 1: That is a phenomenal question. The shifting walls of the
- 24:24Speaker 1: house we talked about at the beginning, they only protect
- 24:26Speaker 1: you as long as you stay inside them. The second
- 24:28Speaker 1: you step out into the unsafe zone, the blueprints are
- 24:30Speaker 1: entirely yours, and you better hope your math is perfect.
- 24:33Speaker 1: Thank you all for taking this GEEP dive with us today.
- 24:35Speaker 1: Keep asking questions, keep looking under the hood, and we
- 24:38Speaker 1: will see you next time,