Properties In C#
- In C# class has another type of members called properties or regular properties.
- A property is used to access a private field / instance variable outside from a class.
- A property can be used as an expression as well as can be assign to variables like a normal variable / field.
- As compare with methods a property is more secure, better approach and powerful for access to private fields, because we have more controls over on fields using a property.
- A property has two keywords [get] and [set] which are called accessors where the [set] accessor initializes a private field and [get] accessor returns that private field through a property.
- Implementation of the properties make object oriented programing more professional.
- Like other members of the class properties can be static or no-static.
General Syntax:
access-modifier type PropertyName
{
get { // code for get accessor }
set { // code for set accessor }
}
STATEMENT | DESCRIPTION |
---|---|
access-modifier | It should be public (Recommended). |
type | It defines a type of a property such as an int, a class, string, structures, enumeration etc. |
PropertyName | This is a name of a property, should be any meaningful name. |
{ | Starting scope or body of a property. |
get { // code for get accessor } | This is a get accessor which contains keyword return which returns the value of a private field through that property. |
set {// code for set accessor } | This is a set accessor which has a keyword value which receives a value automatically and assigns or initializes a value to a private filed or property. The value keyword works as a parameter. |
} | Closing scope or body of a property. |
Example:
using System; namespace csharpBasic { class MarkSheet { // Fields declaration with private access. private double AspMarks; private double JavascriptMarks; private static double TotalMarks; // A static property declaration of double type which initializes TotalMarks and returns. public static double GetTotalMarks { // property starting scope. get { // get accessor which returns TotalMarks fields. return TotalMarks; } set { // set accessor which assigns value to TotalMarks field through "value" parameter. TotalMarks = value; } } // End of property. // Parametrized constructor declaration. public MarkSheet(double aspMarks, double javascriptMarks) { AspMarks = aspMarks; JavascriptMarks = javascriptMarks; } // Return type method returns obtain marks. public double GetObtainMarks() { return AspMarks + JavascriptMarks; } // Return type method returns percentage of obtain marks. public double GetPercentage(double obtainMarks, double totalMarks) { return (obtainMarks / totalMarks) * 100.0; } } class Program { static void Main(string[] args) { // Marksheet object is being created. MarkSheet markSheet = new MarkSheet(80, 70); // Statement gets obtain marks by calling GetObtainMarks() method. double obtainMarks = markSheet.GetObtainMarks(); // Statement is accessing property GetTotalMarks and assigns 200 value to it. double totalMarks = MarkSheet.GetTotalMarks = 200; Console.WriteLine("Total Marks {0}", totalMarks); Console.WriteLine("Obtain marks: {0}", obtainMarks); Console.WriteLine("Pecentage: {0}", markSheet.GetPercentage(obtainMarks, totalMarks)); Console.ReadKey(); } } /* The Output will be: Total Marks 200 Obtain marks: 150 Pecentage: 75 */ }
REMEMBER:
- A property does not define any storage location for fields by itself OR a property does not store a field by itself, it simply manages the access of private fields or instance variables. It is just a way for class fields/instance variables access management.
- By the use of a property you have full control on fields/instance variables.
- A class either has a read only property or write only and also can be both read-write property. If you want to make a read only property simply use a get accessor or want to write only property use only set accessor or use both get and set accessor for read-write property.