Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

C# C# Basics C# Syntax Comments

I give up on C#!

My first program is a JOKE!

using System;

class Program { static double Price(int quantity) { double pricePerUnit; if (quantity >= 100) { pricePerUnit = 1.5; }

    It won't RUN
Simon Coates
Simon Coates
8,481 Points

I couldn't see where that code came from to see context (whether it's intended as complete code expected to run, or snippet to demonstrate some facet) or the problem it's trying to solve. However, if it's helpful, I was able to run the following in an online csharp repl.

using System;

class Program { 
    static double Price(int quantity) {
        double pricePerUnit = 1; 
        if (quantity >= 100) 
        { 
            pricePerUnit = 1.5;
        }
        return pricePerUnit * quantity;
    }
    public static void Main(){        
        Console.WriteLine(Price(10)); //prints 10
        Console.WriteLine(Price(100)); //prints 150
    }
}

The code you posted seemed incomplete. I just added enough code to guarantee that Price returned a value and then made sure the code could execute by adding a main method. You'd presumably need to make sure you save the code and compiled it prior to running. Under most circumstances, if the code is faulty, compiling or running should produce a human readable error message.

1 Answer

Reggie Williams
STAFF
Reggie Williams
Treehouse Teacher

Hey Lynn Collins don't worry, you're still very early in the courses. Have you completed all of the videos related to the program? The code you shared is shown in one of the previous videos but all of the instruction for writing that program isn't shown yet