property in C#

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; }
 
G

Göran Andersson

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.
 
C

c676228

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top