What are the basics?
Theoretically everything can be implemented with for
loops, and it was many years ago. You need years to master about 10 "somewhat" most crucial and subtle aspects and their combinations in programming... to just understand the Tech world is the bottomless pit (or a borderless universe).
INFO
async, await
and generallyTasks
for asynchronous computation;- resource management like
disposable
, lifecycle, memory management; class
,struct
,Span<T>, Memory<T>, ReadOnlySpan<T>, ReadOnlyMemory<T>
,stack
,heap
;- "endless"
Algorithms
anddesign patterns
; abstract
,interfaces
;generic types
and otherOOP
principles;Exceptions
and handling of errors;functional (lambda) programming
, and javascript closures greet you too.- for design
OpenSCAD
and 100 Computer Aided Design apps, Video Editing,CSS
,HTML
,ML/AI
,Python
,Rust
,Go
. Rabit hole 2.0
(c)
Deeper understanding of programming, memory, OS, and CPU internals opens the door to searching and implementing "elegant" and most "performant" solutions.
Iterables
, IEnumerable
and yield
operator are one of those aspects.
Let's dive in a short C#
example / code block.
class Test {
public static void Core() {
foreach (string x in Foo()) {
Console.WriteLine(x);
}
}
static IEnumerable Foo() {
yield return Hello();
yield return there();
}
static string Hello() {
Console.WriteLine("HelloSub");
return "Hello";
}
static string there() {
Console.WriteLine("thereSub");
return "there";
}
}
The output is: HelloSub Hello thereSub there
The important thing here is that Foo() is called only once! The compiler forms a state machine where the
yield return "there"
statement is executed only after the output of "Hello"
.
Each time the internal (.NET) method
MoveNext()
is executed on an iterator (in this case, explicitly by the foreach statement), control is passed back to the Foo() function, right where it was paused.
Then it continues until the next "yield"
statement.
If you are familiar with co-routines from other languages, this is exactly what is happening here, but the compiler does all the heavy lifting itself.
A nice example of how the ordinary GetEnumerator()
function and associated theory can be presented in a different way as an art form, which you might find useful someday.
For further reading, please visit the wiki Iterator.
Bonus
One small tip: when you considering how to name your next namespace,
DON'T name it OFFLINING. "😂" (c)
Happy coding.