Free AI summary of this YouTube video by SumTube
#019 Struct in C# [ شرح بالعربي ] #struct #valuetype
TL;DR
In this tutorial, Engineer Essam Abdulnabi explains the fundamentals of C# structs and how they differ from classes. The lesson covers struct syntax rules, stack vs. heap memory allocation, value semantics, immutability, and readonly structs. Through practical implementations like a digital size calculator and C#'s built-in DateTime struct, viewers learn when and why to use structs over classes.
Key takeaways
- 5:07Structs are value types stored on the stack and cannot have parameterless constructors or field initializers in older C# versions
- 19:00Structs do not support inheritance or finalizers, and recommended instances should remain within 16 bytes for optimal performance
- 51:45Value types like structs emphasize immutability: modifying a struct value creates a new instance rather than altering the original object in place
- 55:45C#'s built-in primitive types (like int and bool) and DateTime are implemented as structs

Tap to enlarge
Most valuable section
For whom
C# developers preparing for technical interviews or seeking a deep understanding of memory management and value types.
Building a Custom DigitalSize Struct29:00
This section walks step-by-step through designing a complete struct from scratch, demonstrating constant conversion factors, custom constructors, and immutable method operations.
Detailed summary
1. Introduction and Agenda
0:00Engineer Essam Abdulnabi introduces episode 19 of the Full Stack Web Developer series. The episode focuses on C# structs, comparing them directly to classes in terms of behavior, memory allocation, and syntax constraints.
Key topics announced include value vs. reference semantics, readonly structs, maximum recommended size guidelines, and an analysis of the built-in DateTime struct.
2. Core Syntax and Constructor Restrictions
0:50The tutorial demonstrates declaring a basic struct and highlights syntax rules that distinguish structs from classes. Unlike classes, structs cannot declare parameterless constructors or use explicit field initializers in standard syntax.
Attempting to write parameterless constructors or assign initial values directly to fields inside a struct triggers compile-time syntax errors, emphasizing that default constructors are implicitly handled by the language specification.
3. Properties, Methods, and Destructor Limitations
10:22This section illustrates that structs fully support properties, standard methods, events, indexers, and operator overloading. However, structs cannot contain finalizers (destructors) since memory cleanup on the stack is handled automatically without garbage collector intervention.
Syntax examples confirm that defining a destructor (~Student) inside a struct triggers a compiler error stating that only class types can contain destructors.
4. Inheritance Rules and System.Object
14:01The video explains that custom inheritance is not supported for structs, meaning they cannot use protected members or serve as base types for other structs or classes. Despite this limitation, all structs implicitly inherit from System.Object.
This behavior ensures that every struct in .NET shares common object capabilities (like ToString or Equals) while maintaining strict performance constraints by avoiding runtime polymorphic overhead.
5. Memory Allocation and the 16-Byte Recommendation
18:38The discussion transitions to memory semantics: structs are value types stored directly on the stack, whereas class instances reside on the managed heap. Stack access is significantly faster due to sequential allocation.
Software engineering best practices recommend keeping struct data sizes under 16 bytes (equivalent to four 32-bit integers). Storing reference types like strings inside structs is discouraged because it undermines the performance benefits of stack allocation.
6. Instantiation Behavior: New Keyword vs. Direct Assignment
24:54The instructor demonstrates instantiated struct behavior using the 'new' keyword versus direct declaration. Using 'new' invokes implicit initializers to reset all fields to their zero or default values.
Declaring a struct variable without 'new' requires explicitly initializing every public field manually before invoking any instance methods; otherwise, the compiler rejects the code.
7. Building a Custom DigitalSize Struct
28:59To illustrate real-world application, the instructor models data storage units (Bit, Byte, KB, MB, GB, TB) within a custom DigitalSize struct. The underlying state is stored privately as a single primitive long value representing total bits.
Private long constants establish conversion factors between units, while public expression-bodied properties format calculated storage output cleanly for consumers.
8. Designing Immutable Methods
44:05This segment introduces immutability concepts within struct design. Methods like AddBit do not mutate internal state directly; instead, they compute scaled bit totals and return a fresh DigitalSize object instance.
The conceptual analogy of time is used: adding a minute to 12:00 PM creates a distinct moment (12:01 PM) rather than modifying the identity of 12:00 PM itself.
9. Readonly Structs
41:54The tutorial explores readonly structs, enforcing absolute state immutability across all fields. Applying the 'readonly' modifier to a struct declaration mandates that every internal instance field must also be marked readonly.
Under this design pattern, member values can only be set once during construction, preventing accidental field modifications across application workflows.
10. Exploring C#'s Built-in DateTime Struct
47:20The built-in DateTime structure is inspected in Visual Studio via Go To Definition to prove it is declared as a readonly struct. Examples show creating dates using default constructors and explicit year/month/day arguments.
Operations like AddDays demonstrate immutability in action: calling AddDays returns a new date value, requiring explicit variable reassignment to persist changes.
11. Primitive Data Types and Final Summary
55:52The episode concludes by linking standard .NET primitive types (such as int, bool, and double) back to structs. The unique exception of System.String—a heap-allocated reference type designed with built-in immutability—is also highlighted.
The instructor wraps up by encouraging practice with sample console applications and reviewing interview summary sheets before signing off.
Questions this video answers
What are the main structural differences between a struct and a class in C#?
A struct is defined using the struct keyword and represents a value type stored on the stack, whereas a class is a reference type stored on the heap. Structs cannot have explicit parameterless constructors (in older language specifications), field initializers, or finalizers (destructors). Additionally, structs do not support custom inheritance hierarchies, though they implicitly inherit from Syst
Why is it recommended to keep struct sizes under 16 bytes?
Because structs are value types copied by value upon assignment or method calls, passing large structs frequently causes unnecessary memory copying overhead on the stack. Keeping struct sizes small (under 16 bytes) ensures fast stack allocation and prevents performance degradation compared to heap-allocated reference types.
How does immutability work in structs like DateTime?
Immutability means that once a struct instance is created, its internal value cannot be directly mutated in place. Methods like DateTime.AddDays standardly return a brand-new DateTime struct containing the recalculated date value, leaving the original instance untouched unless explicitly reassigned.
Ask a follow-up Premium
About this summary
SumTube AI wrote this recap from the video's words and chapters. Credit and the original belong to Metigator | عصام عبدالنبي.
![#019 Struct in C# [ شرح بالعربي ] #struct #valuetype](https://i.ytimg.com/vi/rhupFUHBwvY/hqdefault.jpg)