0

I'm receiving build errors in the class properties when getting & setting an enumeration of another class that has it's own separate getters & setters. There are two of the same errors in the get {} statement of the following code. I'm not sure as to how to resolve the build errors that are displayed as: 'Only assignment, call, increment, decrement, await, and new object expressions can be used as a statement'.

   public class Data
    {

        private IEnumerable<Client> _clientData;

        private IEnumerable<Company> _companyData;

        public IEnumerable<Client> ClientData
        {
            get { _clientData ?? (_clientData = new List<Client>()); }
            set { _clientData = value; }
        }

        public IEnumerable<Company> CompanyData
        {
            get { _companyData ?? (_companyData = new List<Company>()); }
            set { _companyData = value; }
        }
    }
cetmrw791346
  • 153
  • 1
  • 3
  • 13

1 Answers1

5

You need a return statement:

get { return _clientData ?? (_clientData = new List<Client>()); }

Even if you didn't have a return statement missing, you basically can't use ?? (or indeed ? :, or property getters) as a statement on its own. These are expressions to be used in other statements.

So for example:

String x = "Hello";

x.Length; // Invalid - just a property fetch can't be a statement
x.ToString(); // Pointless, but valid - method calls are statements
int y = x.Length; // Valid, assignment statement using property fetch
Jon Skeet
  • 1,421,763
  • 867
  • 9,128
  • 9,194