• Skip to main content
  • Skip to primary sidebar
  • Skip to secondary sidebar
  • Skip to footer

Computer Notes

Library
    • Computer Fundamental
    • Computer Memory
    • DBMS Tutorial
    • Operating System
    • Computer Networking
    • C Programming
    • C++ Programming
    • Java Programming
    • C# Programming
    • SQL Tutorial
    • Management Tutorial
    • Computer Graphics
    • Compiler Design
    • Style Sheet
    • JavaScript Tutorial
    • Html Tutorial
    • Wordpress Tutorial
    • Python Tutorial
    • PHP Tutorial
    • JSP Tutorial
    • AngularJS Tutorial
    • Data Structures
    • E Commerce Tutorial
    • Visual Basic
    • Structs2 Tutorial
    • Digital Electronics
    • Internet Terms
    • Servlet Tutorial
    • Software Engineering
    • Interviews Questions
    • Basic Terms
    • Troubleshooting
Menu

Header Right

Home » C# » Advanced » Properties in C#
Next →
← Prev

Properties in C#

By Dinesh Thakur

Property is a feature to add more smartness to data fields for get and set the value.

Properties are named members of classes, structs and interfaces. They provide a flexible mechanism to read, write, or compute the values of private fields through accessors. Properties can be thought of as virtual fields. From the outside, a class’ property looks just like a field. But from the inside, the property is generated using the actual class fields.

Property declarations take just those modifiers taken by methods. Unlike languages like Java,C# provides dedicated support for accession and mutation of these properties. Suppose, for instance, that a type contains an internal field called ‘age’. With the following code one could specify a property Age, providing accessors and mutators to this internal field

 

public int Age

{

get

{

return this.age;

}

set

{

this.age = value;

}

}

Notice that the term ‘value’ is used in the above .piece of code. This variable always holds

the value passed to the ‘set’ block.· For instance, the execution of the following line of ode (assuming the appropriate class instance) would automatically set ‘value’ in the ‘set’ block to 4.

person.Age = 4;

This property Age can be described as ‘read-write’ since it can be both read from and written to. To make a property ‘write-only’ one simply does not specify a ‘get’ block; to make it ‘readonly’ one does not specify a ‘set’ block. The following piece of code demonstrates the read-only property ‘Adult’:

 

public bool Adult

{

get

{

if (this.age<18)

       return false;

else

return true;

}

}

A property declared using the static modifiers is classified as a static property; otherwise, it is classified as an instance property. Like other static members, a static property is not associated with a specific instance and cannot be referenced through an instance. Instead, it is associated with the type and can only be referenced an instance. Instead, it is associated with the type and can only be referenced through the type name. For example, in the following statements:

 

Button okButton = new Button();

// using an instance property

string s = okButton.caption;

the caption property is associated with the instance okButton. If caption is as a static property, the class name (Button) must be used instead :

// using static property

strings = Button. Caption;

An instance of a class can be accessed using this in the accessors of an instance property, but it is an error to use this in the accessors of a static property. It is an error to use a virtual, abstract, or override modifier on an accessor of a static property.

The following example demonstrates instances, static and read-only properties. It accepts the name of the employee from the keyboard, increments number of Emp by 1, and displays the Employee name and number.

 

Use of property.

 

using system;

public class Employee

{

public static int numberofEmp;

private static int counter;

private string name;

public string Name

{

   get

         {

   return name;

         }

   set

       {

         name = value;

}

}

public static int Counter

{

get

{

return counter;

}

set

{

counter = value;

}

}

public Employee()

{

Counter = ++ Counter + numberofEmp;

}

}

public class MainClass

{

public static void Main()

{

Employee.numberofEmp = 100;

Employee e1 = new Employee();

e1.Name = “deepika”;

console.writeline (“Employee number: {O}”, Employee.counter);

console.writeline (“Employee name: {O}”, e1.Name);

}

}

 

OUTPUT:

 

Employee number: 101

Employee name: Deepika

Accessors

The accessor of a property contains the executable statements associated with getting (reading or computing) or setting (writing) the property. The accessor declarations take the following forms:

 

