Posts

Showing posts from October, 2017

FAQ:

FAQ   Explain the data encapsulation principle?   - An object's data should be accessible only to the object - Becking fields containing the object data should be marked as private. What is a backing field? - A variable in a class used to retain each object's data When should you use a backing field?  - For every data field retained for an object  When should you use a constant?  - When defining a field with a simple data type that that will never change. When should you use a read-only field?   - When defining a field that is initialized from a file, table, or code, but should not change anywhere else in the application. What is the difference between a constant and a read-only field?  -  A constant Is static Assigned on the declaration Assigned to an expression that is fully evaluated at compile time.    - A Read-only field  can be static or non-static Assigned...

Constant vs Read-Only

Constant vs Read-Only Constant Field  Compile-time constant  meaning that it must be assigned to an expression that can be fully evaluated at compile-time. Assigned on declaration only number, boolean, or string always static Constants are best used with simple data type when the value will never change. Read-Only Field Runtime constant  Assigned to any valid expression at runtime Assign on declaration or constructor  any data type optionally static Read-only fields are best for any values that initialized from a file, a table, or same code, but cannot then be changed anywhere else within the application. If the read-only value is shared by instances, then make it as a static read-only field.

Read-Only Fields

Read-Only fields Read-only fields represent object data that is set and never changed for the lifetime of the object. A variable in a class, A read-only is a variable defined in a class.   holds a value that is initialized and then not changed. Must be initialized, in the declaration, or in a constructor Think of a read-only field as a runtime constant value. Read-only fields syntax Optional accessibility modifier, start with optionally accessibility modifier, in many cases, read-only fields are publicly accessible to the application. optional static keyword Readonly keyword Data type, any data type can be specified. Field name, PasclCasing Assigned to a value, must be initialized to a value, it can be in the declaration time.

Constants

Constants   Constants represent information that is hard-coded into the application. Defined in a class A constant is defined in a class and hold a hard-coded value that is expected to never change.   Example:  public const double pi = 3.14; public const int Red = 0xFF0000; public const double InchesPermeter = 39.37; Must be assigned to an expression that can be fully evaluated at compile time, think of constant as a "compile-time" constant value So a constant can't be assigned to a value returned from a method or retrieved from a file, nor can it be an object instance. Compiled into every location that references it Are static Constants are by definition static. That means we don't have to create an instance of the class to access them. We instead access them using the class name. Declaring A Constant Declaring a constant is a bit different from declaring a backing field. Optional accessibility modifier (but is ofte...

Defining Fields appropriately - Backing Fields

Fields hold the data in our application, so defining fields appropriately helps ensure that data is correctly identified and retained. Fields can by variable or Constance, read-only or write-only, public or private, built in or types define by the classes we write. Backing Fields Variable in a class Becking fields are the most common types of fields that we use in our class. Holds data for each object A backing field is simply a variable in a class that holds the data for each object we instantiate from that class. Data Encapsulation/ Information Hiding Because much of the data for our application, including company proprietary and operational data are stored in beaking fields, one best practice is to follow the principle of data encapsulation, also known as information hiding. The principle of data encapsulation specifies that an object's data should be accessible only to that object. so the backing fields that hold that data are privat...

Backing Fields- Nullable type

Nullable Type Allow definition of a value OR null a nullable type is a value type, such as integer, decimal or date, that allows definition of value or null.  Specified with a "?" suffix on the type A nullable type is specified by adding a question mark as the suffix on the type. Distinguishes "not set" from the value type. A nullable type is useful if the code needs to distinguish between not set and the type's default value. for example, if we declared the cost as a decimal value and that value was 0, does that mean that the cost was not yet set? or that the cost was set and the actual cost is 0?  A nullable type helps us makes that distinction. Nullable Type Best Practices Do:   use on simple types to distinguish  "not set" and "default value". Use properties of the type such as HasValue and Value as needed. Avoid: using them if they are not necessary