Thursday, July 1, 2010

PROPERTIES IN C#

In the .NET Framework provides a standard way to specify and mutate name in the C# language.
you can access this type and avoid writing your own name routines.Properties combine aspects of both fields and methods
Properties are members that provide a flexible mechanism to read, write, or compute the values of private fields
A get property accessor is used to return the property value, and a set accessor is used to assign a new value. These accessors can have different access levels. For more information, see Accessor Accessibility.


Example Programe

using System;
using System.Collections;
public class Car
{
private string name;
public string Name
{
get{return name;}
set{name=value;}
}
public Car(string n)
{name=n;}
}
public class CarCollection:CollectionBase
{
public void Add(Car newcar)
{List.Add(newcar);}
public void Remove(Car oldcar)
{List.Remove(oldcar);}
public Car this[int index]
{
get{return (Car)List[index];}
set{List[index]=value;}
}
}








class properties
{
public static void Main(string[] args)
{
CarCollection c=new CarCollection();
c.Add(new Car("santro"));
c.Add(new Car("suzuki"));
c.Add(new Car("nano"));
c.Add(new Car("azer"));

foreach(Car car in c)
{
Console.WriteLine(car.Name);
}

Console.WriteLine(c[2].Name);
c[2].Name="zen";
Console.WriteLine(c[2].Name);

foreach(Car car in c)
{
Console.WriteLine(car.Name);
}
}
}

No comments:

Post a Comment