Sponsored Ad

Sunday, September 27, 2009

C# Properties

Properties provide the opportunity to protect a field in a class by reading and writing to it through the property.In other languages,this is generally able by programs implementing specialized getter and setter methods. C# backdrop accredit this blazon of aegis while aswell absolution you admission the acreage just like it was a field.

Example:

using System;
public class Customer
{
    private int m_id = -1;
    public int GetID()
    {
        return m_id;
    }
    public void SetID(int id)
    {
        m_id = id;
    }
    private string m_name = string.Empty;
    public string GetName()
    {
        return m_name;
    }
    public void SetName(string name)
    {
        m_name = name;
    }
}
public class CustomerManagerWithAccessorMethods
{
    public static void Main()
    {
        Customer cust = new Customer();
        cust.SetID(1);
        cust.SetName("Amelio Rosales");
        Console.WriteLine(
            "ID: {0}, Name: {1}",
            cust.GetID(),
            cust.GetName());
        Console.ReadKey();
    }
}

0 comments:

Post a Comment

Sponsored Ad

Website Update

Followers