Conversation

Your input fuels progress! Share your tips or experiences on prioritizing mental wellness at work. Let's inspire change together!

Join the discussion and share your insights now!

Comments 0

Sharpen your coding skills—try JavaScript challenges on TOOLX now!

advertisement

Properties in C# { get ; set; }

properties in c#


Properties in C#

Access modifiers such as public, private, protected, and internal control the accessibility of fields and methods in C#. The public fields are accessible by other classes, but private fields are accessible only by the class in which they are declared. C# uses a feature called properties that allows you to set and retrieve values of fields declared with any access modifier securely. This is because properties allow you to validate values before assigning them to fields.

For example, consider fields that store the names and IDs of employees. You can create properties for these fields to ensure the accuracy and validity of values stored in them.


1. Applications of Properties

Properties allow you to protect a field in the class by reading and writing to the field through a property declaration. Also, properties allow you to access private fields that would otherwise be inaccessible. Properties can validate values before allowing you to change them and also perform specified actions on those changes. Therefore, properties ensure the security of private data. Properties support abstraction and encapsulation by exposing only necessary actions and hiding their implementation.


2. get and set Accessors

Property accessors allow you to read and assign a value to a field by implementing two special methods. These methods are referred to as the get and set accessors.

The get accessor is used to read a value and is executed when the property name is referred to. It does not take any parameter and returns a value that is of the return type of the property.

The set accessor is used to assign a value and is executed when the property is assigned a new value using the equal to (=) operator. This value is stored in the private field by an implicit parameter called value (keyword in C#) used in the set accessor.


3. Categories of Properties

Properties are broadly divided into three categories, read-only, write-only, and read-write properties.

Read-Only Property:-

The read-only property allows you to retrieve the value of a private field. To create a read-only property, you should define the get accessor. 

Write-only property:-

The write-only property allows you to change the value of a private field. To create a write-only property, you should define the set accessor.

Read-write property:-

The read-write property allows you to set and retrieve the value of a private field. To create a read-write property, you should define the set and get accessors.


4. Static Properties

The static property is declared by using the static keyword. It is accessed using the class name and thus, belongs to the class rather than just an instance of the class. Thus, a programmer can use a static property without creating an instance of the class. A static property is used to access and manipulate static fields of a class safely.


5. Abstract Properties

The abstract property is declared by using the abstract keyword. The abstract property contains the declaration of the property without the body of get and set accessors. The get and set accessors do not contain any statements. These accessors can be implemented in the derived class. An abstract property declaration is only allowed in an abstract class. An abstract property is used when it is required to secure data within multiple fields of the derived class of the abstract class. Further, it is used to avoid redefining properties by reusing the existing properties.


6. Boolean Properties

A boolean property is declared by specifying the data type of the property as bool. Unlike other properties, the boolean property produces only true or false values.

While working with a boolean property, a programmer must be sure that the get accessor returns the boolean value.

Note: A property can be declared static using the static keyword. A static property is accessed using the class name and is available to the entire class rather than just an instance of the class. The set and the get accessors of the static property can access only the static members of the class.


7. Implementing Inheritance

Properties can be inherited just like other members of the class. This means the base class properties are inherited by the derived class.


8. Auto-Implemented Properties

C# provides an alternative syntax to declare properties where a programmer can specify a property in a class without explicitly providing the get and set accessors. Such properties are called auto-implemented properties and result in more concise and easy-to-understand programs. For an auto-implemented property, the compiler automatically creates a private field to store the property variable. In addition, the compiler automatically creates the corresponding get and set accessors.


9. Object Initializers

In C#, the programmer can use object initializers to initialize an object with values without explicitly calling the constructor. The declarative form of object initializers makes the initialization of objects more readable in a program. When object initializers are used in a program, the compiler first accesses the default instance constructor of the class to create the object and then performs the initialization.


10. Implementing Polymorphism

Properties can implement polymorphism by overriding the base class properties in the derived class. However, properties cannot be overloaded.


Properties, Fields, and Methods

A class in a c# program can contain a mix of properties, fields, and methods each serving a different purpose in the class. It is important to understand the differences between them to use them effectively in the class.


1. Properties versus fields

Properties are similar to fields as both contain values that can be accessed. However, there are certain differences between them.

Properties:-

  • Properties are data members that can assign and retrieve values.
  • Properties cannot be classified as variables and therefore, cannot use the ref and out keywords.
  • Properties are defined as a series of executable statements.
  • Properties are defined with two accessors or methods, the get and set accessors.
  • Properties can perform custom actions on the change of the field's value.

Fields:-

  • Fields are data members that store values.
  • Fields are variables that can use the ref and out keywords.
  • Fields can be defined in a single statement.
  • Fields are not defined with accessors.
  • Fields are not capable of performing customized actions.


2. Properties versus Methods

The implementation of properties covers both, the implementation of fields and the implementation of methods. This is because properties contain two special methods and are invoked in a similar manner as fields. There are a few differences between properties and methods.

Properties:-

  • Properties represent the characteristics of an object.
  • Properties contain two methods that are automatically invoked without specifying their names.
  • Properties cannot have any parameters.
  • Properties can be overridden but cannot be overloaded.

Methods:-

  • Methods represent the behavior of an object.
  • Methods are invoked by specifying method names along with the object of the class.
  • Methods can include a list of parameters.
  • Methods can be overridden as well as overloaded.


C# Properties in c# applications of properties in c# get and set accessors in c# categories of properties in c# read only property in c# write only property in c# read write property in c# static properties in c# abstract properties in c# boolean properti

advertisement