0

What is the difference between the two method definitions, one with get one without? I understand that properties can have get and set keywords, but what about in normal methods like below?

public bool IsEmpty 
{
    get { return _end == _start; }
}

public bool IsEmpty () 
{
    return _end == _start;
}
Darren Young
  • 10,972
  • 36
  • 91
  • 150
finxxi
  • 28
  • 5
  • 6
    The first one compiles? You've left out the parentheses. – Jon Hanna Jul 16 '15 at 09:51
  • The second would be a method instead of a property if you would have added the parantheses `IsEmpty(){}` – Tim Schmelter Jul 16 '15 at 09:52
  • Methods have to have Parameters even if empty ! – AnthonyLambert Jul 16 '15 at 09:53
  • 1
    Functionality your code does the same thing. However, a property and a method are conceived to be used for different purposes. Take a look here. http://stackoverflow.com/questions/164527/exposing-member-objects-as-properties-or-methods-in-net – JBond Jul 16 '15 at 09:58
  • Sorry everybody, I misunderstood that they were methods. In fact, they were properties. Problem solved! – finxxi Jul 16 '15 at 10:07

1 Answers1

1

Neither are method definitions. The first is a read-only property definition:

public bool IsEmpty
{
    get { return _end == _start; }
}

The second looks the same, but misses the get keyword:

public bool IsEmpty
{
    return _end == _start;
}

So it won't compile. Make it a method definition by adding parentheses:

public bool IsEmpty()
CodeCaster
  • 147,647
  • 23
  • 218
  • 272
  • Thanks @CodeCaster, I think it is my own stupidity that I thought they were "Method".... problem resolved! – finxxi Jul 16 '15 at 10:07