property in C#

  • Thread starter Thread starter c676228
  • Start date Start date
C

c676228

Hi all,
I usually can see property codes like this way:

public int? CustomerID
{
get { return (int?)this["customerID"]; }
set { this["customerID"] = value; }
}

What about the following?
if it is only get; presents there, what does that mean?
public List<string> Errors { get; }
public Control Form { get; }
public int? PrimaryKey { get; }
 
c676228 said:
Hi all,
I usually can see property codes like this way:

public int? CustomerID
{
get { return (int?)this["customerID"]; }
set { this["customerID"] = value; }
}

What about the following?
if it is only get; presents there, what does that mean?
public List<string> Errors { get; }
public Control Form { get; }
public int? PrimaryKey { get; }

A read-only property only has a get accessor, no set accessor.

The value would typically be stored in a private variable, and methods
of the class can set the value of the property by assigning it directly
to the private variable.
 
Peter,

I think you are right. It seems to me that this could be in higher level
class/interface definition and later on it's inherited or implemented with
regular property definition.
--
Betty


Peter Duniho said:
Hi all,
I usually can see property codes like this way:

public int? CustomerID
{
get { return (int?)this["customerID"]; }
set { this["customerID"] = value; }
}

What about the following?
if it is only get; presents there, what does that mean?
public List<string> Errors { get; }
public Control Form { get; }
public int? PrimaryKey { get; }

That means you cannot set the property.

Note that none of the above examples would compile in a class or struct,
because they use the automatic property syntax, and read-only automatic
properties aren't allowed. So I'm assuming those examples are either from
interface declarations, or are not literal C# examples.

Pete
 
Back
Top