Tuesday 9 August 2011

Encapsulation in C Sharp

Encapsulation: Means protect or prevent data from corruption (Nothing but Hiding the data ).
Example With help of Properties:
using System;
public class Grossary
{
    private string item;

    public string Item
    {
     get{ return item; }
     set { item=Value; }
    }
}

Public Class GrossaryMain
{
    public static int Main(string[] args)
    {
    Item Id=new Item();
    id.item="Sugar";
    Console.Writeline("item :{0}",id.item);
    return 0;
    }
}

OutPut :Sugar.
Description:
We can not access the private data outside of the Grossary() class.
so we modified it with help of properties get, set by using method Item().

The above Properties Get is used to return the value.
             Set is used to Set the Value.

No comments:

Post a Comment