Record declaration syntax in C Sharp (C#) - Time & Space Complexity
Let's see how the time it takes to create records grows as we make more records.
We want to know how the work changes when we declare many records.
Analyze the time complexity of the following code snippet.
record Person(string Name, int Age);
var people = new List<Person>();
for (int i = 0; i < n; i++)
{
people.Add(new Person($"Person{i}", i));
}
This code creates a list of Person records, adding n new records with different names and ages.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: Creating a new
Personrecord and adding it to the list. - How many times: This happens once for each number from 0 up to
n - 1, sontimes.
Each time we increase n, we create and add more records, so the work grows steadily.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | 10 record creations and additions |
| 100 | 100 record creations and additions |
| 1000 | 1000 record creations and additions |
Pattern observation: The work grows directly with the number of records we create.
Time Complexity: O(n)
This means if you double the number of records, the time to create them roughly doubles too.
[X] Wrong: "Creating records is instant and does not depend on how many we make."
[OK] Correct: Each record creation takes some time, so making more records means more total time.
Understanding how creating many objects or records affects time helps you write efficient code and explain your reasoning clearly.
"What if we changed the loop to create records only for even numbers? How would the time complexity change?"
Practice
record in C#?Solution
Step 1: Understand what a record is
A record is a special type in C# designed to hold data immutably.Step 2: Identify the main feature
Records compare by value, not by reference, and are immutable by default.Final Answer:
It provides immutable data with value-based equality. -> Option AQuick Check:
Record = Immutable + Value Equality [OK]
- Thinking records are mutable by default
- Confusing records with classes for database use
- Believing records cannot have properties
int Id and string Name?Solution
Step 1: Recall record declaration syntax
Records use parentheses with comma-separated parameters for properties.Step 2: Check each option
record Person(int Id, string Name); uses correct syntax: parentheses and commas. record Person { int Id; string Name; } uses braces without property syntax. record Person = (int Id, string Name); uses '=' incorrectly. record Person(int Id; string Name); uses semicolon instead of comma.Final Answer:
record Person(int Id, string Name); -> Option BQuick Check:
Record syntax = parentheses + commas [OK]
- Using braces instead of parentheses
- Using semicolons instead of commas
- Adding '=' sign incorrectly
record Point(int X, int Y);
var p1 = new Point(3, 4);
var p2 = new Point(3, 4);
Console.WriteLine(p1 == p2);
Solution
Step 1: Understand record equality behavior
Records compare by value, so two records with same property values are equal.Step 2: Analyze the code
p1 and p2 have same X and Y values (3,4), so p1 == p2 returns true.Final Answer:
True -> Option AQuick Check:
Records compare by value = True [OK]
- Assuming reference equality instead of value equality
- Expecting false because of different instances
- Thinking it causes a compilation error
record Car(string Make, string Model)
{
public int Year;
}
Solution
Step 1: Understand record members
Records can have members, but fields should be properties for immutability.Step 2: Analyze the code
The code declares a public field 'Year' instead of a property, which is not recommended and causes issues.Final Answer:
Fields inside records must be declared as properties, not fields. -> Option DQuick Check:
Use properties, not fields inside records [OK]
- Using fields instead of properties inside records
- Missing semicolon after record declaration (not required here)
- Confusing braces and parentheses usage
Book with properties string Title, string Author, and an optional int? Year that defaults to null. Which is the correct way to declare it?Solution
Step 1: Use concise record syntax with default parameter
Records allow default values in the parameter list using '='.Step 2: Check each option
record Book(string Title, string Author, int? Year = null); correctly declares Year as nullable with default null. record Book(string Title, string Author, int? Year); lacks default. record Book { string Title; string Author; int? Year = null; } uses invalid field syntax. record Book(string Title, string Author) { public int? Year = null; } uses field with default inside braces, which is not idiomatic.Final Answer:
record Book(string Title, string Author, int? Year = null); -> Option CQuick Check:
Default values go in parentheses parameters [OK]
- Omitting default value for optional parameters
- Using fields instead of properties inside braces
- Trying to assign default values inside braces
Ready to prove it?
Pass the test and this pattern moves unknown
