0
0
C Sharp (C#)programming~10 mins

Record declaration syntax in C Sharp (C#) - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - Record declaration syntax
Start
Write 'record'
Specify Name
Define Parameters (optional)
Add Body (optional)
Compile & Use Record
This flow shows how to declare a record: start with 'record', give it a name, optionally add parameters and body, then use it.
Execution Sample
C Sharp (C#)
record Person(string Name, int Age);

var p = new Person("Alice", 30);
Console.WriteLine(p.Name);
Declares a record Person with two properties, creates an instance, and prints the Name.
Execution Table
StepActionCode LineResult/Value
1Declare record Person with Name and Agerecord Person(string Name, int Age);Person type created with two properties
2Create instance p of Personvar p = new Person("Alice", 30);p.Name = "Alice", p.Age = 30
3Print p.NameConsole.WriteLine(p.Name);Output: Alice
4End of program-Program ends
💡 Program ends after printing the Name property of the record instance.
Variable Tracker
VariableStartAfter Step 2Final
pnullPerson(Name="Alice", Age=30)Person(Name="Alice", Age=30)
Key Moments - 2 Insights
Why do we not need to write properties explicitly inside the record?
Because the parameters in the record declaration automatically create properties, as shown in step 1 of the execution_table.
Can we create a record without parameters?
Yes, you can declare a record without parameters and add properties inside the body, but this example uses parameters for simplicity (see concept_flow).
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table, what is the value of p.Age after step 2?
A30
B"Alice"
Cnull
D0
💡 Hint
Check the 'Result/Value' column in step 2 where p is created with Age=30.
At which step does the program output 'Alice'?
AStep 2
BStep 3
CStep 1
DStep 4
💡 Hint
Look at the 'Action' and 'Result/Value' columns for the output in the execution_table.
If we remove parameters from the record declaration, what changes in the execution?
AThe program will print 'Alice' anyway
BThe instance p will have Name and Age set automatically
CThe record will have no properties automatically created
DThe program will not compile
💡 Hint
Refer to key_moments about parameters creating properties automatically.
Concept Snapshot
Record declaration syntax in C#:
record Name(Type1 Prop1, Type2 Prop2);
- 'record' keyword creates immutable data type
- Parameters auto-create properties
- Can add body for methods or extra properties
- Use like class but simpler for data storage
Full Transcript
This example shows how to declare a record in C#. We start by writing 'record' followed by the record name and parameters inside parentheses. These parameters automatically become properties of the record. Then we create an instance of the record by calling its constructor with values. Finally, we access a property and print it. The execution table traces each step: declaring the record, creating an instance, printing a property, and ending the program. The variable tracker shows how the variable 'p' changes from null to holding the record instance. Key moments clarify that parameters create properties automatically and that records can be declared without parameters but then need explicit properties. The quiz tests understanding of property values, output timing, and effects of removing parameters. The snapshot summarizes the syntax and behavior of record declarations.