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

Record declaration syntax in C Sharp (C#) - Mini Project: Build & Apply

Choose your learning style9 modes available
Record declaration syntax
📖 Scenario: You are creating a simple program to store information about a book using a record in C#.
🎯 Goal: Build a C# program that declares a record to hold book details and then creates an instance of that record.
📋 What You'll Learn
Declare a record named Book with two properties: Title and Author, both of type string.
Create an instance of the Book record with the title "The Great Gatsby" and author "F. Scott Fitzgerald".
Print the book's title and author using Console.WriteLine.
💡 Why This Matters
🌍 Real World
Records are useful to store simple data objects like books, people, or products with fixed properties.
💼 Career
Understanding record syntax helps in writing clean and concise data models in C# applications.
Progress0 / 4 steps
1
Declare the Book record
Write a record declaration named Book with two public properties: string Title and string Author.
C Sharp (C#)
Need a hint?

Use the syntax public record Book(string Title, string Author); to declare the record.

2
Create an instance of Book
Create a variable named myBook and assign it a new Book instance with Title set to "The Great Gatsby" and Author set to "F. Scott Fitzgerald".
C Sharp (C#)
Need a hint?

Use Book myBook = new Book("The Great Gatsby", "F. Scott Fitzgerald"); to create the instance.

3
Print the book details
Use Console.WriteLine to print the book's title and author in the format: Title: The Great Gatsby, Author: F. Scott Fitzgerald.
C Sharp (C#)
Need a hint?

Use string interpolation with $"Title: {myBook.Title}, Author: {myBook.Author}" inside Console.WriteLine.

4
Run the program to see the output
Run the program and observe the output. It should display: Title: The Great Gatsby, Author: F. Scott Fitzgerald.
C Sharp (C#)
Need a hint?

Make sure you run the program and check the console output matches exactly.