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

Record declaration syntax in C Sharp (C#) - Cheat Sheet & Quick Revision

Choose your learning style9 modes available
Recall & Review
beginner
What is a record in C#?
A record is a special kind of class in C# designed to hold immutable data with built-in value-based equality.
Click to reveal answer
beginner
How do you declare a simple record with two properties: Name (string) and Age (int)?
You declare it like this:<br>
public record Person(string Name, int Age);
Click to reveal answer
beginner
What keyword is used to declare a record in C#?
The keyword record is used instead of class or struct.
Click to reveal answer
intermediate
Can records have methods and custom constructors?
Yes, records can have methods and custom constructors just like classes, allowing you to add behavior.
Click to reveal answer
intermediate
What is the difference between positional records and standard records?
Positional records declare properties in the record header for concise syntax, while standard records declare properties inside the body.
Click to reveal answer
Which keyword is used to declare a record in C#?
Aclass
Bstruct
Crecord
Dinterface
How do you declare a record with properties Name (string) and Age (int) using positional syntax?
Apublic class Person { string Name; int Age; }
Bpublic record Person(string Name, int Age);
Cpublic struct Person(string Name, int Age);
Dpublic record Person { string Name; int Age; }
What feature do records provide by default?
ANo inheritance
BMutable properties only
CNo constructors allowed
DValue-based equality
Can you add methods inside a record declaration?
AYes, records can have methods.
BNo, records cannot have methods.
COnly static methods are allowed.
DOnly abstract methods are allowed.
Which of these is a valid way to declare a record with a custom constructor?
Apublic record Person { public string Name { get; init; } public Person(string name) { Name = name; } }
Bpublic record Person(string Name) { public Person() { } }
Cpublic record Person(string Name); public Person() { }
Dpublic record Person { string Name; Person(string name) { Name = name; } }
Explain how to declare a positional record in C# and why you might use it.
Think about how to write properties directly in the record header.
You got /5 concepts.
    Describe the main benefits of using records over classes for data storage.
    Focus on how records handle data differently than classes.
    You got /5 concepts.