#1,218 – C# 6.0 – Using Lambdas for Getter-Only Auto-Properties

In addition to using lambda expressions for method bodies in C# 6.0, you can use a lambda expression to implement the getter of a read-only auto-property.

For example:

        public string Name { get; protected set; }
        public int Age { get; set; }

        public string BackwardsName => new string(Name.Reverse().ToArray());

The presence of the lambda expression tells the compiler that this is a property with a getter, rather than a field.

#1,216 – C# 6.0 – Initializing Read-Only Auto-Properties from Constructors

NOTE: The following is proposed syntax for Roslyn, but not yet working in the Visual Studio 2014 CTP, as of 30 Oct 2014.  The Codeplex site lists this as a “Done” feature for Roslyn, but it’s not absolutely clear whether this will ship with Visual Studio 2014.

In C# 6.0, you’ll be able to define read-only auto-properties you can initialize the property as part of its declaration.

  public DateTime DogCreationTime { get; } = DateTime.Now;

The intent in C# 6.0 is that you’ll also be able to initialize these read-only auto-properties from a constructor.  For example:

    public class Dog
    {
        public string Name { get; }

        public Dog(string name)
        {
            Name = name;
        }
    }

#1,206 – C# 6.0 – Auto-Property Initializers

C# 3.0 introduced auto-implemented properties.

Older syntax:

private string name = "default";
public string Name
{
    get { return name; }
    set { name = value; }
}

Improved syntax, using auto-implemented properties:

public string Name { get; set; }

The new syntax provided no mechanism for specifying a default property value. To specify a default value, you either needed to go back to using an explicit backing variable or to set the default value from within a constructor.

C# 6.0 supports specifying default values for auto-implemented properties, as shown below.

        public string Name { get; set; } = "default";

#800 – A Property in an Interface May Include One or Two Accessors

Defining a property in an interface, you can define one or both of the property’s accessors.

    public interface IDogInfo
    {
        int CollarSize { get; set; }
        string Name { get; }
        string SecretName { set; }
    }

When you implement an interface, if a property in the interface has both accessors, you must implement both accessors, and both must be public (you set an access modifier of public on the entire property).

If a property in the interface includes only one accessor, you must implement that accessor, but you can optionally also implement the other accessor.

    public class Dog : IDogInfo
    {
        public int CollarSize { get; set; }

        // IDogInfo has just get
        public string Name
        {
            get { return "NotImpl"; }
        }

        // IDogInfo has just set, but we add a get
        public string SecretName { protected get; set; }
    }

#798 – You Can’t Override Accessors that Are Not Accessible

When you override a virtual property in a child class, you must replicate the accessibility of the accessors in the parent’s class.

However, if one of the accessors has been made inaccessible to the child class, you’ll be able to override the property itself, but not that accessor.

In the example below, we override the Description property, including the get accessor.  However, if we try to include the set accessor in the overridden property, we get a compile-time error because the set accessor is not accessible to the child class.

    public class Animal
    {
        protected string description;
        public virtual string Description
        {
            get { return string.Format("Animal: {0}",description); }

            private set
            {
                if (value != description)
                    description = value;
            }
        }
    }
    public class Dog : Animal
    {
        public override string Description
        {
            get { return string.Format("Dog: {0}", description); }

            private set
            {
                if (value != description)
                    description = value;
            }
        }
    }

798-001

#797 – Setting Accessibility for Property Accessors

By default, the accessibility of a property accessor matches the accessibility of the property itself.  You can also explicitly set the accessibility of an accessor, within certain limitations.

When setting the accessibility of a property accessor:

  • You can’t make an accessor more accessible than the property itself
  • You can’t specify the exact same accessibility on the accessor as the property
  • The property must have both get and set accessors defined
  • You can only set accessibility on one of the accessors, not both
  • You can’t set the accessibility of an accessor on a property in an interface
  • When overriding a virtual property in a parent class, you must replicate the accessibility of the accessors in the parent’s class exactly

The bottom line–you can make one of the accessors a bit more restrictive than the other.

        public string Name { get; protected set; }

#796 – Default Accessibility for Property Accessors

When you implement a property in a class, you define the property’s accessibility, e.g. public, private, protected, internal or protected internal.

Client code uses a property like a field, reading and writing the property’s value.  The property is actually implemented as a two accessor methods.  The get accessor is called when some code reads the property’s value and the set accessor is called when some code write’s the property’s value.

By default, the accessibility of the two property accessors matches the accessibility of the property itself.

        // Property is public, so both get and set accessors are public
        public string Name { get; set; }

        // Property is private, so both get and set accesors are private
        private string SecreteName { get; set; }