Friday, 27 July 2012

Properties And Indexers


PROPERTIES

Properties provide the opportunity to protect a field in a class by reading and writing to it through the property. C# properties enable this type of protection while also letting you access the property just like it was a field.
Another benefit of properties over fields is that you can change their internal implementation over time. With a public field, the underlying data type must always be the same because calling code depends on the field being the same. However, with a property, you can change the implementation. For example, if a customer has an ID that is originally stored as an int, you might have a requirements change that made you perform a validation to ensure that calling code could never set the ID to a negative value. If it was a field, you would never be able to do this, but a property allows you to make such a change without breaking code.


Defining property

using System;
class Employee
{
string _empName;
public string EmpName
{
get
{
return _empName;
}
set
{
_empName = value;
}
}
}
class SalaryDetails : Employee
{
static void Main(string [] args)
{
SalaryDetails obj = new SalaryDetails();
obj.EmpName = "Frank";
Console.WriteLine ("Name : " + obj.EmpName);
}
}


Defining several Properties

using System;
using System.Collections.Generic;
using System.Text;
namespace ConsoleApplication1
{
class Employee
{
string _empName;
int _empID;
float _salary;
public string EmpName
{
get
{
return _empName;
}
set
{
_empName = value;
}
}
public int EmpID
{
get
{
return _empID;
}
set
{
_empID = value;
}
}
public float Salary
{
get
{
return _salary;
}
set
{
_salary = value;
}
}
}
class SalaryDetails : Employee
{
static void Main(string[] args)
{
SalaryDetails obj = new SalaryDetails();
obj.EmpName = "Frank";
obj.EmpID = 10;
obj.Salary = 1000.25F;
Console.WriteLine("Name : " + obj.EmpName);
Console.WriteLine("ID : " + obj.EmpID);
Console.WriteLine("Salary : " + obj.Salary + "$");
}
}
}


ToString() property

using System;
public class Person
{
private string myName="N/A";
private int myAge = 0;
public string Name
{
get
{
return myName;
}
set
{
myName = value;
}
}
public int Age
{
get
{
return myAge;
}
set
{
myAge = value;
}
}
public override string ToString()
{
return "Name: " + Name + " Age: " + Age;
}
public static void Main ()
{
Person obj = new Person();
Console.WriteLine("Person Details: {0}", obj);
obj.Name = "Delhi";
obj.Age = 100;
Console.WriteLine("Person Details: {0}", obj);
obj.Age += 1;
Console.WriteLine("Person Details: {0}", obj);
}
}


Properties: Implementing Polymorphism

Example 1
using System;
class Car
{
string _carType;
public virtual string CarType
{
get
{
return _carType;
}
set
{
_carType = value;
}
}
}
class Ferrari : Car
{
public override string CarType
{
get
{
return base.CarType;
}
set
{
base.CarType = value;
}
}
static void Main(string [] args)
{
Car objCar = new Car();
objCar.CarType = "Utility Vehicle";

Ferrari objFerrari = new Ferrari();
objFerrari.CarType = "Sports Car";
Console.WriteLine("Car Type: " + objCar.CarType);
Console.WriteLine("Ferrari Car Type : " + objFerrari.CarType);
}
}


Example 2:
using System;
using System.Collections.Generic;
using System.Text;
namespace ConsoleApplication1
{
class Car
{
string _carType; v public virtual string CarType
{
get
{
return _carType;
}
set
{
_carType = value;
}
}
}
class Ferrari : Car
{
string _carType;
public override string CarType
{
get
{
return base.CarType;
}
set
{
base.CarType = value;
Console.Write("Enter car Name: ");
_carType = Console.ReadLine();
}
}
static void Main(string[] args)
{
Car objCar = new Car();
objCar.CarType = "Utility Vehicle";
Ferrari objFerrari = new Ferrari();
objFerrari.CarType = "Sports Car";
Console.WriteLine("Car Type: " + objCar.CarType);
Console.WriteLine("Ferrari Car Type : " + objFerrari.CarType);
Console.WriteLine(objFerrari._carType);
}
}
}


Types of Properties

Read-Only Property

using System;
using System.Collections.Generic;
using System.Text;
namespace ConsoleApplication1
{
class Books
{
string _bookName;
long _bookID;
public Books(string name, int value)
{
_bookName = name;
_bookID = value;
}
public string BookName
{
get
{
return _bookName;
}
}
public long BookID
{
get
{
return _bookID;
}
}
public void show()
{
Console.WriteLine(BookID + " " + BookName );
}
}
class BookStore
{
static void Main(string[] args)
{
Books obj = new Books("Learn C# in 21 Days", 10015);
Console.WriteLine("Book Name: " + obj.BookName);
Console.WriteLine("Book ID: " + obj.BookID);
obj.show();
}
}
}


Write-Only Property

using System;
using System.Collections.Generic;
using System.Text;
namespace ConsoleApplication1
{
class Department
{
string _deptName;
int _deptID;
public string DeptName
{
set
{
_deptName = value;
}
}
public int DeptID
{
set
{
_deptID = value;
}
}
public void Display()
{
Console.WriteLine("Department Name: " + _deptName);
Console.WriteLine("Department ID: " + _deptID);
}
}
class Company
{
static void Main(string[] args)
{
Department obj = new Department();
obj.DeptID = 201;
obj.DeptName = "Sales";
obj.Display();
}
}
}


Read-Write Property

