2
using System;

//Find the square root of a number for 10 values from user

class forLoop
{
    static void Main
    {

        double x;

        for(int i=10; i>0 && x>=0; i--)
        {


        Console.WriteLine("You have {0} remaining calculations left", i);
        Console.Write("Please enter a positive number: ");

        x = double.Parse((Console.ReadLine());
        x = Math.Sqrt(x);

        Console.WriteLine("The square root is {0}", x);
        Console.WriteLine("");
        }

    Console.WriteLine("You have 0 remaining calculations left");

    }

}

I need help on this C# problem: Why does the error: "A get or set accessor expected" come up at compile time?

1
  • where is the method ? Oct 26, 2016 at 23:32

3 Answers 3

8

You missed the () in method declaration. Thus, the compiler thinks at some level that you're declaring a Property (albeit it would then throw an error about the void type), not a Method

// Property
public int Property
{
    get { return _field; }
    set { _field = value; }
}

// Property, albeit a get-only property
public int Property => _field;

// Method
public int Method()
{
    return _field;
}

// Method
public int Method() => _field;

UPDATE: Since this is still being seen, I've updated the example values to better reflect their underlying types, and included examples of expression bodies introduced with C# 6

3

You need parentheses (()) in the method declaration.

0
1

Parentheses is required to differentiate a method from a property that requires the get/set syntax

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.