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

Why classes are needed in C Sharp (C#) - See It in Action

Choose your learning style9 modes available
Why Classes Are Needed
📖 Scenario: Imagine you are organizing a library. You want to keep track of books with their title, author, and number of pages. Instead of writing separate variables for each book, you can use a class to group all this information together.
🎯 Goal: You will create a simple Book class to hold information about a book. Then, you will create an object of this class and display its details.
📋 What You'll Learn
Create a class named Book with three public fields: Title, Author, and Pages.
Create an object of the Book class named myBook.
Assign the values "The Great Gatsby" to Title, "F. Scott Fitzgerald" to Author, and 180 to Pages.
Print the book details in the format: Title: The Great Gatsby, Author: F. Scott Fitzgerald, Pages: 180.
💡 Why This Matters
🌍 Real World
Classes are used everywhere in software to model real-world things like books, cars, or users, making programs easier to build and understand.
💼 Career
Understanding classes is essential for any software developer because they form the foundation of object-oriented programming, which is widely used in industry.
Progress0 / 4 steps
1
Create the Book class
Create a public class called Book with three public fields: string Title, string Author, and int Pages.
C Sharp (C#)
Need a hint?

Think of a class as a blueprint. Here, Book is the blueprint with fields to hold information.

2
Create a Book object and assign values
Create an object named myBook of type Book. Assign "The Great Gatsby" to myBook.Title, "F. Scott Fitzgerald" to myBook.Author, and 180 to myBook.Pages.
C Sharp (C#)
Need a hint?

Use new Book() to create the object, then set each field using the dot . operator.

3
Write code to display the book details
Write a Console.WriteLine statement to print the book details in this exact format: Title: The Great Gatsby, Author: F. Scott Fitzgerald, Pages: 180 using myBook fields.
C Sharp (C#)
Need a hint?

Use string interpolation with $"...{variable}..." to insert values inside the string.

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

Make sure your output matches exactly, including commas and spaces.