using System;
namespace ConsoleApplication5
{
class Product
{
string _productName;
int _productID;
float _price;
public Product(string name, int val)
{
_productName = name;
_productID = val;
}
public float Price
{
get
{
return _price;
}
set
{
if (value < 0) { _price = 0; } else { _price = value; } } } public void Display() { Console.WriteLine("Product Name: " + _productName); Console.WriteLine("Product ID: " + _productID); Console.WriteLine("Price: " + _price + "$"); } } class Goods { static void Main(string[] args) { Product obj = new Product("Hard Disk", 101); obj.Price = 345.25F; obj.Display(); } } }


Static Properties

using System;
using System.Collections.Generic;
using System.Text;
namespace ConsoleApplication1
{
class University
{
private static string _department;
private static string _universityName;
public static string Department
{
get
{
return _department;
}
set
{
_department = value;
}
}
public static string UniversityName
{
get
{
return _universityName;
}
set
{
_universityName = value;
}
}
}
class Physics
{
static void Main(string[] args)
{
University.UniversityName = "University of MaryLand";
University.Department = "Physics";
Console.WriteLine("University Name: " + University.UniversityName);
Console.WriteLine("Department Name: " + University.Department);
}
}
}


Abstract properties

using System;
using System.Collections.Generic;
using System.Text;
namespace ConsoleApplication3
{
public abstract class Figure
{
public abstract float DimensionOne
{
set;
}
public abstract float DimensionTwo
{
set;
}
}
class Rectangle : Figure
{
float _dimensionOne;
float _dimensionTwo;
public override float DimensionOne
{
set
{
if (value <= 0) { _dimensionOne = 0; } else { _dimensionOne = value; } } } public override float DimensionTwo { set { if (value <= 0) { _dimensionTwo = 0; } else { _dimensionTwo = value; } } } float Area() { return _dimensionOne * _dimensionTwo; } static void Main(string[] args) { Rectangle obj = new Rectangle(); obj.DimensionOne = 4; obj.DimensionTwo = 8; Console.WriteLine("Area of Rectangle : " + obj.Area()); } } }


INDEXERS

Indexers are an elaboration of properties, and are used where you want to access some class property by index, in an array like manner. They are useful in cases where a class is a container for other objects.
using System;
using System.Collections.Generic;
using System.Text;
namespace ConsoleApplication1
{
class Salary
{
private float[] salary = new float[3];
public float this[int index]
{
get
{
return salary[index];
}
set
{
if (value < 0) { Console.WriteLine("Salary cannot be negetive."); return; } else { salary[index] = value; } } } static void Main(string[] args) { Salary obj = new Salary(); obj[0] = 1000F; obj[1] = 2000F; obj[2] = 3000F; Console.WriteLine("Salary Details"); for (int i = 0; i < 3; i++) { Console.WriteLine(obj[i] + "$"); } } } } Example 2 using System;
using System.Collections.Generic;
using System.Text;
namespace ConsoleApplication1
{
public class IndexTest
{
double[] values;
public IndexTest()
{
values = new double[] { 1.1, 3.3, 5.5, 7.7, 9.9 };
}
public double this[int index]
{
get
{
if (index < 0 || index > values.Length)
throw new ArgumentOutOfRangeException();
return values[index];
}
set
{
values[index] = value;
}
}
}
public class test
{
public static int Main()
{
IndexTest it = new IndexTest();
Console.WriteLine("Index 1 is: " + it[4]);
return 0;
}
}
}


Indexer - Inheritance

using System;
using System.Collections.Generic;
using System.Text;
namespace ConsoleApplication1
{
class Numbers
{
private int[] num = new int[3];
public int this[int index]
{
get
{
return num[index];
}
set
{
num[index] = value;
}
}
}
class EvenNumbers : Numbers
{
public static void Main()
{
EvenNumbers obj = new EvenNumbers();
obj[0] = 0;
obj[1] = 2;
obj[2] = 4;
for (int i = 0; i < 3; i++) { Console.WriteLine(obj[i]); } } } }


Indexer - Polymorphism

using System;
using System.Collections.Generic;
using System.Text;
namespace ConsoleApplication1
{
class Student
{
string[] studName = new string[2];
public virtual string this[int index]
{
get
{
return studName[index];
}
set
{
studName[index] = value;
}
}
}
class Result : Student
{
string[] result = new string[2];
public override string this[int index]
{
get
{
return base[index];
}
set
{
base[index] = value;
}
}
public static void Main(string[] args)
{
Result objResult = new Result();
objResult[0] = "First";
objResult[1] = "Pass";
Student objStu = new Student();
objStu[0] = "Peter";
objStu[1] = "Patrick";
for (int i = 0; i < 2; i++) { Console.WriteLine(objStu[i] + "\t\t" + objResult[i] + "class"); } } } }


Multiple parameters in Indexers

using System;
class Account
{
string[,] accountDetails = new string[4, 2];
public string this[int pos, int column]
{
get
{
return (accountDetails[pos, column]);
}
set
{
accountDetails[pos, column] = value;
}
}
public static void Main(string[] args)
{
Account obj = new Account();
obj[0,0] = "aptech";
Console.WriteLine(obj[0, 0]);
}
}


Indexers in Interfaces

using System;
public interface IDetails
{
string this [int index ]
{
get;
set;
}
}
class Students : IDetails
{
string [] studentName = new string[3];
//int [] studentID = new int[3];
public string this [int index]
{
get
{
return studentName[index];
}
set
{
studentName[index] = value;
}
}
public static void Main(string [] args)
{
Students obj = new Students();
obj [0] = "James";
obj [1] = "Wilson";
obj [2] = "Patrick";
Console.WriteLine("Student Names:");
Console.WriteLine();
for(int i=0; i<3; i++) { Console.Write (obj[i] + " "); } } }

No comments:

Post a Comment