Wednesday, January 1, 2014

C# - Members of a Class

A class can have following members inside it

Fields allow classes and structures to encapsulate data. A field can be given an initial value. Fields are initialized automatically before the constructor for the object instance is called. A field initializer cannot refer to other fields. 

Generally it is not recommended to use public fields. You should mark them as private and expose them via. Properties.

Properties
Is a flexible mechanism to read, write or compute value of a private field. Properties are actually special methods called accessors.

introduced in C# 3.0, Auto implemented properties make property declaration more easy. When you create a auto implemented property, compiler creates a private, anonymous backing field that can only be accessed through property's get and set accessors. To give auto implemented properties a default value you have to do it in the constructor but starting from C# 6.0 you can do it more easily.

fields, constants, properties, methods, constructors, destructors, events, indexers, operators as well as nested types
 
Using Fields vs. Properties

constants
 
methods
 
constructors
 
destructors 
 
events
 
indexers
 
operators
 
nested types

0 comments:

Post a Comment

Powered by Blogger.


Software Architect at Surge Global/ Certified Scrum Master

Experienced in Product Design, Software Engineering, Team management and Practicing Agile methodologies.

Search This Blog

Facebook