Properties in C#

What are properties in C#? What are the advantages of using Properties ?

Questions by Velvizhi.Sadanah

Showing Answers 1 - 9 of 9 Answers

Properties are members of classes. They are also used in structs and interfaces.
Properties are an extension of fields but they do not designate storage locations.
Properties provide a flexible mechanism to read, write, or compute the values of private fields through accessors that read, write, or compute their values.

Below example shows car Model and car Make properties for Car class

Code
  1. using System;

  2. class Car

  3. {

  4.     private string carModel ="Ambasador";

  5.     private int carMake = 0;

  6.  

  7.     public string Model

  8.     {

  9.         get

  10.         {

  11.            return carModel;

  12.         }

  13.         set

  14.         {

  15.            carModel = value;

  16.         }

  17.     }

  18.  

  19.     public string Make

  20.     {

  21.         get

  22.         {

  23.            return carMake;

  24.         }

  25.         set

  26.         {

  27.            carMake = value;

  28.         }

  29.     }

  30. }

Riki

  • Sep 1st, 2015
 

Properties are a level of abstraction to access private fields of a class and they are declare as public. Field are always private member of a class.

  Was this answer useful?  Yes

Give your answer:

If you think the above answer is not correct, Please select a reason and add your answer below.

 

Related Answered Questions

 

Related Open Questions