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

Object instantiation with new in C Sharp (C#) - Mini Project: Build & Apply

Choose your learning style9 modes available
Object Instantiation with new in C#
📖 Scenario: You are creating a simple program to manage a library. Each book has a title and an author.
🎯 Goal: You will create a Book class and then create an object of this class using the new keyword. Finally, you will display the book's details.
📋 What You'll Learn
Create a class named Book with two public string fields: Title and Author.
Create an object of the Book class using the new keyword.
Assign the title "The Great Gatsby" and author "F. Scott Fitzgerald" to the object.
Print the book's title and author in the format: Title: The Great Gatsby, Author: F. Scott Fitzgerald.
💡 Why This Matters
🌍 Real World
Creating objects is how you represent real things in programs, like books in a library system.
💼 Career
Understanding object instantiation is fundamental for software development jobs using C# and object-oriented programming.
Progress0 / 4 steps
1
Create the Book class
Create a public class called Book with two public string fields: Title and Author.
C Sharp (C#)
Need a hint?

Use public class Book { public string Title; public string Author; } structure.

2
Create a Book object
Create a variable called myBook and instantiate it as a new Book object using the new keyword.
C Sharp (C#)
Need a hint?

Use Book myBook = new Book(); to create the object.

3
Assign values to the Book object
Assign the string "The Great Gatsby" to myBook.Title and "F. Scott Fitzgerald" to myBook.Author.
C Sharp (C#)
Need a hint?

Use myBook.Title = "The Great Gatsby"; and myBook.Author = "F. Scott Fitzgerald";.

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

Use Console.WriteLine($"Title: {myBook.Title}, Author: {myBook.Author}"); to print.