Skip to content

Instantly share code, notes, and snippets.

@rickyah
Last active August 29, 2015 13:58
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save rickyah/10016780 to your computer and use it in GitHub Desktop.
Save rickyah/10016780 to your computer and use it in GitHub Desktop.
csharper_properties

#C# Properties

Properties are getters & setters but well done.

They require minimal code for setup:

public int MyProperty {get;set;}

This declares a completely functional property. The compiler generates the member to store the property data automatically.

You can attach different accesor modifiers (public, private, internal, protected) to either the getter or the setter to still use this mininal variable form and allow some nice things:

ReadOnly properties

public int MyProperty {get; private set;}

WriteOnly properties

public int MyProperty {private get; set;}

Writeable by derived classes only properties

public int MyProperty {get; protected set;}

Pros

  • If you need to change the implementation in the future the property public contract does not change. This will allow different naming conventions for public members (camelCase) and properties (PascalCase)

  • Auto-generated properties are as fast as member properties thanks to compiler optimizations ((See the Benchmark code):

    • set member value 100000000 times Time Elapsed 494,4034 ms
    • get member value 100000000 times Time Elapsed 554,156 ms
    • set property value 100000000 times Time Elapsed 545,533 ms
    • get property value 100000000 times Time Elapsed 518,0048 ms

Cons

  • Public Properties do not show as fields in the Unity Inspector by default (but there are ways to bypass this limitation :)

###Properties initialization

Initialize properties using the default constructor (constructor with no arguments). Unity uses this constructor to instanciate a MonoBehaviour so it's fine. This will allow to to keep the initialization of all internal data of a class in a common place.

###Static properties initialization Do it using the class's static constructor:

public class MyClass
{
	int static MyProperty {get;set;}
	static MyClass()
	{
		MyProperty = 42;
	}
}

### Using properties

As a convention properties are NOT used when heavy computations are involved in the get or set operations. Methods should be used instead.

In addition to the obvious usages, properties should be used to:

  • Perform lazy initialization
/// This is NOT a singleton
public NeedJustOneInstanceOfThis object Instance
{
    get 
    {
        if (_instance == null)
        {
            _instance = new NeedJustOneInstanceOfThis();
        }
        
        return _instance;
    }
    
}
  • Expose aggregated data (e.g. a read-only property that returns a value that depends on internal data structure)
public int PatrolPointsCount
{
    get { return _patrolPointsAir.Length + _patrolPointsEarth.Length; }
}
  • Enforce constraints when updating the state (e.g. you update a property and that makes another property to change)
public EState InitialState
{
    get { return _initialState; }
    
    set 
    { 
        // Play the intro animation just once
        if (value == EState.INTRO && _hasPlayedIntroAnimationAlready)
        {
            value = EState.IDLE;
        }
        
        _initialState = value;
    }
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment