Don't understand this construction

  • Thread starter Thread starter Tony Johansson
  • Start date Start date
T

Tony Johansson

Hello!

Can somebody explain the property construction Direction below. It seems
like a strange construction. This is more similar to how interface would
declare property.

public class SortAdorner : Adorner
{
public ListSortDirection Direction
{
get;
private set;
}
....
}

//Tony
 
Hello!

Can somebody explain the property construction Direction below. It seems
like a strange construction. This is more similar to how interface would
declare property.

public class SortAdorner : Adorner
{
public ListSortDirection Direction
{
get;
private set;
}
...
}

That is an "auto-implemented property" where the actual member is
private, anonymous backing field. In the example above the getter is
public but the setter is private.

One way the property may be set is to use constructor dependency
injection.

public SortAdorner (ListSortDirection listSortDirection)
: base()
{
ListSortDirection = listSortDirection;
}

regards
A.G.
 
Can somebody explain the property construction Direction below. It seems
like a strange construction. This is more similar to how interface would
declare property.

public class SortAdorner : Adorner
{
public ListSortDirection Direction
{
get;
private set;
}
...
}

It is an automatic property (added in C# 3.0/.NET 3.5) combined
with different visibility on get and set (added in C# 2.0/.NET 2.0).

http://msdn.microsoft.com/en-us/library/bb384054.aspx
http://msdn.microsoft.com/en-us/library/75e8y5dd.aspx

Arne
 
Hello!

Can somebody explain the property construction Direction below. It seems
like a strange construction. This is more similar to how interface would
declare property.

public class SortAdorner : Adorner
{
     public ListSortDirection Direction
     {
         get;
         private set;
     }
...

}

//Tony

This is an auto-implemented read-only property.
The "get" method returns an internal variable, but the property (and
variable)
cannot be set from outside the class. So, within the constructor or
other method,
you can set Direction to be anything you want, but to anyone using the
class, it
can only be read.

Matt
 
Back
Top