Properties Usage - C# Coding Standard 12

As we know C# is a object oriented language. Properties support encapsulation which is one of the pillar of object oriented concepts. Today we discuss properties usage coding standards in C#.

             Name the properties using noun or noun phrase

             Instead of public or protected variables, always use properties

             If property returning arrays or collection, property name should be ended with Collection

             If the method is logical data member then use property instead of method

             Always use automatic properties instead of normal properties if get and set method doesn't contain any logic

             Name the Boolean properties like IsConnected or IsValid 

             Read-only properties can set via only constructor and we can not change these values in any other places other than in constructor. So use read-only properties where user can not change the value

             Never use write-only properties, instead use method

             We need to set default value for all properties

             If a property returns an Enum then property name should be same as Enum name

             All properties which need to set, set the values in constructors

             If the set method of property throwing an exception, we need preserve the previous state of that property

             Don't throw the exception from get method of the property 

             Your properties should allow to set values in any order. Object should not depend on the order of the properties while setting the values

             While assigning the value to property, always validates the value in set method only not in any other places