tehricefarmer tehricefarmer - 7 months ago 61
C# Question

Error CS1014: A get or set accessor expected

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("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?


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