© 2026 Hedgehog Software, LLC

TwitterGitHubDiscord
More
CommunitiesDocsAboutTermsPrivacy
Search
Star
Setup for Free
C#C
C#•4mo ago•
75 replies
strikeouts27

Understanding get; set; backing field requirements.

Per the instructions (full instructions listed in the comments)

"make a ToString() method that returns a string containing all job information in the following format."
Job 111 Smith exterior paint 20 hours @$45.00 per hour. Total price is $900.00


my ToString Method so far 
            public override string ToString()
            {
                Console.WriteLine($"Job {jobNumber} {customer} exterior paint {hours} hours @{price} per hour. Total price is {totalPrice}");
            }
my ToString Method so far 
            public override string ToString()
            {
                Console.WriteLine($"Job {jobNumber} {customer} exterior paint {hours} hours @{price} per hour. Total price is {totalPrice}");
            }


The very last {} totalPrice needs to be created. Which requires defining it in a get set

            private const double hourlyrate = 45.00;
            private double hours;
            private double totalPrice; 
            private double totalPrice
            {
                get; set
                {
                    hours = value;
                    price = hourlyrate * price;
                }
            }
            private const double hourlyrate = 45.00;
            private double hours;
            private double totalPrice; 
            private double totalPrice
            {
                get; set
                {
                    hours = value;
                    price = hourlyrate * price;
                }
            }


I was under the impression that if you are going to write a custom get set that is NOT an auto property, you must create a backing field for it. So I made a totalPrice double and tried to run the code. I got this error.

why is this?

/workspaces/9780357429235_farrell-c-sharp-aaba885a-f662-4800-a980-c6b80f6b520c/chapter10/ex03A/student/JobDemo.cs(57,41): error CS0501: 'JobDemo.Job.totalPrice.get' must declare a body because it is not marked abstract, extern, or partial [/workspaces/9780357429235_farrell-c-sharp-aaba885a-f662-4800-a980-c6b80f6b520c/chapter10/ex03A/student/JobDemo.csproj]
/workspaces/9780357429235_farrell-c-sharp-aaba885a-f662-4800-a980-c6b80f6b520c/chapter10/ex03A/student/JobDemo.cs(57,41): error CS0501: 'JobDemo.Job.totalPrice.get' must declare a body because it is not marked abstract, extern, or partial [/workspaces/9780357429235_farrell-c-sharp-aaba885a-f662-4800-a980-c6b80f6b520c/chapter10/ex03A/student/JobDemo.csproj]
C# banner
C#Join
We are a programming server aimed at coders discussing everything related to C# (CSharp) and .NET.
61,871Members
Resources

Similar Threads

Was this page helpful?
Recent Announcements
Next page

Similar Threads

Not really understanding Backing Fields with custom get; and set;
C#CC# / help
2mo ago
Help understanding "get" and "set"
C#CC# / help
4y ago
✅ Backing Field and Primary Constructors
C#CC# / help
11mo ago
✅ HashSet property with List backing field
C#CC# / help
4y ago