Properties

In this chapter you will learn:

  1. What is class properties

What is properties

Properties are fields with logics. C# uses get and set keywords to declare a property.

A property consists of a name along with get and set accessors. The accessors are used to get and set the value of a variable.

The general form of a property is shown here:

type propertyName {/*from  j a  v  a  2 s  .  co m*/
    get {
        // get accessor code
    }
    
    set {
        // set accessor code
    }
}

A property cannot be passed as a ref or out parameter to a method. You cannot overload a property.

A property should not alter the state of the underlying variable when the get accessor is called.

get and set are called property accessors. get accessor is called when reading the property and set accessor is called when assigning value to the property.

value is the parameter, which designates the value being assgined.

A property can have the following modifier:

  • static
  • public
  • internal
  • private
  • protected
  • new
  • virtual
  • abstract
  • override
  • sealed
  • unsafe
  • extern
using System;/*  j  a v a  2s .  c  o  m*/
class Rectangle{
   private int width;
   
   public int Width{
      get{
         return width;
      }
      set{
         width = value;
      }
   }
}

class Program
{
    static void Main(string[] args)
    {
        Rectangle r = new Rectangle();
        r.Width = 5;
        Console.WriteLine(r.Width);
    }
}

The output:

Next chapter...

What you will learn in the next chapter:

  1. Add statement to the getter and setter of a property
  2. Put logic to property setter
  3. throw Exception from property setting
Home » C# Tutorial » Class
Classes and Structs
Encapsulation
Class and Struct Accessibility
Members
Static Members
Class
Class instance variables
Class instance Methods
Class Inheritance
Class constructor
Default constructors
Parameters of constructors
Constructor overloading
Static Constructor
Destructor
Override destructor
new operator
Class field
Static Fields
Read only field
Field default value
const value
Abstract class
Static Class
Partial type
Methods
Method local variable
Method Overloading
Method overloading with ref and out
Type conversion and method overloading
Error in Method overloading
virtual methods
Override methods
Method Parameter
Pass parameters
ref parameters
out parameters
Variable parameter length
Optional parameters
Named parameters
static method
function Return
Recursive methods
Virtual methods and polymorphism
Cast
Shadow inherited members
Seal a member
this keyword
base keyword
base and name hiding
Access Specifiers
private
public
protected
internal
Indexer
String type indexer
Multidimensional indexer
Indexer overloading
Indexer to append
Indexer logic
Properties
Properties getter and setter
Accessibilities of properties
Automatic properties
Read only and write only property
Virtual properties
Abstract Properties
Static Properties
interface
interface implementation
Explicit interface implementation
Interface inheritance
Indexer in an interface
Interface Properties
struct and interface
virtual interface implementation
as operator
is operator
null reference
Nested classes
object class
ToString method
GetHashCode
Object initialization
Extension method
Struct Instances vs. Class Instances