Maxim Gershkovich
Maxim Gershkovich

Reputation: 47169

Autogenerated properties in C#

In VB.NET it is possible to do the following in a class.

Public Property MyProperty As String

At this point a getter and setter is automagically created for you and you can refer to variable defined by the property as such.

Me._MyProperty = "BlahBlah"

Is there an equivalent mechanism in C# ?

Upvotes: 3

Views: 3129

Answers (2)

Neil N
Neil N

Reputation: 25258

public string MyProperty {get; set;}

by default they are both public accessors, you can make one of them private like this:

public string MyProperty {get; private set;}

Upvotes: 8

Rohan West
Rohan West

Reputation: 9298

In C# you cannot refer to the underlying variable of auto implemented properties directly.

Upvotes: 5

Related Questions