Auto Implemented

An auto implemented property is equivalent to a property value that is stored in a private field
This is also sometimes referred to as Automatic Properties

public string Property_Name { get; set; } 

replaces

private string m_sName; 

public string Property_Name
{
   get { return m_sName; }
   set { m_sName = value; }
}

You can also create a read-only auto-implemented property by inserting the private keyword.

public string Property_Name { get; private set; } 

You cannot use auto implemented in the following cases:
add extra code to the Get and Set procedure
you don't want public accessors for get and set
properties that are writeonly or readonly
you want the property to accept parameters


VB allows a property to accept parameters, C# does not.



Auto Initializing

Added in .NET 4.6
This allows assignment of properties directly within their declaration
Before you could only initialise the value of a property in the classes constructor.


class MyClass 
{
   public string Property_One { get; } = System.Environment.Username;
}

Instead of

class MyClass 
{
   public string Property_One()
   {
      Property_One - System.Environment.Username;
   }
   public string Property_One { get; }
}


© 2024 Better Solutions Limited. All Rights Reserved. © 2024 Better Solutions Limited TopPrevNext