C# Properties2 min read

C# properties are the class members that provide a flexible way for the class to expose its private field. It provides a way to read, write, and perform some computation to the private fields of the class.

These properties have special methods called accessors that are used to set, get, and compute on private fields of the class. The two accessors used are the get accessor and set accessor.

  • get accessor: It specifies read-only property and returns a property value. We use it to get the private filed value as a public.
  • set accessor: It specifies write-only property and returns a property value. We use it to assign a new value to the private filed.

The properties can be read-only, write-only, or read-write property. The read-only implements get an accessor but no set accessor. The write-only implements set accessor but no get accessor whereas read-write implements both set and get accessors.

We use it in the following way in a program:

Let us go through a program to understand its use.


Example 1: C# program for Properties

We will write a program for a read-write property using a set and get accessors.

Output:

Student Name: John Mac
Studen Roll: 15


Example 2: C# program for read-only Properties

Output:

Student Name: Jacob Hill
Studen Roll: 17


MORE

Keith Number Program in C

In this tutorial, we will write a Keith number in C. It is one of the most asked questions in an interview. Before that, you …
Read More

Keith Number Program in C++

In this tutorial, we will write a Keith number in C++. It is one of the most asked questions in an interview. Before that, you …
Read More

Singly linked list Program C++

In this tutorial, we will write a C++ program to implement singly linked list. In this program, we will create a separate function for each …
Read More

Fascinating Number Program in C

In this tutorial, we will write a C program to check whether the given number is a Fascinating number or not. You may go through …
Read More

Peterson Number in C

In this tutorial, we will learn about the Peterson number and check if the number is Peterson or not in C. What is Peterson Number? …
Read More

Peterson Number in C++

In this tutorial, we will learn about the Peterson number and write a C++ program to check if the given number is a Peterson number …
Read More