set {accessor body}

get {accessor body}

where, accessor body is a block that contains the statements to be executed when the accessor is invoked.

 

The get Accessor

 

The body of the get accessor is similar to that of a method. It must return a value of the property type. The execution of the get accessor is.equivalent to reading the value of the field.

The following is a get accessor that returns the value of private field name :

 

private string name; II the name field

public string Name II the Name property

{

get

{

return name;

}

}

When you reference the property, except as the target of an assignment, the .get accessor is invoked to read the value of the property. For example:

Emp1oyee el = new Employee ();

–

–

console.write (el.Name); II The get accessor is invoked here

The get accessor must terminate is a return or throw statement and control can not flow off the accessor body.

The set Accessor

The set accessor is similar to a method that returns void. It uses an implicit parameter called value, whose type is the type of the property. In the following example, a set accessor is added the Name property.

public string Name

{

       get

     {

             return name;

       }

       set

     {

             name = value;

       }

}

When you assign a value to the property, the set accessor is invoked with an argument that provides the new value. For example:

 

e1 .Name = “Deepika”, // The set accessor is invoked here

It is an error to use the implicit parameter name (value) for a local variable declaration in a set accessor.

A property is classified according to the accessors used as follows:

• A property with a get accessor only is called a read-only property. You can not assign a value to a read-only property.

• A property with a set accessor only is called write-only property. You can not reference a write only property except as a target of an assignment.

• A property with both get and set accessors is a read-write property.

In a property declaration, both the get and set accessor must be declared inside the body of the property.

You’ll also like:

  1. What are objects and properties? How are they related to each other
  2. What are the common properties of the Controls
  3. Properties in Java Example
  4. PHP visibility of the Methods and Properties
  5. What is the purpose of record count and absolute position properties
Next →
← Prev
Like/Subscribe us for latest updates     

About Dinesh Thakur
Dinesh ThakurDinesh Thakur holds an B.C.A, MCDBA, MCSD certifications. Dinesh authors the hugely popular Computer Notes blog. Where he writes how-to guides around Computer fundamental , computer software, Computer programming, and web apps.

Dinesh Thakur is a Freelance Writer who helps different clients from all over the globe. Dinesh has written over 500+ blogs, 30+ eBooks, and 10000+ Posts for all types of clients.


For any type of query or something that you think is missing, please feel free to Contact us.


Primary Sidebar

C# Tutorials

C# Tutorials

  • C# - .NET Languages Types
  • C# - Dot Net
  • C# - Dot Net Framework
  • C# - Inheritance Types
  • C# - Features
  • C# - CTS
  • C# - CLS
  • C# - CLR
  • C# - Console
  • C# - MSIL
  • C# - Base Class Library
  • C# - Web Forms Creation
  • C# - C# Vs C++
  • C# - Statements Types
  • C# - JIT
  • C# - CLI
  • C# - Controls Types
  • C# - String Types
  • C# - Execution Model

Other Links

  • C# - PDF Version

Footer

Basic Course

  • Computer Fundamental
  • Computer Networking
  • Operating System
  • Database System
  • Computer Graphics
  • Management System
  • Software Engineering
  • Digital Electronics
  • Electronic Commerce
  • Compiler Design
  • Troubleshooting

Programming

  • Java Programming
  • Structured Query (SQL)
  • C Programming
  • C++ Programming
  • Visual Basic
  • Data Structures
  • Struts 2
  • Java Servlet
  • C# Programming
  • Basic Terms
  • Interviews

World Wide Web

  • Internet
  • Java Script
  • HTML Language
  • Cascading Style Sheet
  • Java Server Pages
  • Wordpress
  • PHP
  • Python Tutorial
  • AngularJS
  • Troubleshooting

 About Us |  Contact Us |  FAQ

Dinesh Thakur is a Technology Columinist and founder of Computer Notes.

Copyright © 2025. All Rights Reserved.

APPLY FOR ONLINE JOB IN BIGGEST CRYPTO COMPANIES
APPLY NOW