Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Error CS1014: A get or set accessor expected

Tags:

c#

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?

like image 692
tehricefarmer Avatar asked Jan 28 '26 16:01

tehricefarmer


2 Answers

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

like image 59
David Avatar answered Jan 31 '26 06:01

David


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

like image 42
SLaks Avatar answered Jan 31 '26 05:01

SLaks



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!