简体   繁体   中英

Why is my class attributes not being printed to the console?

Hi I'm new here and was hoping to get some guidance on figuring out why I am not getting the expected output. I am following a tutorial about on C# and I've gotten to the topic of creating and using classes. Here is the specific video link starting at the right minute mark. https://youtu.be/GhQdlIFylQ8?t=12868

1. Problem When I run my program the terminal outputs 1 follow by a line break. It is supposed to output a sting called "Harry Potter".

2. What I've tried I've tried to remove the 'public' from each class attributes to see if it made a difference. I've tried to print out the class attribute.author instead of the.title

3. Code

Here is my program code:

using System;

namespace Giraffe
{
    class Program
    {
        static void Main(string[] args) // method or function (contaier for code) this is the mehtod that gets executed
        {
            // thi is a book object. an actual physical book inside the progam an instance 
            Book book1 = new Book();

            // accessing the books attribute by writing book1.'title' 'author' 'pages'
            // Defining the attributes with variables
            book1.title = "Harry Potter";
            book1.author = "JK Rowling";
            book1.pages = 400;

            Console.WriteLine(book1.title);

            Console.ReadLine();
           
        }

        
    
    }
}

Program.cs with terminal output

Here is my Book class code:

using System;
namespace Giraffe
{
    public class Book
    {
        // define a series of class attributes essentially varibales that hold informaiton that describes what a book is.
        // template/blueprint for the data type of a book
        public string title;
        public string author;
        public int pages;
    }
}

Book.cs the class file

Ok I figured it out. There was some error in the brackets that the IDE kept throwing so I deleted all the code in the Book.cs file and got the brackets right before writing code inside of them.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM